# Sending integers via PIC's USART

I need to send integers from my micro controller board (PIC16F877) to the PC. I'm using MAX232 for this. I need to use a code similar to this (coded using mikroc 8.2).

Please note that this is not correct (actually the line Usart_Write(count); will not work as intended). But it'll show what I'm trying to do.

#define BAUD_RATE 57600
#define DELAY 500
int count;

void interrupt(){
if(INTCON.INTF == 1 ){
count++;
INTCON.INTF = 0;
}
}

void setup(){
INTCON = 0x90;
TRISB = 0x01;
Usart_Init(BAUD_RATE);
count = 0;
}

void main() {
setup();
while(1){
Usart_Write('>');
Usart_Write(count);
Usart_Write('<');
count = 0;
delay_ms(DELAY);
}
}


The application is simple. The variable counter is incremented with PORTB0 interrupt and counter value is needed to be send to PC.

Can someone show me a correct (and simple) way to do this..

• What specifically doesn't work in the above code? (which bit are you having trouble with?) Apart from setting count to 0 in the main loop (which you shouldn't do if you want it to increment) it doesn't look too bad. – Oli Glaser Oct 4 '12 at 10:35
• Shouldn't this be at stackoverflow? And, I think you should make an effort in actually trying first, then post the problem at stackexchange – chwi Oct 4 '12 at 13:45
• @OliGlaser this is to compute the speed of a rotating wheel using a digital counter. The interrupt will increment count whenever it gets a signal. But I read the count value at fixed intervals and send the values to the PC. Then ,of course, i must reset the counter. There may be a better way of achieving this, but this method is not wrong. The issue is that I dunno how to send the integer value through USART... – Anubis Oct 5 '12 at 3:59
• If you mean how to represent it as ASCII values so it's "human readable" on the PC, then Toby's answer is the way to go. There are quite a few useful functions in the standard library that help with things like this - have a read of the documentation of your library to see what print functions are available. – Oli Glaser Oct 5 '12 at 6:41

As I understand it, your problem is how to print a sequence of ASCII characters representing an integer.

You already have a function which can print a char, so you want something like the following:

char buf[16];
char *p;

sprintf(buf, "%d", count);
p = buf;
while(*p)
Usart_Write(*p++);


If you don't have sprintf() in your C library, try itoa().

(Note for the pedants, snprintf() is safer, but not universally supported)

Whenever you need to check if something is working or not, try focusing on its specific parts. Like in this code you wont miss an increased value of count:-

void main() {
setup();
while(1){
if(count)
{
Usart_Write('>');
Usart_Write('<');
count = 0;
}
delay_ms(DELAY);
}
}

• Agree.. (but not an answer, eh?) – Anubis Oct 5 '12 at 4:02

Why dont you send bytes of integer?

char *p =(char*)&count;

send(*p);p++;
send(*p);p++;
send(*p);p++;
send(*p);


and on the other side you can collect bytes and form an integer, this is probably faster then converting to char array.

Regards, Luka

• Be aware that not all systems have a 32-bit int, not all systems allow casting (int *) to (char *) due to alignment and the micro may have a different endianness to the PC. – Toby Jaffey Oct 5 '12 at 9:34
• Yes, that is what you have to take into account, but you can always know how big the INT on embedded device is or you can write macro for diferent INTs. About endians on PC and PIC you can always change them faster then cast to char array. – Luka Pivk Oct 5 '12 at 11:27