|
|
View previous topic :: View next topic |
Author |
Message |
Henk
Joined: 22 Mar 2004 Posts: 6 Location: JOHANNESBURG, SOUTH AFRICA
|
9-Digit number to LCD |
Posted: Sun Nov 20, 2005 6:18 am |
|
|
Hi Guys.
I need to diplay a 9-digit number on a 2x16 lcd display, and wrote the following code to do this. It works fine accept at the point where the 5th digit changes from a 6 to a 7, or is greater than 7 to start with.
e.g. value is 999969999
when incremented by one, should be 999970000.
I instead get 99997}"�7. i.e. the last 5 digits are garbage.
here's the routine:
void DisplayHundMillions(x,y,int32 value){
int HM,TM,M,HT,ToT,T,H,tens,units;
int HMASCII, TMASCII,MASCII,HTASCII,ToTASCII,TASCII,HASCII,tensASCII,unitsASCII;
HM=value/100000000;
TM= (value-(HM*100000000))/10000000;
M= (value-(HM*100000000)-(TM*10000000))/1000000;
HT= (value-(HM*100000000)-(TM*10000000)- (M*1000000))/100000;
ToT= (value-(HM*100000000)-(TM*10000000)-(M*1000000)-(HT*100000))/10000;
T= (value-(HM*100000000)-(TM*10000000)-(M*1000000)-(HT*100000)-(ToT*10000))/1000;
H= (value-(HM*100000000)-(TM*10000000)-(M*1000000)-(HT*100000)-(ToT*10000)-(T*1000))/100;
tens= (value-(HM*100000000)-(TM*10000000)-(M*1000000)-(HT*100000)-(ToT*10000)-(T*1000)-(H*100))/10;
units=(value-(HM*100000000)-(TM*10000000)-(M*1000000)-(HT*100000)-(ToT*10000)-(T*1000)-(H*100)-(tens*10));
HMASCII=HM+48;
TMASCII=TM+48;
MASCII=M+48;
HTASCII=HT+48;
ToTASCII=ToT+48;
TASCII=T+48;
HASCII=H+48;
tensASCII=tens+48;
unitsASCII=units+48;
Lcd_gotoxy(x,y);
Lcd_putc(HMASCII);
Lcd_putc(TMASCII);
Lcd_putc(MASCII);
Lcd_putc(HTASCII);
Lcd_putc(ToTASCII);
Lcd_putc(TASCII);
Lcd_putc(HASCII);
Lcd_putc(tensASCII);
Lcd_putc(unitsASCII);
}
Just can't find this bug.
Henk |
|
|
Ttelmah Guest
|
|
Posted: Sun Nov 20, 2005 7:58 am |
|
|
This seems a very hard way to do this. You could just printf the number:
Code: |
void DisplayHundMillions(x,y,int32 value){
printf(LCD_PUTC,"%09lu",value);
}
|
If you want the numeric values as well, you could use sprintf, and just extract these from the string.
There is also the 'mod' function, which also simplifies the arithmetic massively.
So:
Code: |
int HM,TM,M,HT,ToT,T,H,tens,units;
units = value%10;
value/=10;
tens=value%10;
value/=10;
//Carry on for all the digits...
|
A for why it fails, it has four faulty digits, not five. The reason is that the sum 'ToT*10000', is the first one, that has both values smaller than an int32. The 10000, will fit in an int16, as will the 'ToT' value (which is an int8). Hence the compiler, switches to doing this term in int16 arithmetic, not int32, and there is an overflow in the term. You can force int32 arithmetic to be used, by casting either value up to int32.
It'll solve a lot faster using the modulus function.
Best wishes |
|
|
Henk
Joined: 22 Mar 2004 Posts: 6 Location: JOHANNESBURG, SOUTH AFRICA
|
|
Posted: Sun Nov 20, 2005 10:19 am |
|
|
Thankyou very much. I thought pintf applied only to RS232.
It worked perfectly
Henk |
|
|
|
|
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
|