Untitled
unknown
plain_text
a year ago
911 B
3
No Index
#include <avr/io.h>
#include <avr/interrupt.h>
#define USART0_BAUD_RATE(BAUD_RATE) ((float)(F_CPU * 64 / (16 * (float)BAUD_RATE)) + 0.5)
void uart_init(void) {
// Set PA6 (the Tx pin) as output initialized to low.
PORTA.OUTCLR = PIN6_bm;
PORTA.DIRSET = PIN6_bm;
// set baud_rate register
USART0.BAUD = (uint16_t)USART0_BAUD_RATE(9600);
// most interrupts and loopback disabled
USART0.CTRLA = 0;
// Only Tx enabled
USART0.CTRLB = USART_TXEN_bm;
// USART0.CTRLC defaults to 8/N/1 asynchronous
}
void _putc(const uint8_t v) {
while (!(USART0.STATUS & USART_DREIF_bm)) {
}
USART0.TXDATAL = v;
}
void uart_write(uint16_t v) {
// Synchronously write data
uint16_t div = 10000;
while (div > 0) {
const uint8_t ch = '0' + v/div;
v %= div;
div /= 10;
_putc(ch);
}
_putc('\r');
_putc('\n');
while (!(USART0.STATUS & USART_DREIF_bm)) {
}
_putc('\n');
}
Editor is loading...