Created
December 24, 2012 01:15
-
-
Save ricardas/4367002 to your computer and use it in GitHub Desktop.
Atmega16: USART
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// chip: atmega16 | |
#ifndef F_CPU | |
#define F_CPU 16000000 | |
#endif | |
#include <avr/io.h> | |
#include <util/delay.h> | |
#define BAUD 9600 | |
#define MYUBRR F_CPU/16/BAUD-1 | |
void USART_init(unsigned int ubrr) { | |
UBRRH = (unsigned char)(ubrr >> 8); | |
UBRRL = (unsigned char)ubrr; | |
UCSRB = 1 << RXEN | 1 << TXEN; | |
UCSRC = 1 << URSEL | 1 << USBS | 3 << UCSZ0; | |
} | |
void USART_transmit( unsigned char data ) { | |
while(!(UCSRA & (1 << UDRE))); | |
UDR = data; | |
} | |
void USART_transmit_string(const char *data) { | |
unsigned char c; | |
while(( c = *data++ )) { | |
USART_transmit(c); | |
_delay_ms(10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#ifndef F_CPU
#define F_CPU 1843200
#endif
#include <avr/io.h>
#include <util/delay.h>
#define BAUD 9600
//#define MYUBRR F_CPU/16/BAUD-1
#define MYUBRR ((F_CPU) / (16 * BAUD)) - 1
//#define MYUBRR (F_CPU) / (16 * BAUD) - 1
//#define MYUBRR F_CPU/16/BAUD-1
// Function to initialize USART0 for 9600 baud rate
void USART0_Init(unsigned int ubrr)
{
// Set baud rate
UBRR0H = (unsigned char)(ubrr >> 8);
UBRR0L = (unsigned char)ubrr;
}
// Function to transmit a character over USART0
void USART0_Transmit(unsigned char data)
{
// Wait for the transmit buffer to be empty
while (!(UCSR0A & (1 << UDRE0)));
}
// Function to transmit a string over USART0
void USART0_TransmitString(const char* str)
{
while (*str)
{
USART0_Transmit(*str);
_delay_ms(10);
str++;
}
}
/* Function to clear the USART0 transmit buffer
void USART0_ClearTransmitBuffer()
{
while (!(UCSR0A & (1 << UDRE0)))
{
// Read the UDR0 register to clear the buffer
volatile unsigned char dummy = UDR0;
}
}
*/
int main()
{
// Initialize USART0 with a baud rate of 9600
USART0_Init(MYUBRR);
}
this is my code i want to print string on a terminal, but i am getting this o/p
i am getting string along with the some gv values
<00><00><00><00><J€H@�� <00>�a�ƒ�<00><00>����<00>à<00>„À 0!<00><00<00>Hello, World