rewrote usart init function with some macro stuff.

This commit is contained in:
Jake Goodwin 2023-01-16 19:43:28 -08:00
parent 1791a94f9e
commit 830c1028c0
1 changed files with 34 additions and 29 deletions

View File

@ -7,7 +7,6 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <stdint.h>
#include <util/delay.h>
#include "avr_usart.h"
@ -22,28 +21,30 @@ int main() {
led_blink();
led_blink();
_delay_ms(1000);
_delay_ms(1000);
init_usart0();
//enable interrupts
sei();
uint8_t buf_len = 12;
//unsigned char data[12] = "snd 12chars\0";
//unsigned char data[1] = "A";
unsigned char data[1] = "B";
unsigned char linefeed = 0x0a;
unsigned char carrigereturn = 0x0d;
//unsigned char data[11] = "snd 11chars";
unsigned char data[8] = "AT+BAUD3";
unsigned char data_cmd[2] = "AT";
uint8_t data_len = 8;
uint8_t data_cmd_len = 2;
while(1) {
//serial0_write(data, buf_len);
if(data[0] == 0) {
data[0] = '!';
}
tx_usart0(data[0]);
//serial0_read(data, buf_len);
//data = rx_usart0();
serial0_flush_rxbuf();
serial0_read_with_err_checking(data, 1);
led_blink();
serial0_write(data, data_len);
tx_usart0(carrigereturn);
tx_usart0(linefeed);
//_delay_ms(1000);
_delay_ms(500);
}
return 0;
@ -61,26 +62,30 @@ int main() {
* Description: init usart0 hardware in async mode
*/
void init_usart0(void) {
//DDRB |= (1<<TX_DDR)|(0<<RX_DDR);
//PORTD |= (0<<TX_PIN)|(0<<RX_PIN);
//setup stuff for usart communications.
//set baud rate, this is 9600Baud with a 8Mhz clock
//UBRR0H |= (uint8_t) (BT_UBRR>>8);
//UBRR0L |= (uint8_t) BT_UBRR;
UBRR0H |= (uint8_t) (0x33>>8);
UBRR0L |= (uint8_t) 0x33;
UBRR0H |= (uint8_t) (BT_UBRR>>8);
UBRR0L |= (uint8_t) BT_UBRR;
//Enable recv and Transmit pins, overrides other uses.
//IN the usart control and status register 0B
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
#if USE_U2X
UCSR0A |= (1<<U2X0);
#else
UCSR0A &= ~(1<<U2X0);
#endif
//setting the data frame format
//We leave the 2MSB zero for async serial. And we also don't enable
//parity bits for now. We set a 2bit stop bit and 8bit char size.
//UCSR0C = (1<<USBS0) | (3<<UCSZ00);
UCSR0C = (1<<USBS0) | (1<<UCSZ00) | (1<<UCSZ01) | (0<<UCSZ02);
UCSR0C = (0<<UMSEL01)|(0<<UMSEL00)| //Async
(0<<UPM01)|(0<<UPM00)| //Parity bits
(1<<USBS0)| //Stop bits
(0<<UCSZ02)|(1<<UCSZ01)|(1<<UCSZ00)| //Set the number of bits.
(0<<UCPOL0); //Clock polarity, 0=falling 1=rising
}