![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
hiti
Joined: 13 Feb 2012 Posts: 2
|
How simplify my code? |
Posted: Mon Feb 13, 2012 4:21 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 13, 2012 4:45 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
hiti
Joined: 13 Feb 2012 Posts: 2
|
|
Posted: Mon Feb 13, 2012 5:10 pm |
|
|
Thanks, I don't know that we can do this with printf.
I test it work well.
Thanks, it is exactly what I want!
Finally I do this:
Code: |
int32 z = 0x07D8; //initial value of integer 2008
while(1){
printf(lcd_putc, "%lu", z);
z++;
delay_ms(1000);
}
|
|
|
![](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
|