From edf2acc6cc34cfde39f857ef1c991dfda8008bc9 Mon Sep 17 00:00:00 2001 From: Maciej Bowszys Date: Tue, 9 Dec 2025 21:53:20 +0100 Subject: [PATCH] 251209_uart --- recv/pixmod_recv_1v0/main.c | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/recv/pixmod_recv_1v0/main.c b/recv/pixmod_recv_1v0/main.c index 1e1a425..f9b8416 100644 --- a/recv/pixmod_recv_1v0/main.c +++ b/recv/pixmod_recv_1v0/main.c @@ -100,3 +100,57 @@ void setPixelColor(uint8_t n, uint8_t r, uint8_t g, uint8_t b) { +int uart_putchar(char c, FILE *stream); + +void uart_init(void) { + UART_TX_DDR |= (1 << UART_TX_PIN); // set to output + + UART_TX_PORT |= (1 << UART_TX_PIN); // set to HIGH - idle state in UART + + fdevopen(uart_putchar, NULL); +} + +void uart_transmit(uint8_t data) { + uint8_t i; + + UART_TX_PORT &= ~(1 << UART_TX_PIN); // LOW for start bit + _delay_us(UART_BIT_DELAY_US); + + // send 8 bits LSB + for (i = 0; i < 8; i++) { + if (data & (1 << i)) { + UART_TX_PORT |= (1 << UART_TX_PIN); // set high + } else { + UART_TX_PORT &= ~(1 << UART_TX_PIN); // set low + } + + _delay_us(UART_BIT_DELAY_US); + } + + UART_TX_PORT |= (1 << UART_TX_PIN); // high for stop bit + _delay_us(UART_BIT_DELAY_US); +} + +void uart_print_str(const char* str) { + while (*str) { + uart_transmit(*str++); + } +} + +int uart_putchar(char c, FILE *stream) { + uint8_t sreg = SREG; + cli(); + + if (c == '\n') { + uart_transmit('\r'); + } + uart_transmit(c); + + SREG = sreg; + sei(); + + return 0; +} + + +