prgKbd-keyboard/firmware/lib/uart.c

57 lines
883 B
C
Executable File

#include <avr/io.h>
void USARTWriteChar(char data)
{
//Wait until the transmitter is ready
while(!(UCSRA & (1<<UDRE)))
{
//Do nothing
}
//Now write the data to USART buffer
UDR=data;
}
void USARTInit(uint16_t ubrr_value)
{
//Set Baud rate
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
/*Set Frame Format
>> Asynchronous mode
>> No Parity
>> 1 StopBit
>> char size 8
*/
UCSRC=(1<<URSEL)|(3<<UCSZ0);
//Enable The receiver and transmitter
UCSRB=(1<<RXEN)|(1<<TXEN);
}
void UARTWriteString(char* data,int len)
{
int i;
for(i=0;i<len;i++)
{
USARTWriteChar(data[i]);
}
}
uint8_t UART_GETCHAR(void)
{
while (!(UCSRA & (1<<RXC))) // warten bis Zeichen verfuegbar
;
return UDR; // Zeichen aus UDR an Aufrufer zurueckgeben
}