251209_uart
This commit is contained in:
parent
8ee91bc2c9
commit
edf2acc6cc
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user