Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

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
 

cb30

Member level 2
Joined
Feb 21, 2002
Messages
53
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
335
Yes ,You can but you must define primitive printf function "putch" or "putchar".
 

CADDevil

Member level 5
Joined
Jun 26, 2001
Messages
80
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
649
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.)
 

cancel

Full Member level 5
Joined
May 27, 2001
Messages
251
Helped
15
Reputation
30
Reaction score
4
Trophy points
1,298
Activity points
1,878
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
 

Gomez

Full Member level 2
Joined
Feb 12, 2002
Messages
125
Helped
10
Reputation
20
Reaction score
3
Trophy points
1,298
Activity points
1,006
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
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top