summaryrefslogtreecommitdiff
path: root/final_project/serial.c
blob: 7a230efda1263aa7a8f730034890414c4267c35b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * CFile1.c
 *
 * Created: 4/3/2024 10:21:26 AM
 *  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;
	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;
}
void usart_txt(char data) { //transmit data
	UDR1 = data; //buffer the data
	while(~UCSR1A & (1 << TXC)); //wait until the transmission is complete
	UCSR1A |= (1 << TXC);
	
}
char usart_rxt() {
	if(UCSR1A & (1 << RXC)) { return UDR1; } //attempt to get input,
	return '\0'; //if there is none, then return null char
}

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]);
}