uart.cpp 807 Bytes
Newer Older
Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
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
#include "uart.hpp"

#include "servos.hpp"
#include "command.h"

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

void initUart()
{
    cli();
    
    UCSR0A = (1 << U2X0);  // double transmission speed
    UCSR0B = (1 << RXCIE0) // enable RX complete interrupt
           | (1 << RXEN0)  // enable receiver
           | (1 << TXEN0); // enable transmitter
    UCSR0C = (0 << UMSEL01) | (0 << UMSEL00) // asynchronous USART
           | (0 << UPM01) | (0 << UPM00)     // no parity bit
           | (0 << USBS0)                    // 1 stop bit
           | (1 << UCSZ01) | (1 << UCSZ00);  // 8 data bits
    
    UBRR0 = 16; // 115.2k baud rate
    
    sei();
}

Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
27
void writeUart(uint8_t value)
Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
28
{
Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
29
30
31
    while (!(UCSR0A & (1 << UDRE0)));
    
    UDR0 = value;
Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
32
33
}

Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
34
ISR(USART_RX_vect)
Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
35
{
Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
36
    uart_add_ch(UDR0);
Xoaquin Castrelo's avatar
Xoaquin Castrelo committed
37
}