2013年10月5日星期六

printf() in IAR using DLIB

AVR forum - printf() in IAR using DLIB

harshthegreat89 - Sep 30, 2011 - 12:16 PM
Post subject: printf() in IAR using DLIB

Hi, 

I want to use printf() for printing on serial port in IAR compiler. 

The following code is running when CLIB is selected but doesn't work when DLIB is selected. 

Code:


#include
#include
#include

#define F_CPU 8000000UL  // Oscillator frequency set as per hardware to 8 Mhz.

#define BAUD_RATE 19200
#define BAUD_PRESCALER (((F_CPU/(BAUD_RATE*16UL))) - 1)


//This routine initializes the UART
void InitUart(void)
{
   //Set Baud rate
   UBRRH = (BAUD_PRESCALER >>8); //load upper 8 bits of baud rate value to high byte of UBRR register.
   UBRRL = (BAUD_PRESCALER);  //load lower 8 bits of baud rate value to low byte of UBRR register.
   
   
   //Enable UART receiver and transmitter
   UCSRB |= (1 << RXEN) | (1 << TXEN);

   //Set to 8 data bits. Currently selecting  1 stop bit option. If you want 2 bit, write 1 to USBS register.
   UCSRC |= (1 << UCSZ1) | (1 << UCSZ0);     
}

int putchar(int c)
{
     while ((UCSRA & (1<     return (UDR = c);
}

int main( void )
{
  InitUart();

  while(1)
  {
    printf("harsh");
  }