USART Question
Moderators: David Barker, Jerry Messina
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 am
USART Question
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.
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.
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.
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.
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 am
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 am
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 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.
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?
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?
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 am
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.
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.
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.
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.
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 am
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)
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 am
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 am
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 am
I'm having problems with the USART again.
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.
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.
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 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
-
- Posts: 33
- Joined: Wed Jul 01, 2009 5:55 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:
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.
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 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.