328P serial printf hello_world?

Hey
Is there a simple howto or example how to get the printf to serial output to work?

I cant see the printf’s in my minicom.

TIA Micke

Hello,

The implementation of printf that we provide with the AVR library is for writing to the LCD. It is documented here. Was that the printf you were trying to use?

In any case, if you expect to get help on this issue, you should at least say what board you are using and how it is connected to your PC.

-Paul

Hey, sorry for the unclear question :slight_smile:

I’m running this pololu.com/catalog/product/1220
I’m trying to use printf to print debug on the serial terminal using this pololu.com/catalog/product/740 i put the small jumper to U.
but i don’t get it to work :-\ , I have no LCD connected thats why i want to have it on serial.

Br Micke

Well, the only printf we provide prints to an LCD. You can use our Serial library to access the serial port on an Orangutan. If you really need the features of printf(), you could consider using sprintf() instead to fill a buffer, then printing it with a serial library function.

-Paul

You can also look at the source code for the library and look at the lcd_init() function. That function connects the LCD to the output of printf using a function pointer. So you could do the analogous thing with the UART and then printf output will go to the UART.

-David

I used the serial port on the Pololu USB AVR Programmer to do my version of “hello”. I’m VERY old school, so it’s “Hello, sailor”. It prints the string to the serial, which tests communication, then echos everything received. I tested it on an ATMega48, so it should be EXACTLY the same for you. The samples are NOT the same.

I did it with NO special support libraries, just the chip on my breadboard, a little voltage regulator circuit, and my Pololu USB AVR Programmer. I used WinAVR-20090313 for my compiler support. The WinAVR <util/setbaud.h> is a little strange, but the code that I started with and reference set the baud the hard way without setbaud.h. If you don’t have setbaud.h, see the second code example and text. It is a very good explanation and shows examples.

As they say “Google is your friend”. The web sites that I used as sources are listed in the comments.

It isn’t perfect - the serial port doesn’t run at the correct specified rate and I don’t know why. Still, I got my support routines written and tested, then went on to use them to find the otherwise unfindable problem in my other code. In a little while, strange behavior of months duration was found and fixed.

/*
 echo.c - Serial communications test - defines ports and echos the characters
    that it receives on serial port 0.

 Serial communications test sample came from this web site:
   http://winavr.scienceprog.com/avr-gcc-tutorial/avr-usart-explained.html
  with more info found here:
   http://winavr.scienceprog.com/avr-gcc-tutorial/programing-avr-usart-module.html

   Sample needed specific usart number added, so UBRRH became UBRR0H, etc.
 JED 7 March 2010

   On ATMega48/88/168/328, PD1 = TXD (USART Output Pin),
       PD0 = RXD (USART Input Pin)

  For some reason, my computer sees these at 4800 bps even if I try to
  specify BAUD 9600 or 19200
 */

#include <stdint.h>
#include <avr/io.h>
// Define baud rate (assume F_CPU set by makefile)
#define BAUD 4800
// #define BAUD 9600
// #define BAUD 19200
#define BAUD_TOL 2   // defaults to 2%, warns if F_CPU/BAUD don't work together
#include <util/setbaud.h>

void USART_vInit(void) {
    // Set baud rate, high and low
    UBRR0H = UBRRH_VALUE;    // defined by setbaud.h
    UBRR0L = UBRRL_VALUE;    // defined by setbaud.h
    // Set frame format to 8 data bits, no parity, 1 stop bit
    UCSR0C = (0<<USBS0)|(1<<UCSZ01)|(1<<UCSZ00);
    // Enable receiver and transmitter
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    }

void USART_vSendByte(uint8_t u8Data) {
// Wait if a byte is being transmitted
    while((UCSR0A&(1<<UDRE0)) == 0);
    UDR0 = u8Data;  // Transmit data
    }

uint8_t USART_vReceiveByte() {
    // Wait until a byte has been received
    while((UCSR0A&(1<<RXC0)) == 0);
    return UDR0;    // Return received data
    }

void USART_vSendString(char u8string[]) {
    short i;
    for (i=0;u8string[i]!=0x00;i++)
        {
        USART_vSendByte(u8string[i]);
        }
    }

int main(void) {
    uint8_t u8Data;
    USART_vInit();    // Initialise USART
//    USART_vSendByte('A'); // Send string
    USART_vSendString("\n\rHello, sailor!\n\r");     // Send starup string

    for(;;) {    // Repeat indefinitely, receive then echo each char
        u8Data = USART_vReceiveByte();
        USART_vSendByte(u8Data);
        }
    }

Cool!! Thank you very much, ill test it asap!