Test serial data transmission on atmega32a
This commit is contained in:
47
src/main.c
47
src/main.c
@@ -1,6 +1,51 @@
|
||||
#define UNUSED(s) (void)(s)
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
|
||||
|
||||
void UART_Init(long USART_BAUDRATE)
|
||||
{
|
||||
UCSRB |= (1 << RXEN) | (1 << TXEN);
|
||||
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
|
||||
UBRRL = BAUD_PRESCALE;
|
||||
UBRRH = (BAUD_PRESCALE >> 8);
|
||||
}
|
||||
|
||||
unsigned char UART_RxChar()
|
||||
{
|
||||
while ((UCSRA & (1 << RXC)) == 0);
|
||||
return UDR;
|
||||
}
|
||||
|
||||
void UART_TxChar(char ch)
|
||||
{
|
||||
while ((UCSRA & (1 << UDRE)) == 0);
|
||||
UDR = ch;
|
||||
}
|
||||
|
||||
void UART_SendString(char *str)
|
||||
{
|
||||
unsigned char j = 0;
|
||||
|
||||
while (str[j] != 0) {
|
||||
UART_TxChar(str[j++]);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char buf[64];
|
||||
|
||||
UART_Init(9600);
|
||||
|
||||
for (int i = 1;; i++) {
|
||||
if (i >= 999) i = 1;
|
||||
sprintf(buf, "BOINK #%03d\r\n", i);
|
||||
UART_SendString(buf);
|
||||
_delay_ms(3000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user