PDA

View Full Version : My USART problems..



Glenn
- 29th July 2009, 17:43
I'm building a thing that really could use some serial out and input, so I decided to try to finally use the USART's in some pics..

First I tried to get it to work in my nearly-finished testrig for the rest of the stuff, but when this didnt work I built a "serial testring" to be able to sort out the problems.

But no. get nothing in my term program.

This is my testcode..



' Serial testrig 1.0 Glenn a t repulsiv.se

@ device PIC16F877A,HS_OSC,LVP_OFF,WDT_OFF,PROTECT_OFF

' Lets define..

DEFINE OSC 20 ' Lets work at 20MHz

' And the LCD..
DEFINE LCD_DREG PORTD ' Set LCD Data port
DEFINE LCD_DBIT 0 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTD ' Set LCD Register Select port
DEFINE LCD_RSBIT 4 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTD ' Set LCD Enable port
DEFINE LCD_EBIT 5 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us

' And the serial port using USART.
DEFINE HSER_BAUD 9600 ' HSER baud rate
DEFINE HSER_SPBRG 129 ' HSER SPBRG init
DEFINE HSER_RCSTA 90h ' HSER receive status init
'DEFINE HSER_TXSTA 24h ' HSER transmit status init high speed
DEFINE HSER_TXSTA 20h ' HSER transmitter init


pause 1000 ' Gracetime for the LCD

Main:
LCDOUT $FE, 1, "Hello world"
pause 500
LCDOUT $FE, $C0, "Again and again"
pause 500
LCDOUT $FE, 1
pause 500
gosub serial
pause 500
goto main:

serial:
HSEROUT ["Glenn",10]
Return


..I tried different things, but this should work ? or ?

I attached a schematics of my simple testrig.

Glenn
- 29th July 2009, 17:45
Hrrm, yes, I can see that I forgot two things in the schematics, first of all the MAX233 have a 1uF cap close to its Vcc and gnd, and I also put a swicth that change the position of RX/TX coz you never know what you connect it to.

I set my terminal program to 9600bps 8N1, no flowcontrol.

mackrackit
- 29th July 2009, 18:06
Looks like pins 2 and 3 on the DB9 backwards?

mackrackit
- 29th July 2009, 18:20
I did not see the part about the tx/rx switch.

Do you have a copy of Pic Multicalc?
http://www.picbasic.co.uk/forum/showpost.php?p=74024&postcount=4

Glenn
- 29th July 2009, 19:02
Yea, since I use a cable that is crossed I connected it that way, but later I added the switch that change the positions, mainly coz I wasnt really sure how it should be :)

Yea, I have picmulticalc, but I didnt really thought of that it had USART help.

And yes..

I choosed 20Mhz 9600bps and copied the defines to my code (replacing the old ones), and voila, it worked.

Niice.

But I still dont undersstand why it didnt work before ?

..This was the defines picmulticalc gave me:

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_SPBRG 129 ' 9600 Baud @ 20MHz, 0,16%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically

mackrackit
- 29th July 2009, 19:12
I have never tried DEFINE_BAUD
maybe that was the problem.

Glenn
- 30th July 2009, 00:25
Hmm, now I have problem with reading from the USART instead..

I searched the forum and found ALOT of threads, but none that really solved my prob..

I want to read a string of chars, like 16, BUT if a CR (13) occurs it should skip the rest.

I.e I want to be able to write strings UP to 16 chars in my terminal program, and get them as a string or array.

This seem to be a bit harder than I first thought ?

It was very easy to read single chars with HSERIN.. and I thiought it would be easy with something like

HSERIN [ str rxarr\16\13 ]

..But no ?

Does anyone have a bit of sample code that do this using HSERIN ?

mackrackit
- 30th July 2009, 01:37
Have you used SEROUT2/SERIN2 much? The hardware command use the same syntax.

Off the top I would try
$d
in place of
13

What exactly is or is not happening?

Glenn
- 31st July 2009, 00:33
Well, I havent used SERIN, never really had use of the serial stuff before.

Now I have:

main:

HSERIN [ str rxarr\8\13 ]
LCDOUT $FE, 1, "Serial data is:"
LCDOUT $FE, $C0, rxarr
pause 500
goto main:


..This just print the first char I write.. I want rxarr to contain eight chars.

btw I found this on melabs website, which made me even more confused..

http://melabs.com/resources/samples/pbp/usart.bas

mackrackit
- 31st July 2009, 01:00
I think I see what you need. When you use the STR thing it is creating an array. Here is an example.


SERIN2 PORTB.5,24972,[WAIT("A"),STR NUMS\16] '7, Even
X1 = (NUMS[1])
X2 = (NUMS[3])
X3 = (NUMS[5])
X4 = (NUMS[7])
X5 = (NUMS[9])
X6 = (NUMS[11])
X7 = (NUMS[13])
X8 = (NUMS[15])

the above is saving a 16 character array but I only want every other bit. Now I have eight variables X1 to X8. Do what ever you need with them.

Sorry for the poor example but it is what I have with me.