CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ASCII converting data

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

ASCII converting data
PostPosted: Sun Jun 10, 2012 12:30 pm     Reply with quote

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.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 10, 2012 1:35 pm     Reply with quote

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.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Jun 11, 2012 1:21 am     Reply with quote

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
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

re
PostPosted: Mon Jun 11, 2012 9:54 am     Reply with quote

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!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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