Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

how to direct printf function from USART0 to USART1 in codevision

Status
Not open for further replies.

tariq70

New Member
hi
I'm using ATMEGA128A and codevision .I want to use printf for both usarts but printf only works for one.
in stdio.h lib its only defined functions i can't see the codes using for printf function.
appreciate your help.
 
In standard C you can use a different stream with "fprintf", or send the formatted data to a character array using sprintf.

If you can define a named stream for the additional serial port, you should be able to use fprintf.
Otherwise, use sprintf to do the formatting then copy the char string to serial1, or whatever the second port is called.

More info on using two ports here:
 
I thought stdio just pointed to a stub, and it's up to you what you place there - presumably there's a stub that prints to a specific USART?.

PIC's use putch(), and the usual code is like this:

C:
/*****************************
Dependencies:   xc.h, stdio.h
Processor:      PIC16F1824
Complier:       XC8 v1.00 or higher
Result: Prints to the UART1 Window of the IDE
Debugger: Simulator
*****************************/
void putch(unsigned char byte)
{
    TXSTA=0x26;
    RCSTAbits.SPEN=1;
    TXREG=byte;
    while(!TXIF)continue;
    TXIF=0;
}


You could simply add extra code so it sends to both USART's, or add lines to select which ones you want to print to, or select an LCD instead, anything you want.
 
thank you everyone for your answers.

i found my answer in below codes and it's working:

unsigned char poutput;

void putchar(char c)
{
switch (poutput)
{
case USART0: // the output will be directed to USART0
while ((UCSR0A & DATA_REGISTER_EMPTY)==0);
UDR0=c;
break;

case USART1: // the output will be directed to USART1
while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
UDR1=c;
break;

};
}

this function must be defined before main function
so when i want to send data by printf through usart0 i do this:
poutput=USART0;
printf("USART0\r");

and through usart1 :
poutput=USART1;
printf("USART1\r");

this codes are working fine.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top