60 lines
1.4 KiB
C++
60 lines
1.4 KiB
C++
|
/*
|
||
|
* Rs232.h
|
||
|
*
|
||
|
* Created: 04.11.2013 21:31:09
|
||
|
* Author: netz
|
||
|
*/
|
||
|
|
||
|
#ifndef RS232_H_
|
||
|
#define RS232_H_
|
||
|
|
||
|
#include <avr/io.h>
|
||
|
#include <avr/interrupt.h>
|
||
|
|
||
|
template <uint32_t baudrate>
|
||
|
class Uart {
|
||
|
public:
|
||
|
Uart() {
|
||
|
sei();
|
||
|
init();
|
||
|
println("Uart done!");
|
||
|
}
|
||
|
void print(const char *text) {
|
||
|
while (*text)
|
||
|
{
|
||
|
uart_putchar(*text);
|
||
|
text++;
|
||
|
}
|
||
|
}
|
||
|
void printDec(uint16_t wert) {
|
||
|
print((wert/10000)+'0');
|
||
|
print(((wert/1000)%10)+'0');
|
||
|
print(((wert/100)%10)+'0');
|
||
|
print(((wert/10)%10)+'0');
|
||
|
print((wert%10)+'0');
|
||
|
}
|
||
|
void println(const char *text) {
|
||
|
print(text);
|
||
|
print("\r");
|
||
|
print("\n");
|
||
|
}
|
||
|
void print(uint8_t wert) {
|
||
|
uart_putchar(wert);
|
||
|
}
|
||
|
private:
|
||
|
void init() {
|
||
|
UBRR0L = (F_CPU / (baudrate * 16L) - 1); //Teiler wird gesetzt
|
||
|
UCSR0A= (0<<RXC0) | (0<<TXC0) | (0<<UDRE0) | (0<<FE0) | (0<<DOR0) | (0<<UPE0) | (0<<U2X0) | (0<<MPCM0);
|
||
|
UCSR0B= (0<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0) | (0<<RXEN0) | (1<<TXEN0) | (0<<UCSZ02) | (0<<RXB80) | (0<<TXB80); //Enable TXEN im Register UCR TX-Data Enable
|
||
|
UCSR0C= (0<<UMSEL01) | (0<<UMSEL00) | (0<<UPM01) | (0<<UPM00) | (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00) | (0<<UCPOL0); //8N1
|
||
|
}
|
||
|
uint8_t uart_putchar(uint8_t c) {
|
||
|
loop_until_bit_is_set(UCSR0A, UDRE0); //Ausgabe des Zeichens
|
||
|
UDR0 = c;
|
||
|
return 0;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
#endif /* RS232_H_ */
|