/* * Author: username * Date: 2024 * filename: usart.c * description: module_purpose */ #ifndef __AVR_ATtiny404__ #define __AVR_ATtiny404__ #endif #include "usart.h" #include #include // These are technically correct, but the libc requires F_CPU for delay stuff // that doesn't match the actual F_CPU because it uses the value after the // clock is divided. // #define F_CPU 3333333UL // #define F_PER F_CPU / 6 #define F_PER 3333333UL #define USART0_BAUD_RATE(BAUD_RATE) \ ((float)(F_PER * 64 / (16 * (float)BAUD_RATE)) + 0.5) // RX PIN6, TX PIN7 // ALT: RX PIN12 TX PIN11 void USART0_Init(void) { // Config TxD as output, and rx as input? PORTB.DIR &= ~(1 << 3); PORTB.DIR |= (1 << 2); // It says to set the TX pin high? // PORTB.OUT |= (1<<2); // set buad rate. USART0.BAUD = (uint16_t)USART0_BAUD_RATE(9600); // USART0.BAUD = 1388; // set the frame format. USART0.CTRLC = 0x3; // setting 8-bit mode. // Enable transmitter and receiver (USARTn.CTRLB) // USART0.CTRLB |= (1<<7)|(1<<6); USART0.CTRLB |= USART_TXEN_bm; } void USART0_Alt_Init(void) { // setup Alternate pints on PA1 and PA2 PORTMUX.CTRLB |= PORTMUX_USART0_ALTERNATE_gc; PORTA.DIR |= (1 << 1); PORTA.DIR &= ~(1 << 2); // It says to set the TX pin high? // PORTA.OUT |= (1<<11); // set buad rate. USART0.BAUD = (uint16_t)USART0_BAUD_RATE(9600); // set the frame format. USART0.CTRLC = 0x3; // setting 8-bit mode. // Enable transmitter and receiver (USARTn.CTRLB) // USART0.CTRLB |= (1<<7)|(1<<6); USART0.CTRLB |= USART_TXEN_bm; } void USART0_sendChar(char c) { while (!(USART0.STATUS & USART_DREIF_bm)) { ; } USART0.TXDATAL = c; USART0.TXDATAH = c; } void USART0_sendString(char *str) { for (size_t i = 0; i < strlen(str); i++) { USART0_sendChar(str[i]); } }