-
Notifications
You must be signed in to change notification settings - Fork 1
/
USART_Int_atmega328.c
72 lines (54 loc) · 1.38 KB
/
USART_Int_atmega328.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* USARTatmega328.c
*
* Created on: Feb 5, 2011
* Author: bgouveia
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "USART_Int_atmega328.h"
/********************************************************************************
Global Variables
********************************************************************************/
void USART_Init(uint16_t ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/*
>> Asynchronous mode
>> No Parity
>> 1 StopBit
*/
/* Set frame format: 8data, 1stop bit */
UCSR0C = (1<<UCSZ01)| (1<<UCSZ00);
/*Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
//Enable double transmission speed
UCSR0A |= (1<<U2X0);
}
//This function is used to read the available data
//from USART. This function will wait until data is
//available.
char USARTReadChar_blocking()
{
//Wait until a data is available
while(!(UCSR0A & (1<<RXC0))){/*Do nothing*/ }
//Now USART has got data from host
//and is available is buffer
return UDR0;
}
//This function writes the given "data" to
//the USART which then transmit it via TX line
void USARTWriteChar(char data)
{
//Wait untill the transmitter is ready
while(!(UCSR0A & (1<<UDRE0)));
//Now write the data to USART buffer
UDR0=data;
}
void print_string(const char *data) {
while (*data != '\0') {
USARTWriteChar(*data++);
}
}