#define F_CPU 16000000 // needed for delay.h
#include <avr/io.h>
#include <util/delay.h> // needed for _delay_ms() function
void USART_Transmit( unsigned char data );
void USART_Init();
int main(void)
{
unsigned char count = 0;
/* setup() */
DDRB = 0x20; // PB5 = Arduino pin 13
USART_Init(); // set up the USART for 115200 baud
/* loop(); */
while(1)
{
PORTB = PORTB ^ 0x20; // toggle PORTB bit 5
count ++; // increment count
if(count > 9) // keep count < 10
count = 0;
USART_Transmit( (count + 48) ); // convert count to ASCII and transmit it
_delay_ms(500);
}
}
/* These functions are copied from the ATmega328P user Manual (20.5, 20.6.1) */
void USART_Init()
{
/* Set baud rate */
UBRR0H = 0;
UBRR0L = 16; // UBRR0 = (F_CPU/( 8 * 115200 )) - 1 = 16.36
/* set double speed mode */
UCSR0A |= (1<<U2X0);
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
}