How to use printf with Hi-Tech PIC C and 16F877?

Status
Not open for further replies.

ikarakaya

Junior Member level 3
Joined
Nov 2, 2001
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
139
I am using Keil C to program 8051. I can use printf to send data to serial I/O. Can i use it with HI-TECH PIC C and 16F877 ? And how ?

Thanks
 

Yes ,You can but you must define primitive printf function "putch" or "putchar".
 

Or simply use a sprintf function and copy the string to UART.
Actually, this way can be better, because you only have one function for "print" a string to some device and you can choose (with one additional parameter) where will go the output (UART, LCD, SPI, I2C etc.)
 
hi ...

hi .. i tried this with avr .. i found that on elektroda ;0)

int putchar(int c)
{
if (c == '\n')
putchar('\r');
while(!(USR&32)); //wait while tx buffer empty
UDR = (char)c; //send char to uart register
return c;
}

after you overload original putchar with this all printf features would work.

regards

cancel
 


And printf need the new() function.
Better is sprintf with a static array and is possible a "const" format string (into flash).

Gomez
 

use this code

#include <pic.h>
#include <stdio.h>
#include <string.h>

//------ init uart

void InitUART(void)
{
SPBRG = 12; /* 19200bPS @ 4MHz */
BRGH = 1; /* High Speed Mode */
TXEN = 1; /* Enable Transmit */
SPEN = 1; /* Enable Serial Port */
}

//------ write char to uart

void putch(unsigned char byte)
{
while(!TRMT); /* Wait for TX Buffer Empty */
TXREG = byte;
}

and then just use normal printf.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…