View previous topic :: View next topic |
Author |
Message |
anishpsla
Joined: 31 Dec 2014 Posts: 9
|
Display decimal in serial terminal |
Posted: Fri Jan 02, 2015 10:54 am |
|
|
I write a small program to test serial port functionality. The program is to display the numbers 0-30. But the output is in hex format. How can I display the number in decimal format ? My code is
Code: | #include <16f877a.h>
#use delay(crystal=16MHz)
#use rs232(baud=9600, UART1)
void main() {
int i;
do {
for(i = 0; i <= 30; ++i) {
printf("Count is %x \r\n", i);
delay_ms(1000);
}
} while (TRUE);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Jan 02, 2015 11:44 am |
|
|
Read the manual, or a book on C. Look at the entry for printf. Hint the 'x' in your format specifier comes from heXadecimal. Wonder what they might use for Decimal?. |
|
|
anishpsla
Joined: 31 Dec 2014 Posts: 9
|
|
Posted: Fri Jan 02, 2015 12:08 pm |
|
|
Thanks for the reply. I got the code %d will do the magic.
But how if I want to compare the values. For example, if the current analog value is 80% greater than of previous value then I want to ring a alarm. The ADC o/p also in hex format. How can I convert it for real world calculations ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Fri Jan 02, 2015 1:36 pm |
|
|
Internally, _all_ numbers are binary. Nothing else. You can decide to display such numbers as octal, binary, hexadecimal or decimal as you want. The numbers still remain the same. Similarly you can input numbers into maths operations in any format you want. For instance:
'c' 0x63 99 0b1100011 0143
Are all exactly the same number (if I've done the conversion right...).
You just compare them. You don't have to worry how they are stored. They are just numbers. The comparison has no idea of what format the value arrived in, or is going out in. |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Fri Jan 02, 2015 4:20 pm |
|
|
Although it does care if they are signed or unsigned ...
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
|