arduino pure programming part7--printf implementation


#include <avr/io.h>
#include <avr/interrupt.h>

/*
UDR-->UART Data Register
UCSRA,B,C---> Uart control status register a,b,c
UBRRnL,UBRRnH --> UART Baud Rate Register

*/

void print_init(unsigned int ubrr)
{
//ubrrn = fosc/16(Baud) -1.for 9600 = 104
//set baud rate
UBRR0H = (unsigned char)ubrr>>8;
UBRR0L = (unsigned char)ubrr;

//enable the receiver and transmitter and receiver enable interrupt

UCSR0B = (1<<RXEN0) |(1<<TXEN0) ;

UCSR0C = (1<<UCSZ01) | (1<<UCSZ00); //8-bit
}

void putch(unsigned char ch)
{
while(!(UCSR0A&&1<<UDRE0));
UDR0 = ch;
while(!(UCSR0A&&TXC0));
}

void print(char *string)
{
int i=0;
while(string[i])
{
putch(string[i]);
i++;
}
}

int main()
{
print_init(104);
//putch('c');
print("Hello World");
return 0;



}

To see the output use cutecom or minicom with baud rate set to 9600,8data bits , 1 stop bit and no parity

Comments

Popular posts from this blog

bb.utils.contains yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig

PR, PN and PV Variable in Yocto