51 lines
1.2 KiB
C++
51 lines
1.2 KiB
C++
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include "usart.h"
|
|
|
|
namespace usart
|
|
{
|
|
namespace {
|
|
static FILE mystdout;
|
|
}
|
|
|
|
void init( unsigned long baud) {
|
|
fdev_setup_stream(&mystdout, put_printf, NULL, _FDEV_SETUP_WRITE);
|
|
stdout = &mystdout;
|
|
const unsigned int ubrr = F_CPU/16/baud-1;
|
|
|
|
/*Set baud rate */
|
|
UBRRH = (unsigned char)(ubrr>>8);
|
|
UBRRL = (unsigned char)ubrr;
|
|
//Enable receiver and transmitter
|
|
UCSRB = (1<<RXEN)|(1<<TXEN);
|
|
//UCSRB |= (1<< RXCIE)|(1<<TXCIE);
|
|
/* Set frame format: 8data, 2stop bit */
|
|
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
|
|
sei();
|
|
|
|
}
|
|
|
|
void put(char data) {
|
|
/* Wait for empty transmit buffer */
|
|
while ( !( UCSRA & (1<<UDRE)) )
|
|
;
|
|
/* Put data into buffer, sends the data */
|
|
UDR = data;
|
|
}
|
|
|
|
char get( void ) {
|
|
/* Wait for data to be received */
|
|
while ( !(UCSRA & (1<<RXC)) )
|
|
;
|
|
/* Get and return received data from buffer */
|
|
return UDR;
|
|
}
|
|
|
|
int put_printf(char var, FILE *stream) {
|
|
put(var);
|
|
return 0;
|
|
}
|
|
} // namespace usart
|
|
|
|
|