![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
ASCII converting data |
Posted: Sun Jun 10, 2012 12:30 pm |
|
|
Hi!
Here is my problem.
I need to write a code which ought to drive LCD display with SPI communication. The problem occurs when I have to print two or more digits value like: 168. To that display I need to send following data:
Code: |
spi_write(first character address)
spi_write(0x31)// ASCII_1==hex_1
spi_write(second character address) //shift cursor right, one poss.
spi_write(0x36)// ASCII_6==hex_6
spi_write(third character address) //shift cursor right
spi_write(0x36)// ASCII_8==hex_8
|
Now I got 168 on my display, but if want to increase one value it's almost impossible.
So, my genuine question is: how to convert (by most easier approach) 168(decimal) to 168(ASCII).
I know it's a stupid question from poor guy with bad English, but I will really appreciate every answer. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 10, 2012 1:35 pm |
|
|
Use the printf function's ability (in CCS) to redirect the character output
to a routine that accepts a byte as the parameter. So, if you want printf
to send characters out the SPI port, then do this:
Code: |
void spi_byte(int8 value)
{
spi_write(value);
}
//==========================================
void main()
{
.
.
.
printf(spi_byte, "168");
|
printf converts the number to 3 ASCII digits and sends them (one at a
time) to the spi_byte() routine, which transmits them with spi_write().
You can also put the number in a variable:
Code: |
int8 my_value;
my_value = 168;
printf(spi_byte, "%u", my_value);
|
This is in the CCS manual. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Mon Jun 11, 2012 1:21 am |
|
|
Just as a 'comment' though, it is perhaps worth understanding that at no point, do you have a 'decimal' number.
If (for instance), you code:
Code: |
int8 fred;
fred = 100;
|
The variable 'fred', contains the _binary_ representation of '100'. 0b01100100.
The compiler has converted your decimal number into binary (which the chip needs), for you.
Printf, allows you to reformat this internal binary data to a variety of output formats, including ASCII.
Given the need to send register numbers between the digits (is this _really_ needed? - most chips automatically advance to the _next_ register when they receive a value, so I'd expect to be able to address one register, and then write the three characters in sequence - perhaps LS digit first?), I'd look possibly at 'sprintf', which puts the digits into a character array.
Code: |
int8 ival;
char buffer[4]; //One larger than the number
ival=168;
sprintf(buffer,"%03u",ival);
//buffer now contains '1', '6', '8' as required
ival=34;
sprintf(buffer,"%03u",ival);
//buffer now contains '0', '3', '4'
|
So you can just output buffer[0], buffer[1], and buffer[2] for the required digits. Note the important '03' in the format. This makes the code format the number with leading zeros. Needed with either this approach, or using printf, so you know where each digit is in the string.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
re |
Posted: Mon Jun 11, 2012 9:54 am |
|
|
Thank you so much Guys for the detail information! I know that I'm actually using different formats but the processor works only with binary info. My point was to defragment binary digit 10101000(168 in decimal) to three separate characters, which I successfully did, thanks to you! |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|