USART Question

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

USART Question

Post by xnederlandx » Wed Jul 01, 2009 6:06 am

Hello,

I have a question regarding USART. I want to communicate between two PIC's (18F4680) serially.


The situation

PIC1:

- Monitors sensors(Temperature, Voltage, Current)
- Controlls outputs(Fans (PWM), Transistors, Relays)
- Sends data Serially to PIC2.
- Receives limited data from PIC2.

PIC2:
- Receives data from PIC1 and Displays on GLCD (EasyPIC 5 GLCD - KS0108 Driver)
- Pushbuttons to switch between screens, and change settings.
- Sends settings back to PIC1.

I'm confused as to why there is a "read" & "write" command in the USART library, aswell as a "send" command, and which one I need to use.

Is there any sample code which I should look at?

Thanks in advance.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Wed Jul 01, 2009 9:23 am

Ah you old PICAXErs!

I don't have a 'send' in my USART library, is this something new?

Read reads from the USART and Write writes to the USART.
USART.Write("Hello"), writes Hello to USART

Set your baud rate first using USART.SetBaudrate(br9600) for example.

Use the DataAvailable or DataAvailableTimeout(x) to check for bytes in the receive buffer.

Have a look in the samples folder under 'USART'. Examples are a bit limited but it should get you started.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Wed Jul 01, 2009 10:11 am

Thankyou for the help.

I'll post here once I've got something.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Wed Jul 01, 2009 9:35 pm

Code: Select all

Device = 18F4431                            '
Clock = 32                                  ' PIC 18F4431 Config with external 8 Mhz Resonator
Config OSC = HSPLL                          '

#option GLCD_data = PORTD                   '
#option GLCD_RS = PORTB.2                   ' GLCD PIN CONFIGURATION
#option GLCD_EN = PORTB.4                   '
#option GLCD_RW = PORTB.3                   ' AS PER EASYPIC5 SCHEMATIC
#option GLCD_CS1 = PORTB.1                  '
#option GLCD_CS2 = PORTB.0                  '
#option GLCD_RST = PORTB.5                  '
#option GLCD_invert_cs = true               '
#Option GLCD_INIT_DELAY = 100               '
#option GLCD_MODEL = KS0108                 '


Include "glcd.bas"                          'Load GLCD Library
Include "tahoma.bas"                        'Load Tahoma Font
Include "graphics"                          'Load Graphics Library
Include "USART.bas"                         'Load USART Library.
Include "Convert.bas"                       'Load Conversion Library.


Dim     S_ID As Byte                        'First Byte of Serial Message (byte), which tells us what the data (I.E. Temperature) is we are recieving
Dim     S_Data As Word                      'Next Part of Serial Message (word), which tells us the data is. 

SetBaudrate(br115200)                       'Specifies USART Baudrate
                                            'Can be br300, br600, br1200, br2400, br4800, br9600, br19200, br38400, br57600 or br115200.
                                            
Input(PORTC.1)                              'Pushbutton as Input
 Output(PORTC.0)

SetFont(Tahoma)
Font.Style = fsNormal
TextAlign = taLeft
Cls
GLCD.Rectangle(0,0,127,63)  

Repeat
Toggle(PORTC.0)
DelayMS(50)
                         
If DataAvailable = true Then
    Read(S_ID,S_Data)
    If S_ID = 1 Then
        GLCD.WriteStr(42,2,DecToStr(S_Data) + "C")
        
    EndIf    
EndIf

Until PORTC.Booleans(1)
I was just making a really simple program to check for data, and send it to the display. All that happens is the led turns on/off every time I push reset.

I assume DataAvailable = true, and thus it jams at the Read block?

I have had a look at the examples, but they are either super easy, or super complicated.

Sorry If I am asking stupid questions...

Thanks in Advance.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Thu Jul 02, 2009 8:01 am

Only a 10 second look.

May I suggest that you get into the practice of putting the Include name in front of he function.

e.g. change Read to USART.Read etc. It can save confusion later on.

Dataavailable is just easy-to-read shorthand for checking PIR1.5.
So, your code should only read when that bit is set, but then it'll sit there looking for 2 bytes.

Can you write to your GLCD? If so, read PIR1.5 and display it to see if something funny is going on.

What do you have connected to Rx pin on your PIC?
If all on EasyPIC board, have you have jumpered your EasyPIC board correctly?
Have you actually tried sending it 2 bytes?
Baud rate correct?
Have you tried it with EasyPIC LEDs switched on AND off?

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Thu Jul 02, 2009 9:12 pm

I have the serial communication working. :)

I needed to set a few switchblocks on the EasyPIC 5 Board for the RS232 interface.

Then I also needed to get a serial port on my PC (to emulate the other PIC), as my original one wasn't functioning any more. Then I also needed to find a cable that worked.

so.... 4 hours and 10 cables later I got the echo sample program working. :D

I shall post again soon, once I have further experimented.

Edit: Is there anyway of checking if there are 3 bytes?

I can't find a way of just putting the text I receive onto the GLCD.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Thu Jul 02, 2009 10:37 pm

Code: Select all

Repeat
Toggle(PORTC.0)
DelayMS(50)                      
If USART.DataAvailable = true Then
    S_ID(0) = USART.ReadWord
    USART.WriteByte(S_ID(0))
    USART.ClearOverrun()
    GLCD.WriteStr(42,2,S_ID(0) + "  ")   
EndIf

Until PORTC.Booleans(1)
                            
That seems to work - I can get the first character I type in the terminal on the screen.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Thu Jul 02, 2009 11:00 pm

"Edit: Is there anyway of checking if there are 3 bytes? "

- why not just read until NOT DataAvailable in a loop and increment a counter on each loop. Thats what I do.
Works perfectly.

I think you are thinking back to front.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Fri Jul 03, 2009 1:47 am

Is the buffer only 1 byte?

I can't seem to send multiple characters in my terminal at a time.

I would like to type in 3 characters on my terminal, and them to show on my GlCD. I am currently only able to show 1.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Fri Jul 03, 2009 2:31 am

I found the problem. I was using a bad Terminal Program. Should of used the windows one.

It was sending all sorts of rubish at the end of characters.


Thankyou very much for the help :D
I have the serial communication working flawlessly now.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Mon Aug 10, 2009 7:05 am

I'm having problems with the USART again. :x

I've done alot of testing to find out what the problem is. I can't seem to find what it is that is causing it.

-I send data to the PIC18F2525 from the serial program in my computer.
-It'll only take one byte, and will keep jamming.
-When I show that byte on the display (by means of a DecToStr conversion, I get the value "0" no matter what I send it.

I'm clueless. All I want it to do is receive 3 bytes @ 9600 baud (from the Picaxe). :(
Could somone please help me with this?

PS:
Yes I have used setalldigital, Set the baudrate, selected the right switchblocks.

Thanks in advance.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Mon Aug 10, 2009 9:16 am

Post the section code you are using for reading.

You sould be able to read oodles of bytes @9600 in a simple While DataAvaialble Wend loop.

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Mon Aug 10, 2009 9:31 am

Code: Select all

While USART.DataAvailable = true              'If there is data in the Buffer.                  
SID = USART.ReadByte                       'Read first byte of message from USART buffer
USART.ClearOverrun                          'Clear Buffer Overrun.
SData.Byte0 = USART.ReadByte
USART.ClearOverrun                          'Clear Buffer Overrun
SData.Byte1 = USART.ReadByte   
USART.ClearOverrun                          'Clear Buffer Overrun
wend

xnederlandx
Posts: 33
Joined: Wed Jul 01, 2009 5:55 am

Post by xnederlandx » Mon Aug 10, 2009 9:32 am

when a value comes in (I chose to display it on GLCD, for testing), it is always "0".

Edit: Does anyone have any sort of "tested" code that works for sure, and will "put" the 3 bytes into the variables.
Edit: When I send data with the picaxe, do I need to send it N9600 or T9600?

Thanks in advance.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Mon Aug 10, 2009 10:04 am

Eh?
Methinks you should have a read of the USART routne and a PIC Data Sheet.

Work on a general routine like this.

Create a byte array and then something along these lines:

Code: Select all

    Dim ByteNo As Byte
    ByteNo=0
    Repeat
        MyArray(ByteNo) = USART.ReadByte()
        Inc(ByteNo)
    Until Not USART.DataAvailableTimeout(10)
If you need to Clear then clear after the above.

If you use the timeout then yu can send variable length strings. If you wish just 3 chars then modifiy the above and count the bytes.

You will need True for PICAXE serial out.
Note that PICAXE does NOT set the pin high before the first srial out, so you will have to do that or potentially lose the first byte.

PS. when I tried a similar thingin proton years ago (i.e. PICAXE to PIC USART) I occasionally had issues with inaccuracies in baud. You may have to twiddle BRG. I only mention this in the unlikely event that you have some funnies happening.
Last edited by Francis on Mon Aug 10, 2009 10:08 am, edited 1 time in total.

Post Reply