AVR Serial


/*
Bismillah Hir Rahmaanir Raheem.
AVR-GCC Serial.
ATMega88. Default 8MHz Internal clock.
Low Fuse = 0x62
High Fuse = 0xDF
On Bray's Terminal the Baud is set to 1200, although in program it is 9600. Else getting problem through AVR-CDC.
*/

#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <string.h>

#define BAUD 9600
#define MYUBRR (((F_CPU / (BAUD * 16UL))) - 1)
//#define MYUBRR 51

void USART_Init( unsigned int ubrr);
void USART_Transmit( unsigned char data );
void USART_Transmit_String( unsigned char *string );

uint16_t Read = 0;
char Result[6];

int main (void) {
USART_Init(MYUBRR);

for (;;){
Read = Read + 1;
memset(Result, 0, sizeof(Result));
sprintf(Result, "%d", Read);
USART_Transmit_String(Result);
USART_Transmit(13);
_delay_ms(500);
}

}

void USART_Init( unsigned int ubrr) {// usart. p. 177
UCSR0B |= (1 << RXEN0) | (1 << TXEN0);
UBRR0L = (unsigned char)ubrr;
UBRR0H = (unsigned char)(ubrr >> 8);
//UCSR0C = (0 << USBS0)|(3 << UCSZ00);
}

void USART_Transmit( unsigned char data )
{
while (( UCSR0A & (1 << UDRE0)) == 0 ){};
UDR0 = data;
}

void USART_Transmit_String( unsigned char *string ){
while (*string){
USART_Transmit(*string);
string++;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s