b15f/usart.cpp

60 lines
1,006 B
C++
Raw Normal View History

2019-03-26 10:35:41 +00:00
#include "usart.h"
void USART::init()
{
2019-03-27 10:45:30 +00:00
UCSR0A = _BV(U2X0);
2019-03-26 10:35:41 +00:00
UCSR0B = _BV(RXEN0) | _BV(TXEN0);
// Einstellen des Datenformats: 8 Datenbits, 1 Stoppbit
UCSR0C = _BV(UCSZ00) |_BV(UCSZ01);// (1<<URSEL0)|(1<<UCSZ10)|(1<<UCSZ00);
// setze Baudrate
2019-03-27 10:45:30 +00:00
UBRR0H = (((F_CPU / (8UL * BAUDRATE))-1) >> 8) & 0xFF;
UBRR0L = ((F_CPU / (8UL * BAUDRATE))-1) & 0xFF;
2019-03-26 10:35:41 +00:00
}
void USART::writeByte(uint8_t b)
{
2019-03-27 10:45:30 +00:00
while (!(UCSR0A & (1<<UDRE0)));
2019-03-26 10:35:41 +00:00
UDR0 = b;
2019-03-28 12:32:24 +00:00
// while(!(UCSR0A & _BV(TXC0)));
2019-03-26 10:35:41 +00:00
}
2019-03-26 14:02:58 +00:00
void USART::writeInt(uint16_t v)
{
2019-03-28 12:32:24 +00:00
2019-03-27 10:45:30 +00:00
while (!(UCSR0A & (1<<UDRE0)));
2019-03-26 14:02:58 +00:00
UDR0 = v & 0xFF;
2019-03-27 10:45:30 +00:00
2019-03-26 14:02:58 +00:00
v >>= 8;
2019-03-27 10:45:30 +00:00
while (!(UCSR0A & (1<<UDRE0)));
2019-03-26 14:02:58 +00:00
UDR0 = v & 0xFF;
2019-03-28 12:32:24 +00:00
// while(!(UCSR0A & _BV(TXC0)));
2019-03-26 14:02:58 +00:00
}
2019-03-27 14:48:36 +00:00
void USART::writeStr(const char* str, uint8_t len)
{
writeByte(len);
while(len--)
writeByte(*str++);
}
2019-03-26 10:35:41 +00:00
uint8_t USART::readByte()
{
while (!(UCSR0A & (1<<RXC0)));
return UDR0;
}
2019-03-26 14:02:58 +00:00
uint16_t USART::readInt()
{
uint16_t v = readByte();
v |= readByte() << 8;
return v;
}