Test/src/usart/usart.c
2024-07-24 17:54:41 -07:00

64 lines
1.2 KiB
C

/*
* Author: username
* Date: 2024
* filename: usart.c
* description: module_purpose
*/
#include "usart.h"
#include <avr/io.h>
#include <string.h>
#define F_CPU 3333333UL
#define F_PER F_CPU / 6
#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);
//setup Alternate pints on PA1 and PA2
/*
PORTMUX.CTRLB |= PORTMUX_USART0_ALTERNATE_gc;
PORTA.DIRSET |= (1<<1);
PORTA.DIRSET &= ~(1<<2);
*/
//It says to set the TX pin high?
//PORTB.OUT |= (1<<2);
//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]);
}
}