Test serial data transmission on atmega32a

This commit is contained in:
2024-08-24 22:02:52 +02:00
parent 08f14d81c7
commit 84cf9165d1
2 changed files with 51 additions and 3 deletions

View File

@@ -5,7 +5,7 @@
# Written by Leon Krieg <info@madcow.dev> # Written by Leon Krieg <info@madcow.dev>
ARCH := m32 ARCH := m32
FREQ := 1600000UL FREQ := 8000000UL
MCU := atmega32a MCU := atmega32a
ASP := usbasp ASP := usbasp
GCC := avr-gcc GCC := avr-gcc
@@ -22,7 +22,10 @@ all: flash
.PHONY: flash .PHONY: flash
flash: $(TARGET) flash: $(TARGET)
$(AVD) -c $(ASP) -p $(ARCH) -U flash:w:$(TARGET) $(AVD) -c $(ASP) -p $(ARCH) \
-U lfuse:w:0xff:m \
-U hfuse:w:0x99:m \
-U flash:w:$(TARGET)
$(TARGET): src/main.c $(TARGET): src/main.c
$(GCC) -o $(ELFILE) $(CFLAGS) $(CPPFLAGS) $^ -mmcu=$(MCU) $(GCC) -o $(ELFILE) $(CFLAGS) $(CPPFLAGS) $^ -mmcu=$(MCU)

View File

@@ -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) 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; return 0;
} }