Help .. convert to characters

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Help .. convert to characters

Post by Jerry Messina » Tue Oct 14, 2014 11:22 am

Sorry... I only meant that code as a guide for reading and displaying a message. You probably still need to use ISRRX to collect up the bytes.

You might want to try this instead:

Code: Select all

dim location as byte
dim msgb0 as byte
dim msgb1 as byte

dim w as word

USART.SetBaudrate(br9600)
ISRRX.Initialize()

while (true)
   // check for usart overrun. it'll stop data if not cleared
   if (ISRRX.Overrun) then
      ISRRX.Reset()
      // maybe display a message.
   endif      

    // now, to receive a message from the PICAXE you must always read three bytes
   // get first byte (location index)
   while (ISRRX.DataAvailable = false)
   end while
   location = ISRRX.ReadByte()

   // get second byte - lowbyte of parameter (LSB)
   while (ISRRX.DataAvailable = false)
   end while
   msgb0 = ISRRX.ReadByte()

   // get third byte - highbyte of parameter (MSB)
   while (ISRRX.DataAvailable = false)
   end while
   msgb1 = ISRRX.ReadByte()

   // display the two message bytes based on the location
   select (location)
      case 23, 24, 25, 26      // parameter is already ascii text
         GLCD.WriteAt(10, 10, msgb0, msgb1)
      else               // parameter is binary data...
         // write data as two bytes (LSB, MSB)
         GLCD.WriteAt(10, 10, dectostr(msgb0), ", ", dectostr(msgb1))

         // or write data as a single 16-bit value
         w.byte1 = msgb1
         w.byte0 = msgb0
         GLCD.WriteAt(10, 10, dectostr(w))
   end select
end while
Make sure that the PIC side is started before the PICAXE side. With that protocol, there's no way to synchronize the two sides so if they ever get out of sync by even one byte, you're hosed and would have to restart both ends.

xva3rjy
Posts: 40
Joined: Sat Sep 27, 2014 11:41 am

Re: Help .. convert to characters

Post by xva3rjy » Tue Oct 14, 2014 4:09 pm

I added a few GLCD.WhriteAt's to see whats happening during different stages.
I also attach a photo of the GLCD so that you can see my problem.
Could those be Chinese characters?

Code: Select all

Device = 18F2525
Clock =   40                              '  external crystal x 4

//Config OSC = HSPLL                       '   Config OSC = HSPLL 

#Option GLCD_MODEL = KS0108
#option GLCD_CS_COUNT = 3
#Option GLCD_SCREEN_WIDTH = 192
#Option GLCD_SCREEN_HEIGHT = 64
#Option GLCD_DATA = PORTB
#Option GLCD_RS = PORTA.3
#Option GLCD_CS1 = PORTA.4
#Option GLCD_CS2 = PORTA.5
#Option GLCD_CS3 = PORTC.6
#Option GLCD_RW = PORTA.2
#Option GLCD_EN = PORTA.1
#Option GLCD_RST = PORTA.0
#Option GLCD_ASPECT_RATIO = 75
#Option GLCD_INIT_DELAY = 100               
#Option GLCD_INVERT_CS = False

Include "GLCD.bas"
Include "FixedFont.bas"
Include "Arial.bas"
Include "Garamond.bas"
Include "Tahoma.bas"
Include "Verdana.bas"
Include "Times.bas"
Include "DS1307.bas" 
Include "convert.bas"
Include "utils.bas"
Include "graphics.bas"
Include "ks0108.bas"
Include "ISRRX.bas"
Include "usart.bas"

Dim location As Byte
Dim msgb0 As Byte
Dim msgb1 As Byte

Dim w As Word

ADCON1 = $07 // PORTE as digital (GLCD)
SetAllDigital()
GLCD.Cls 
GLCD.SetFont(Fixed)       //no garbled

GLCD.WriteAt(5,5,"RESET 5 5")
GLCD.WriteAt(100,5,"RESET 100 5")
GLCD.WriteAt(5,100,"RESET 5 100")
GLCD.WriteAt(100,100,"RESET 100 100")
DelayMS(500)  
Cls

GLCD.Rectangle(0,0,190,63)  

   USART.SetBaudrate(br9600)
   ISRRX.Initialize()                 

While (true)
   // check for usart overrun. it'll stop data if not cleared
   If (ISRRX.Overrun) Then
      ISRRX.Reset()
      // maybe display a message.
   EndIf     

    // now, to receive a message from the PICAXE you must always read three bytes
   // get first byte (location index)
   While (ISRRX.DataAvailable = false)
   End While
   location = ISRRX.ReadByte()
   GLCD.WriteAt(10,5,location)

   // get second byte - lowbyte of parameter (LSB)
   While (ISRRX.DataAvailable = false)
   End While
   msgb0 = ISRRX.ReadByte()
   GLCD.WriteAt(60,5,msgb0)

   // get third byte - highbyte of parameter (MSB)
   While (ISRRX.DataAvailable = false)
   End While
   msgb1 = ISRRX.ReadByte()
    GLCD.WriteAt(110,5,msgb1)

   // display the two message bytes based on the location
   Select (location)
      Case 17, 18, 19, 20, 21, 22, 23, 24, 25, 26      // parameter is already ascii text
         GLCD.WriteAt(10,20, msgb0, msgb1)
      Else               // parameter is binary data...
         // write data as two bytes (LSB, MSB)
         GLCD.WriteAt(60, 20, DecToStr(msgb0), ", ", DecToStr(msgb1))

         // or write data as a single 16-bit value
         w.byte1 = msgb1
         w.byte0 = msgb0
         GLCD.WriteAt(110,20, DecToStr(w))
   End Select
End While

xva3rjy
Posts: 40
Joined: Sat Sep 27, 2014 11:41 am

Re: Help .. convert to characters

Post by xva3rjy » Tue Oct 14, 2014 4:11 pm

I'm included a photo of the GLCD.
Attachments
Photo of GLCD
Photo of GLCD
GLCD.JPG (85.9 KiB) Viewed 6159 times

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Help .. convert to characters

Post by Jerry Messina » Tue Oct 14, 2014 4:42 pm

The GLCD commands you added are creating the "chinese characters".

Most of the bytes that are coming over the serial port are pure binary data bytes, not printable ASCII characters.
The ONLY data that you can directly send to GLCD.WriteAt are the two parameter bytes for locations/messages 23, 24, 25, and 26.

EVERYTHING else has to be converted to a printable text string before you write it, and you do that using "dectostr(x)"

xva3rjy
Posts: 40
Joined: Sat Sep 27, 2014 11:41 am

Re: Help .. convert to characters

Post by xva3rjy » Tue Oct 14, 2014 5:26 pm

Then when are my PTT ERR characters and why aren't they displayed?

You can see that in my postings above they do show up in the terminal.

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Help .. convert to characters

Post by Jerry Messina » Tue Oct 14, 2014 8:52 pm

I don't know. Maybe they're getting overwritten as messages come in?

Perhaps try this to print out the three bytes when they come in (in hex)

Code: Select all

   // now, to receive a message from the PICAXE you must always read three bytes
   // get first byte (location index)
   while (ISRRX.DataAvailable = false)
   end while
   location = ISRRX.ReadByte()

   // get second byte - lowbyte of parameter (LSB)
   while (ISRRX.DataAvailable = false)
   end while
   msgb0 = ISRRX.ReadByte()

   // get third byte - highbyte of parameter (MSB)
   while (ISRRX.DataAvailable = false)
   end while
   msgb1 = ISRRX.ReadByte()

   GLCD.WriteAt(10, 10, "Hex Bytes: ", HexToStr(location), ", ", HexToStr(msgb0), ", ",HexToStr(msgb1))

xva3rjy
Posts: 40
Joined: Sat Sep 27, 2014 11:41 am

Re: Help .. convert to characters

Post by xva3rjy » Wed Oct 15, 2014 12:17 am

You lost me on what you're trying to show.
This was what was displayed on the GLCD..

Hex Bytes: 80, 0, 0

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Help .. convert to characters

Post by Jerry Messina » Wed Oct 15, 2014 9:46 am

That code receives three bytes and then displays them all in hex on the display. That will show a "complete message" from the PICAXE no matter what it is, in a fixed format.

One thing to keep in mind is that unless you put them at different locations on the display, new messages will overwrite old messages as they're displayed. If they don't come in very slowly you'll not see them all.

The message $80, $00, $00 isn't among the ones you've listed so far. Is it a valid one?

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Help .. convert to characters

Post by bitfogav » Wed Oct 15, 2014 1:33 pm

Like Jerry has pointed out why don't you try and list all the different locations on the display?.. That way you will see what is going on..
You will have to set the X,Y locations to suit your GLCD display size..

Example:

Code: Select all

   // display the two message bytes based on the location
   Select (location)
      Case 17
         GLCD.WriteAt(5,2,location, msgb0, msgb1)
      Case 18
         GLCD.WriteAt(5,12,location, msgb0, msgb1)
      Case 19
         GLCD.WriteAt(5,22,location, msgb0, msgb1)
      Case 20
         GLCD.WriteAt(5,32,location, msgb0, msgb1)
      Case 21
         GLCD.WriteAt(5,42,location, msgb0, msgb1)
      Case 22
         GLCD.WriteAt(50,2,location, msgb0, msgb1)
      Case 23
         GLCD.WriteAt(50,12,location, msgb0, msgb1)
      Case 24
         GLCD.WriteAt(50,22,location, msgb0, msgb1)
      Case 25
         GLCD.WriteAt(50,32,location, msgb0, msgb1)
      Case 26      
         GLCD.WriteAt(50,42,location, msgb0, msgb1)
   End Select
exampleglcd.jpg
exampleglcd.jpg (38.15 KiB) Viewed 6140 times

xva3rjy
Posts: 40
Joined: Sat Sep 27, 2014 11:41 am

Re: Help .. convert to characters

Post by xva3rjy » Wed Oct 15, 2014 9:07 pm

Ok ... still having problems... code and photo attached.

Code: Select all

Device = 18F2525
Clock =   40                              '  external crystal x 4

//Config OSC = HSPLL                       '   Config OSC = HSPLL

#Option GLCD_MODEL = KS0108
#option GLCD_CS_COUNT = 3
#Option GLCD_SCREEN_WIDTH = 192
#Option GLCD_SCREEN_HEIGHT = 64
#Option GLCD_DATA = PORTB
#Option GLCD_RS = PORTA.3
#Option GLCD_CS1 = PORTA.4
#Option GLCD_CS2 = PORTA.5
#Option GLCD_CS3 = PORTC.6
#Option GLCD_RW = PORTA.2
#Option GLCD_EN = PORTA.1
#Option GLCD_RST = PORTA.0
#Option GLCD_ASPECT_RATIO = 75
#Option GLCD_INIT_DELAY = 100               
#Option GLCD_INVERT_CS = False

Include "GLCD.bas"
Include "FixedFont.bas"
Include "Arial.bas"
Include "Garamond.bas"
Include "Tahoma.bas"
Include "Verdana.bas"
Include "Times.bas"
Include "DS1307.bas"
Include "convert.bas"
Include "utils.bas"
Include "graphics.bas"
Include "ks0108.bas"
Include "ISRRX.bas"
Include "usart.bas"

Dim location As Byte
Dim msgb0 As Byte
Dim msgb1 As Byte

Dim w As Word

ADCON1 = $07 // PORTE as digital (GLCD)
SetAllDigital()
GLCD.Cls
GLCD.SetFont(Fixed)       

GLCD.WriteAt(5,5,"RESET 5 5")
GLCD.WriteAt(100,5,"RESET 100 5")
GLCD.WriteAt(5,100,"RESET 5 100")
GLCD.WriteAt(100,100,"RESET 100 100")
DelayMS(500) 
Cls

GLCD.Rectangle(0,0,190,63) 

   USART.SetBaudrate(br9600)
   ISRRX.Initialize()    
     
While (true)
   // check for usart overrun. it'll stop data if not cleared
   If (ISRRX.Overrun) Then
      ISRRX.Reset()
      // maybe display a message.
   EndIf          
                
   // now, to receive a message from the PICAXE you must always read three bytes
   // get first byte (location index)
   While (ISRRX.DataAvailable = false)
   End While
   location = ISRRX.ReadByte()
   GLCD.WriteAt(5,2,location)

   // get second byte - lowbyte of parameter (LSB)
   While (ISRRX.DataAvailable = false)
   End While
   msgb0 = ISRRX.ReadByte()
   GLCD.WriteAt(25,2,msgb0)
   
   // get third byte - highbyte of parameter (MSB)
   While (USART.DataAvailable = false)
   End While
   msgb1 = USART.ReadByte()
   GLCD.WriteAt(45,2,msgb1)    

// display the two message bytes based on the location
   Select (location)
      Case 17
         GLCD.WriteAt(5,12,location, msgb0, msgb1)
      Case 18
         GLCD.WriteAt(25,12,location, msgb0, msgb1)
      Case 19
         GLCD.WriteAt(45,12,location, msgb0, msgb1)
      Case 20
         GLCD.WriteAt(5,22,location, msgb0, msgb1)
      Case 21
         GLCD.WriteAt(25,22,location, msgb0, msgb1)
      Case 22
         GLCD.WriteAt(45,22,location, msgb0, msgb1)
      Case 23
         GLCD.WriteAt(5,32,location, msgb0, msgb1)
      Case 24   
         GLCD.WriteAt(25,32,location, msgb0, msgb1)
      Case 25
         GLCD.WriteAt(45,32,location, msgb0, msgb1)
      Case 26     
         GLCD.WriteAt(5,42,location, msgb0, msgb1)
      Else               
         GLCD.WriteAt(25, 42, "hello")
   End Select
End While   
Attachments
GLCD photo
GLCD photo
GLCD2.JPG (48.91 KiB) Viewed 6133 times

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Help .. convert to characters

Post by bitfogav » Wed Oct 15, 2014 10:18 pm

It looks like the issue is with the first byte "location" lets try running the program but change the While Loop with the following code..
This should only run once to catch only the first 3 bytes from the picaxe, this should help debug.. update us with what is displayed on the GLCD.

Code: Select all

While (true)
   If (ISRRX.Overrun) Then
      ISRRX.Reset()
   EndIf          
                
   While (ISRRX.DataAvailable = false)
   End While
   location = ISRRX.ReadByte()

   While (ISRRX.DataAvailable = false)
   End While
   msgb0 = ISRRX.ReadByte()
 
   While (USART.DataAvailable = false)
   End While
   msgb1 = USART.ReadByte() 

   GLCD.WriteAt(5,2,location)
   GLCD.WriteAt(25,2,msgb0)
   GLCD.WriteAt(45,2,msgb1)

   while true
     ' the prog will stop here
   wend
End While   
Just for the topic it looks like the topic started at http://www.picaxeforum.co.uk/showthread ... on-18f2525

also the project is for VK4DD Ham Radio
and example code:
PA control pcb 20X2 v17.zip
(6.83 KiB) Downloaded 216 times

xva3rjy
Posts: 40
Joined: Sat Sep 27, 2014 11:41 am

Re: Help .. convert to characters

Post by xva3rjy » Thu Oct 16, 2014 12:47 am

Ok I ran it and am back at my original issue..garbled text...photo attached.
Attachments
GLCD3.JPG
GLCD3.JPG (48.07 KiB) Viewed 6126 times

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Help .. convert to characters

Post by bitfogav » Thu Oct 16, 2014 9:36 am

Are you sure that we have the correct usart baud freq?

The picaxe code as the following

Code: Select all

Baud_Setup = B19200_64 	'Display Baud (4800 baud, 64Mhz Processor Clock)
In your code you have

Code: Select all

USART.SetBaudrate(br9600)

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Help .. convert to characters

Post by Jerry Messina » Thu Oct 16, 2014 9:40 am

At some point I assume the correct baud rates were being used because he got printable characters. Again, the issue is that most everything coming from the PICAXE is binary data, and you can't just print the binary data. There are a few text messages, but even they have binary data mixed in with them.

bitfogav had a good suggestion. With any luck this should put the messages on individual lines. Set MAX_LINES to match your display.

Code: Select all

dim line as byte
dim line_spacing as byte
const MAX_LINES = 8				// max number of lines to print before wrapping

USART.SetBaudrate(br9600)
ISRRX.Initialize()   
     
line_spacing = GLCD.Font.Height + 2 	// calc Y increments
line = 1

While (true)
   // check for usart overrun. it'll stop data if not cleared
   If (ISRRX.Overrun) Then
      ISRRX.Reset()
      // maybe display a message.
   EndIf         
               
   // now, to receive a message from the PICAXE you must always read three bytes
   // get first byte (location index)
   While (ISRRX.DataAvailable = false)
   End While
   location = ISRRX.ReadByte()

   // get second byte - lowbyte of parameter (LSB)
   While (ISRRX.DataAvailable = false)
   End While
   msgb0 = ISRRX.ReadByte()
   
   // get third byte - highbyte of parameter (MSB)
   While (USART.DataAvailable = false)
   End While
   msgb1 = USART.ReadByte()


   // display the three byte message on a new line, along with the line number
   // the bytes will be displayed as hex data no matter which message type
   // in the format "01: ll, xx, yy"
   GLCD.WriteAt(1, line*line_spacing, dectostr(line, 2), ": ", HexToStr(location,2), ", ", HexToStr(msgb0,2), ", ",HexToStr(msgb1,2))

   line = line + 1

   if (line > MAX_LINES) then
   	  line = 1
   endif
End While   
edit: Thanks for posting that picaxe source. That helps clarify things (so long as the baudrate is correct).

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: Help .. convert to characters

Post by bitfogav » Thu Oct 16, 2014 10:22 am

Jerry Messina wrote:At some point I assume the correct baud rates were being used because he got printable characters.
I'm not so sure looking at all the topic posts, The only time he got printable characters to the GLCD was when you added HexToStr. the other times
it was just the raw data which makes me think that the baud is incorrect.

Code: Select all

   // now, to receive a message from the PICAXE you must always read three bytes
   // get first byte (location index)
   while (ISRRX.DataAvailable = false)
   end while
   location = ISRRX.ReadByte()

   // get second byte - lowbyte of parameter (LSB)
   while (ISRRX.DataAvailable = false)
   end while
   msgb0 = ISRRX.ReadByte()

   // get third byte - highbyte of parameter (MSB)
   while (ISRRX.DataAvailable = false)
   end while
   msgb1 = ISRRX.ReadByte()

   GLCD.WriteAt(10, 10, "Hex Bytes: ", HexToStr(location), ", ", HexToStr(msgb0), ", ",HexToStr(msgb1))  
Jerry Messina wrote:Thanks for posting that picaxe source. That helps clarify things (so long as the baudrate is correct).
That's no problem Jerry I thought the topic needed more information, good old google search :) also I found the schematics for the project
VK4DD GLCD Sch. This also shows that there might be a standby data line from the Picaxe which could be used to sync the main board to the GLCD (driver).

Post Reply