2023-01-14 06:51:49 +00:00
|
|
|
# AVR_ATMEGA_USART
|
2023-01-14 19:19:21 +00:00
|
|
|
---
|
2023-01-14 06:53:13 +00:00
|
|
|
## Description:
|
|
|
|
|
2023-01-14 19:19:21 +00:00
|
|
|
This is a asynchronous serial program for the ATMEGA seiries of mcu. The sotfware
|
2023-01-14 06:53:13 +00:00
|
|
|
is tested and working.
|
|
|
|
|
2023-01-14 19:19:21 +00:00
|
|
|
Over time, I'm planning to add support for multiple AVR MCUs, however that
|
|
|
|
may end up requiring more memory, so extensive of use of define macros will
|
|
|
|
likely end up being the end result.
|
|
|
|
|
2023-01-17 05:01:04 +00:00
|
|
|
|
2023-01-14 19:19:21 +00:00
|
|
|
## Features:
|
|
|
|
|
|
|
|
- Multiple Baud Rates: 300, 600, 1200, 2400, 9600 ...
|
|
|
|
- Asynchronous Receive and Transmit.
|
|
|
|
- Optional Parity checking.
|
|
|
|
- 7-8 data bits.
|
|
|
|
- Hardware abstraction API
|
|
|
|
- No Interrupt service routines required.
|
2023-02-02 23:18:53 +00:00
|
|
|
- BSD-3 Licensing, aka you can use this for whatever you want pretty much.
|
2023-01-17 05:01:04 +00:00
|
|
|
- Runtime configuration of serial settings.
|
2023-01-14 19:19:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
|
|
|
|
|
|
|
### Example 1
|
|
|
|
|
|
|
|
```C
|
|
|
|
#include "avr_usart.h"
|
|
|
|
|
2023-01-15 22:37:57 +00:00
|
|
|
/*
|
|
|
|
*Description: Echos a single char back to the term.
|
|
|
|
*/
|
|
|
|
void echo_char(char* cmd) {
|
2023-01-14 19:19:21 +00:00
|
|
|
init_usart();
|
|
|
|
|
2023-01-15 22:37:57 +00:00
|
|
|
unsigned char data[1] = "0";
|
|
|
|
uint8_t data_len = 1;
|
|
|
|
|
|
|
|
//clear the buffer
|
|
|
|
serial0_flush_rxbuf();
|
|
|
|
|
|
|
|
//receive the char.
|
|
|
|
serial0_read(data, data_len);
|
|
|
|
|
|
|
|
//Now echo it.
|
|
|
|
serial0_write(data, data_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2023-01-17 05:01:04 +00:00
|
|
|
usart0_init();
|
|
|
|
serial0_enable_parity_bit(2); //odd parity
|
|
|
|
sei(); //enable interrupts, not needed right now.
|
|
|
|
|
2023-01-15 22:37:57 +00:00
|
|
|
while(1) {
|
|
|
|
echo_char();
|
2023-01-14 19:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2023-01-14 06:53:13 +00:00
|
|
|
|
|
|
|
## ISSUES:
|
|
|
|
|
|
|
|
It's required to set the fuses in the chip for 8Mhz, otherwise a baud rate
|
|
|
|
of 9600 will result in errors at a 7% rate.
|
|
|
|
|
2023-01-17 05:01:04 +00:00
|
|
|
### Internal Crystal
|
|
|
|
|
|
|
|
The internal crystal oscillator can have up too 10% variation. This doesn't
|
|
|
|
seem like a lot, but it makes a huge differnce.
|
|
|
|
|
|
|
|
for one of the Atmega 328p MCUs I tested it woul return garbage data to a
|
|
|
|
a serial terminal at 8-N-1 4800 baud.
|
|
|
|
|
|
|
|
when I set the usb to serial adapter too 7-N-1 I would stop having issues
|
|
|
|
suddenly.
|
|
|
|
|
|
|
|
going back to 8-N-1 on the usb to serial adapter and then changing it's baud
|
|
|
|
rate too 4825 totally fixed the issue. This told me that the crystal that
|
|
|
|
the 328p was clocking off of was faster than it should be.
|
2023-01-14 06:53:13 +00:00
|
|
|
|
2023-01-17 05:01:04 +00:00
|
|
|
TLDR; You should really use a decent external clock for async serial otherwise
|
|
|
|
you are going to have a bad time.
|