16x2 LCD, playing with numbers.

Kylobeetle

Member
Hello everyone again! nice to be here with you, I have a question I would like to know, I have used and found this piece of code for displaying numbers in the
LCD taking care of not get the actual ASCII value instead of the wanted number .

Code:
        ch1 = (number/1000)%10;
        ch2 = (number/100)%10;
        ch3 = (number/10)%10;
        ch4 = (number/1)%10;

  Lcd_Set_Cursor(2,1);
        Lcd_Print_String("Inside Main Loop");
        Lcd_Set_Cursor(1,1);
        Lcd_Print_String("Number: ");
        Lcd_Print_Char(ch1+'0');
        Lcd_Print_Char(ch2+'0');
        Lcd_Print_Char(ch3+'0');
        Lcd_Print_Char(ch4+'0');

This works, what im looking for its to know the meaning of this, what I have found is that its a common practice to add null value or 48 (which is the same) at the end of the "number" i want to display because if i dont do it, I would only get its ASCII value instead.. But about the "(number/1000)%10 ? What does that % there? its like the base im working with ? (base 10) ?




I just tried to analyze it by myself, but im not sure about my findings haha . thanks in advance!
 
Take a large number:

1234

1234 /1000 = 1... (Modulus not needed on first... If the number is five figures it will help)
1234 / 100 = 12... Modulus 10 = 2 ...
1234 /10 = 123... Modulus 10 = 3...

In this example the divide isn't necessary .. But some compliers struggle with modulus on a larger number.
Some times I divide by 1000 and then I remove the thousands.
 
This works, what im looking for its to know the meaning of this, what I have found is that its a common practice to add null value or 48 (which is the same) at the end of the "number"

You're not adding a 'null' value, you're adding the ASCII value of '0' which is 48 DEC - although personally I usually add 0x30, the same value in HEX.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…