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 CCS Technical Support

Display decimal in serial terminal

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



Joined: 31 Dec 2014
Posts: 9

View user's profile Send private message

Display decimal in serial terminal
PostPosted: Fri Jan 02, 2015 10:54 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 02, 2015 11:44 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 02, 2015 12:08 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 02, 2015 1:36 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 02, 2015 4:20 pm     Reply with quote

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
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