Usart troubles.

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
mgrondstra
Posts: 11
Joined: Mon May 10, 2010 12:02 pm
Location: Netherlands, Enschede

Usart troubles.

Post by mgrondstra » Thu May 27, 2010 1:07 pm

Hi all,

I have another question. Since i've got my glcd up and running with you're help i have been working on PIC-GPS communications.

And with this i can't seem to get the usart bit working.

I have the following code:

Code: Select all

Device = 18F4550 
Clock = 8 

Config 
   PLLDIV = 2, 
   CPUDIV = OSC1_PLL2, 
   USBDIV = 2, 
   FOSC = INTOSCIO_EC, 
   VREGEN = ON     


 //====user settings to override default module options ==== 
#option GLCD_MODEL = S1D13700      //Controller mounted on display 
#option GLCD_MODE = 8080          //hardware default M6800 Indirect CNF<3:2> 

#option GLCD_DATA = PORTD          // Data port - only 8 bit port 
#option GLCD_RES = PORTB.5         // reset pin    
#option GLCD_RD = PORTB.4          // RD pin - 8080 mode  
#option GLCD_WR = PORTB.3          // WR pin - 8080 mode 
#option GLCD_A0 = PORTB.2          // A0 pin 
#option GLCD_CS = PORTB.0          // chip select 

#option GLCD_TCR = $2C             // TC/R setting to fix flicker problem on display override system  instruction ($48) 
#option GLCD_ASPECT_RATIO = 100    // aspect ratio, smaller number will squeeze y For GLCD circles And Boxes 
#option GLCD_INIT_DELAY = 100      // initialisation delay (ms) 

Dim
 msg(12) As Byte
 
Include "Usart.bas"
Include "Convert.bas"
Include "glcd.bas"
Include "arial.bas"

Public Function SendMessage(ByRef message() As Byte, length As Byte) As Boolean
Dim
 checksum As Byte,
 i As Byte,
 acked As Byte,
 receive As Byte,
 receive1 As Byte,
 receive2 As Byte,
 receive3 As Byte,
 receive4 As Byte,
 receive5 As Byte
 
High(PORTA.0)
checksum = $00
//Header
USART.WriteByte($A0)
USART.WriteByte($A1)

//Payload length
USART.WriteByte($00)
USART.WriteByte(length)
High(PORTA.1)
//payload
i = 0
While i < length
 USART.WriteByte(message(i))
 checksum = checksum Xor message(i)
Wend
High(PORTA.2)
//checksum
USART.WriteByte(checksum)
//End message
USART.WriteByte($0D)
USART.WriteByte($0A)

acked = 0
receive = 0
receive1 = 0
receive2 = 0
receive3 = 0
receive4 = 0
receive5 = 0
High(PORTA.3)
While acked=0

If DataAvailableTimeout(20) Then
     receive5 = receive4
     receive4 = receive3
     receive3 = receive2
     receive2 = receive1
     receive1 = receive

     receive = USART.ReadByte()
     High(PORTA.4)
EndIf

If receive4 = $A1 Then
 High(PORTA.5)
 If USART.ReadByte() = $83 Then
 acked = 1
 GLCD.WriteAt(0,14,"NACK")
 DelayMS(2000)
 ElseIf USART.ReadByte() = $84 Then
 acked = 1
 GLCD.WriteAt(0,14,"NACK")
 DelayMS(2000)
 EndIf
EndIf

Wend 
Low(PORTA.0)
Low(PORTA.1)
Low(PORTA.2)
Low(PORTA.3)
Low(PORTA.4)
Low(PORTA.5)
End Function


/// Start Main program
    
//OSCTUNE.6 = 0           // Turn off/on PLL
OSCCON = %01110110 
   
Pen.Color = 1 
Brush.Color =1 
Pen.Mode = 0     // pen modes, pmCopy = 0, pmMerge = 1, pmXOR = 2, 
Brush.Style = 0   //1=bsClear  0=bsSolid 
SetFont(Arial)

//Set baudrate
msg(0) = $05
msg(1) = $00
msg(2) = $03
msg(3) = $00
GLCD.WriteAt(0,0,"Baudrate")
SendMessage(msg(), 4)
USART.SetBaudrate(br38400)
Cls(1)
And my GLCD shows "baudrate" on the screen. But only PortA.0 is high(one led is on)

I suspect i'm doing something wrong with addressing the uart on my pic or gps. I know for sure i have connected the gps correctly, because i had it running using MikroC.

Can someone shed some light on this?

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

Post by Francis » Thu May 27, 2010 4:49 pm

I've only had a 20 second glance.

"And with this i can't seem to get the usart bit working"

- which bit (part) is not working ?

Are you 'talking' to Terminal on your PC?
Are your serial connections correct? (Levels and polarity).

Maybe start with some simple USART tests and rearrange your code.

Your code calls the subroutine before you have set the USART baudrate.

And it appears only to call the sub once.
So with a Datavailable timeout of 20mS there's very little chance you'll be able to send it anything in time.
You may need to rethink the structure or explan the code a bit more.

PS. A nice tab-sized indent of code in subroutines and loops etc. makes it MUCH more readable for others :wink:

mgrondstra
Posts: 11
Joined: Mon May 10, 2010 12:02 pm
Location: Netherlands, Enschede

Post by mgrondstra » Thu May 27, 2010 6:52 pm

The PIc is connected to an gps unit. This unit gives an steady flow of nmea messages via the usart tx(gps/rx(PIC). To configure this gps i have to send some messages. These messages are not transmitted, the code stops working at the HIGH(PORTA.0) line in the function sendmessage.

If this works i have to send more messages about update freq, and format of the nmea string. But i want to start with setting the baudrate.

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

Post by Francis » Fri May 28, 2010 8:00 am

I can't pretend to follow it properly, but:-

As you haven't set port direction (or anything) then set the UASRT.Setbaudrate before you do any USART writes.

I don't know why you have your subroutine as a function - it doesn't appear to return anything.

As it stands in your example:

Code: Select all

i = 0 
While i < length 
 USART.WriteByte(message(i)) 
 checksum = checksum Xor message(i) 
Wend
will just sit there all day looping.
PS I've assumed you've set your clock correctly. It's a good idea to check and 'rehearse' your serial comms using Serial Communicator window in SF.

Post Reply