Help .. convert to characters

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

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

Help .. convert to characters

Post by xva3rjy » Wed Oct 08, 2014 3:05 pm

I'm trying to display the same test on the terminal and GLCD, however am only getting numbers.

Code: Select all

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

//Config OSC = HSPLL                       '   Config OSC = HSPLL 

#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_INIT_DELAY = 500               
//#Option GLCD_INVERT_CS = False

#Option GLCD_MODEL = KS0108

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 i As Byte


ADCON1 = $07 // PORTE as digital (GLCD)
SetAllDigital()
GLCD.Cls   
GLCD.SetFont(Fixed)
GLCD.WriteAt(4,2,"RESET")
DelayMS(500)  
Cls   
                    
SetBaudrate(br9600)
// read in characters and echo to screen...
Repeat

While ISRRX.DataAvailable
   i = ISRRX.ReadByte()
    USART.WriteByte(i)
    GLCD.WriteAt(10,10,DecToStr(i) + " TEST")
    DelayMS(200) 
   
Wend
Until 1=0
The hyper terminal displays "ERRPTT ERRPTT ERRPTT.....]
The GLCD displays [230 TEST 241 TEST 137 TEST 97 TEST 43 TEST .....]

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Help .. convert to characters

Post by Coccoliso » Wed Oct 08, 2014 6:55 pm

Hello,
the easiest way is to use the pseudo-typecast or once you have a variable of type Byte use .AsChar for convert it in char.
In your example..

Code: Select all

Dim i As Byte

ADCON1 = $07 // PORTE as digital (GLCD)
SetAllDigital()
GLCD.Cls   
GLCD.SetFont(Fixed)
GLCD.WriteAt(4,2,"RESET")
DelayMS(500)  
Cls   
                    
SetBaudrate(br9600)
// read in characters and echo to screen...
Repeat

While ISRRX.DataAvailable
   i = ISRRX.ReadByte()
    USART.WriteByte(i)
    GLCD.WriteAt(10,10, i.asChar + " TEST")  // Pseudo typecast to Char 
    DelayMS(200) 
   
Wend
Until 1=0

DecToStr() convert the contents of the variable, then the ascii code of the byte, in a printable string.
If you receive an "A" is displayed "65" on the GLCD.

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

Re: Help .. convert to characters

Post by xva3rjy » Thu Oct 09, 2014 2:18 pm

The suggestion regarding using "asChar" ie... GLCD.WriteAt(10,10, i.asChar + " TEST") did not work. The fitst characters shown are garbled "TEST" is ok.

Still looking for assistance..hyperterm shows PTT ERR PTT ERR etc..
GLCD.WriteAt(10,10, DecToStr(i) + "TEST") still showing numbers and TEST

The code I was given is sending out the following serial data from a PICAXE 20x2
hserout 0,(23,"PT")
hserout 0,(24,"T ")
hserout 0,(25," E")
hserout 0,(26,"RR") 'Send "PTT ERR" Message to Display
pause 1500

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

Re: Help .. convert to characters

Post by Jerry Messina » Thu Oct 09, 2014 3:21 pm

You're already getting the 8-bit ASCII character code in the byte 'i' when you read the uart, so all you should need to do is

Code: Select all

GLCD.WriteAt(10,10, i, " TEST")

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

Re: Help .. convert to characters

Post by xva3rjy » Thu Oct 09, 2014 4:11 pm

I still get a single mostly (95%) alternating garbled text with "TEST".

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

Re: Help .. convert to characters

Post by Jerry Messina » Thu Oct 09, 2014 4:56 pm

on the PICAXE side, what does hserout 0,(23,"PT") send?
If it's sending a byte = 23 first, then that's not a printable character and you'll have to deal with that. Most likely hyperterm is just ignoring it.

Also, a loop like you've shown

Code: Select all

While ISRRX.DataAvailable
   i = ISRRX.ReadByte()
    USART.WriteByte(i)
    GLCD.WriteAt(10,10,.......)
    DelayMS(200)
Wend
is likely going to end up missing all sorts of characters as they're sent from the PICAXE. The 200ms delay will cause you to miss receiving all the other characters unless the sending thing is slow as molasses.

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

Re: Help .. convert to characters

Post by xva3rjy » Thu Oct 09, 2014 8:56 pm

I'm getting nowhere my problem. How did the previous developer display the data on the GLCD. What was the purpose of the numbers, 23, 24,25 etc below?

hserout 0,(17,Statusb0,Statusb1)
hserout 0,(23,"RS")
hserout 0,(24,"T ")
hserout 0,(25," E")
hserout 0,(26,"RR")

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Help .. convert to characters

Post by Coccoliso » Thu Oct 09, 2014 9:06 pm

xva3rjy,
I had already asked but you have not answered me (http://sfcompiler.co.uk/phpBB3/viewtopi ... 886#p10313) you're using a MAX232 between MPU and PICAXE?
Explain to us what is the source that uses (a PC terminal program o else ) and what interface you put between it and the PIC. In the post I am referring the source was a picaxe and you have not specified how you've connected it to the PIC. Have you tried to connect with a terminal application from the PC to the PIC? If you did you have to put a RS232 / TTL converter that inverts the signal.

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

Re: Help .. convert to characters

Post by Jerry Messina » Thu Oct 09, 2014 10:39 pm

How did the previous developer display the data on the GLCD. What was the purpose of the numbers, 23, 24,25 etc below?
You want US to answer that?

I'm glad to help but I have NO idea what the previous developer did, nor how the PICAXE works.

If I had to guess, I'd say that the numbers are some sort of custom protocol somebody dreamed up to transfer the data.
Do you have any documentation of the program on the PICAXE side?

User avatar
Coccoliso
Posts: 152
Joined: Mon Feb 17, 2014 10:34 am

Re: Help .. convert to characters

Post by Coccoliso » Thu Oct 09, 2014 11:02 pm

xva3rjy wrote: GLCD.WriteAt(10,10, DecToStr(i) + "TEST") still showing numbers and TEST

The code I was given is sending out the following serial data from a PICAXE 20x2
hserout 0,(23,"PT")
hserout 0,(24,"T ")
hserout 0,(25," E")
hserout 0,(26,"RR") 'Send "PTT ERR" Message to Display
pause 1500
The characters were saying to see "231", "240" are exactly the 1's complement of those in SEROUT the picaxe.
But you're connected directly PIC TX to PICAXE RX and PIC RX to PICAXE TX ?
What characters do you hope to find ? You cannot connect directly an RS232 to TX and RX pins of the PIC for opposite polarities.
And if you have connected the PC directly IS RS232 LEVEL (+12V/-12V) and PIC IS CMOS/TTL LEVEL(0/5V)
Attachments
rs232-ttl.png
rs232-ttl.png (18.78 KiB) Viewed 4224 times

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

Re: Help .. convert to characters

Post by xva3rjy » Fri Oct 10, 2014 8:37 am

The circuits are an exact copy of a known working design. I have the original code on the PICAXE side and can see what is being sent out via the serial connection. The original developer is no longer around and I need to duplicate as best I can the code on the 18f2525 and sent the text to the GLCD. All I really need is some help (this is my first coding project ever) to display that text.

I am assuming that the number is used to store the data which follows into memory, and another routine constantly checks/updates the the text on the GLCD depending on what is there. You folks are the experts. Some surely knows how to do this.


hserout 0,(23,"PT")
hserout 0,(24,"T ")

At a high level ..
store "PT" in location 23
store "T " in location 24
display what is inlocation 23 on GLCD
display what is in location 24 on GLCD
etc

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

Re: Help .. convert to characters

Post by Jerry Messina » Fri Oct 10, 2014 9:48 am

Ok then, from your other thread you showed messages like

Code: Select all

hserout 0,(17,Statusb0,Statusb1)
hserout 0,(23,"PT")
hserout 0,(24,"T ")
hserout 0,(25," E")
hserout 0,(26,"RR") 'Send "PTT  ERR" Message to Display
hserout 0,(17,Statusb0,Statusb1)
hserout 0,(18,FWDPWRb0,FWDPWRb1)
hserout 0,(19,REVPWRb0,REVPWRb1)
hserout 0,(20,TEMPb0,Tempb1)
hserout 0,(21,VOLTAGEb0,VOLTAGEb1)
hserout 0,(22,CURRENTb0,CURRENTb1)
So it looks like all the packets from the picaxe are three bytes in length: a "location" number and two bytes of data. Do you only want to display the text message in locations 23-26? Is it limited to always 8 characters? What do you want to do with the others? Anything?

Also, previously I mentioned about dropping characters. I didn't notice that you had switched to using ISRRX instead of just the plain USART, so that should help a lot. Sorry.

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

Re: Help .. convert to characters

Post by xva3rjy » Fri Oct 10, 2014 10:11 am

I want to display all the locations on the GLCD. I will decide later where what size, font etc.

There are still others locations/data.

hserout 0,(17,Statusb0,Statusb1)
hserout 0,(18,FWDPWRb0,FWDPWRb1)
hserout 0,(19,REVPWRb0,REVPWRb1)
hserout 0,(20,TEMPb0,Tempb1)
hserout 0,(21,VOLTAGEb0,VOLTAGEb1)
hserout 0,(22,CURRENTb0,CURRENTb1)

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

Re: Help .. convert to characters

Post by Jerry Messina » Fri Oct 10, 2014 8:55 pm

Here's a simple example that should read and display an individual message. I've made some assumptions as to the order of the data being sent, but you should get the idea.

Code: Select all

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

dim w as word

while (true)
	// check for usart overrun. it'll stop data if not cleared
	if (usart.Overrun) then
		usart.ClearOverrun()
	endif		

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

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

	// get third byte - highbyte of parameter (MSB)
	while (usart.DataAvailable = false)
	end while
	msgb1 = usart.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
If you want to collect up all 8 bytes of the text message "PTT ERR" one way to do this is to get all four of the messages (23-26), save the characters each time, and then write them to the display when you get message #26. You could do something like this (adding the code to the select/case above)

Code: Select all

dim s as string

// start out with an empty string
s = ""

// each time you get msg #23-26 append the two chars to the string
s = s + msgb0.aschar
s = s + msgb1.aschar

// when you have them all, then display the string
if (location = 26) then
    GLCD.WriteAt(10, 10, s)
    // clear out string for next time
    s = ""
endif

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

Re: Help .. convert to characters

Post by xva3rjy » Tue Oct 14, 2014 9:44 am

There is still a issue with conversion .. characters come in garbled.
Below is the output of the terminal with 2 different settings simulating what should be received by the pic.

*Display data as decimal
017 005 002
023 080 084
024 084 032
025 032 069
026 082 082
017 005 002

*Display data as ASCII / Display non-ASCII as HEX
[11][05][02]
[17]PT
[18]T
[19] E
[1A]RR
[11][05][02]

Post Reply