From 4976af6a8458b8263abceb19368e95688031fb9a Mon Sep 17 00:00:00 2001 From: brett Date: Thu, 25 Apr 2024 00:09:09 -0500 Subject: doooone --- final_project/serial.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'final_project/serial.c') diff --git a/final_project/serial.c b/final_project/serial.c index bb5b58f..7a230ef 100644 --- a/final_project/serial.c +++ b/final_project/serial.c @@ -5,14 +5,19 @@ * Author: bsw9xd */ #include "serial.h" + +#define F_CPU 16000000UL +#define BAUD 9600 //standard minimum baud rate void usart_init() { - volatile int ubrr = (F_CPU / (16UL * BAUD)) - 1; //TODO: why is this volatile int??? + volatile int ubrr = (F_CPU / (16UL * BAUD)) - 1; UCSR1A = 0; //async normal communication + //enable transmission/reception UCSR1B = (1 << TXEN) | (1 << RXEN); + //8 bits per packet, no parity, 1 stop bit UCSR1C = (1 << UCSZ0) | (1 << UCSZ1); - + //set baud rate UBRR1H = (unsigned char)(ubrr << 8); UBRR1L = (unsigned char)ubrr; } @@ -23,17 +28,16 @@ void usart_txt(char data) { //transmit data } char usart_rxt() { - if(UCSR1A & (1 << RXC)) { - return UDR1; - } - return '\0'; + if(UCSR1A & (1 << RXC)) { return UDR1; } //attempt to get input, + return '\0'; //if there is none, then return null char } -char usart_rxt_blocking() { //TODO maybe replace usart_txt - while(!(UCSR1A & (1 << RXC))); //TODO simplify +char usart_rxt_blocking() { + while(!(UCSR1A & (1 << RXC))); //wait for input via polling return UDR1; } void usart_txstr(char *str) { + //transmit strong character by character untill null terminator for(int i = 0; str[i] != '\0'; i++) usart_txt(str[i]); } -- cgit v1.2.3