Binary to ASCII Conversion
MississaugaGTS
Posts: 3
Hello All!
I have program and circuit set up to do the following......
1.·· read several analog signals
2.·· save the values to an EEPROM via I2C
Then after the process has completed.....
3.·· read the values from EEPROM via I2C
4.·· print the values to an LCD module
All of my code works as it should, but, when I save data (eg. 1234)·to EEPROM, it saves as...
Address 0 = 00001100 (12)
Address 1 = 00010010 (34)
Then when I send this data to the LCD I need the ASCII equivelent.....
00110001 (1)
00110010 (2)
00110011 (3)
00110100 (4)
How do I convert from Binary to ASCII with the BS2?
·
I have program and circuit set up to do the following......
1.·· read several analog signals
2.·· save the values to an EEPROM via I2C
Then after the process has completed.....
3.·· read the values from EEPROM via I2C
4.·· print the values to an LCD module
All of my code works as it should, but, when I save data (eg. 1234)·to EEPROM, it saves as...
Address 0 = 00001100 (12)
Address 1 = 00010010 (34)
Then when I send this data to the LCD I need the ASCII equivelent.....
00110001 (1)
00110010 (2)
00110011 (3)
00110100 (4)
How do I convert from Binary to ASCII with the BS2?
·
Comments
Save_Value:
· eeAddr = rdgNum * 2
· I2COUT SDA, SlaveAddr, eeAddr.BYTE1\eeAddr.BYTE0, [noparse][[/noparse]rdgValue.BYTE0, rdgValue.BYTE1]
· RETURN
Read_Value:
· eeAddr = rdgNum * 2
· I2CIN SDA, SlaveAddr, eeAddr.BYTE1\eeAddr.BYTE0, [noparse][[/noparse]rdgValue.BYTE0, rdgValue.BYTE1]
· RETURN
You would put the reading number in "rdgNum" and the value for that reading id "rdgVal."· Note that these subroutines assume that you are using a device with more than 256 locations, and the storage is standard "Little Endian" (low-byte, then high-byte).
Conversion from binary to ASCII is easy if you're using a serial LCD, simply use the BIN8 modifier.· If you need to do it manually, you can do it like this:
Print_BIN8:
· FOR idx = 7 TO 0
··· char = "0" + myValue.LOWBIT(idx)
··· GOSUB LCD_Write
· NEXT
· RETURN
This loops through eigh bits of a byte and converts them to the character "0" or "1"
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
I am working with the BS2, so unfortunately, I do not have the I2C instructions at my disposal. Also the LCD module that I am using is a Truly MTC-C162DPRN-2N (16x2).
This module uses 8 or 4 bit parallel communication....I have it configured for 8 bit and I·dedicated a 74HC595 shift register to the module to use as few I/O of the Stamp as possible.
I was thinking about it and I believe this code might make for simple conversion.
'****************************************
rawdata······ VAR·· byte
newval······· VAR·· byte
outdata······ VAR·· byte
Main:
· GOSUB Read_Mem··········· 'Reads a memory location and assigns the value to rawdata
· newval = rawdata
· DO WHILE newval > 0
··· outdata = newval // 10················ 'Gets remainder
··· outdata = outdata + %00110000··· 'Adds 30 to convert BIN to ASCII
··· newval = newval / 10
··· GOSUB Write_LCD
· LOOP
END
Of course this will only work for numbers and not characters.
what do you think?
Print_Dec:
· LOOKDOWN newVal, <=[noparse][[/noparse]9, 99, 999, 9999, $FFFF], width
· FOR idx = width TO 0
··· outData = (newVal DIG idx) + "0"
··· GOSUB Write_LCD
· NEXT
This uses a trick with LOOKDOWN to determine how many digits are in your value and print value correctly, left to right.· You can define "width" and "idx" as Nibs.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
I will try using the LOOKDOWN instruction as you suggested.
Thanks again for your help!
Jeff C