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 support@ccsinfo.com

How simplify my code?

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



Joined: 13 Feb 2012
Posts: 2

View user's profile Send private message

How simplify my code?
PostPosted: Mon Feb 13, 2012 4:21 pm     Reply with quote

Hello, i search to transform integer to a string and display it to my lcd (1x16).

I do this, it work, but i think it is not nice code.

my function:
Code:

int  tostring(int val,int ordre){

int unite,dizaine,centaine;

unite = val%10;
dizaine = (val/10)%10;
centaine = val/100;

if(ordre==1){return unite;}
if(ordre==2){return dizaine;}
if(ordre==3){return centaine;}

}

For display on my lcd:
Code:

lcd_putc( tostring(code_scan,3)+48 ); //+48 for optain ascii number code
lcd_putc( tostring(code_scan,2)+48 );
lcd_putc( tostring(code_scan,1)+48 );

How can I do more simply? Can I use pointer? How my function can return an array? Is it possible?


Tinks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 13, 2012 4:45 pm     Reply with quote

Use the sprintf() function. It's like printf(), except it puts the output into
an array. Make sure you set the size of the array large enough to hold
all the characters, and the string-terminator byte of 0x00.
See this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=34659

Or actually, I think you really just want to use the re-direction feature
of printf that CCS provides. The output of printf can be sent to lcd_putc
instead of the serial port. Here is a sample program:
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1
hiti



Joined: 13 Feb 2012
Posts: 2

View user's profile Send private message

PostPosted: Mon Feb 13, 2012 5:10 pm     Reply with quote

Thanks, I don't know that we can do this with printf.

I test it work well.

Thanks, it is exactly what I want!

Very Happy

Finally I do this:
Code:

int32 z = 0x07D8; //initial value of integer 2008

while(1){

printf(lcd_putc, "%lu", z);
z++;
delay_ms(1000);

}
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