Uhr/Zahl/Zahl/uart.hpp
2013-12-10 17:09:51 +00:00

69 lines
1.4 KiB
C++

/*
* Rs232.h
*
* Created: 04.11.2013 21:31:09
* Author: netz
*/
#ifndef RS232_H_
#define RS232_H_
#include "config.h"
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
template <uint32_t baudrate, uint8_t diget>
class Uart {
public:
Uart() {
sei();
init();
send("Uart done!\n");
}
void send(const char *text) {
while (*text)
{
uart_putchar(*text);
text++;
}
}
/* USART_RX_vect */
void interrupt() {
uint8_t nextChar;
nextChar = UDR;
if( nextChar != '\n' && nextChar != '\r' && uart_str_count < UART_MAXSTRLEN ) {
uart_string[uart_str_count] = nextChar;
uart_str_count++;
} else {
if(uart_string[0] == 't') {
number = uart_string[diget];
}
uart_str_count = 0;
}
}
uint8_t getDig() {
return number;
}
private:
uint8_t number = 0;
static const uint8_t UART_MAXSTRLEN = 12;
uint8_t uart_str_count = 0;
uint8_t uart_string[UART_MAXSTRLEN];
void init() {
UCSRC = (1 << UCSZ1) | (1 << UCSZ0); //8N1
UCSRB = /*(1<<RXEN1) | / (1<<RXCIE) | */ (1<<TXEN); //Enable TXEN im Register UCR TX-Data Enable
UBRRH = (uint8_t)(((F_CPU / 16 / baudrate) - 1)>>8);
UBRRL = (uint8_t)(F_CPU / 16 / baudrate) - 1; //Teiler wird gesetzt
}
uint8_t uart_putchar(uint8_t c) {
loop_until_bit_is_set(UCSRA, UDRE); //Ausgabe des Zeichens
UDR = c;
return 0;
}
};
#endif /* RS232_H_ */