Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

ikarakaya

Junior Member level 3
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
 

CADDevil said:
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.)

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top