Go to file
Jake Goodwin 1d40f91885 Added a define for debuging usage 2023-02-24 14:27:14 -08:00
src Added a define for debuging usage 2023-02-24 14:27:14 -08:00
.gitignore updated to ignore binary stuff. 2023-01-13 22:57:03 -08:00
LICENSE Initial commit 2023-01-14 06:51:49 +00:00
Makefile hacked at makefile to run tests 2023-02-18 23:06:22 -08:00
README.md added info on parity flags 2023-02-18 23:35:57 -08:00

README.md

AVR_ATMEGA_USART


Description:

This is a asynchronous serial program for the ATMEGA seiries of mcu. The sotfware is tested and working.

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.

Parity calculations are done in hardware automatically, so there are no functions written to interface with the serial error flags as the end user will know more about exception and error handling for their application.

2023-02-18: Support for using external crystal oscillators was added to the makefile. Using a 14.7456Mhz crystal allows for reduced error rates when using any standard baud rates.

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.
  • BSD-3 Licensing, aka you can use this for whatever you want pretty much.
  • Runtime configuration of serial settings.

Instructions

Example 1

#include "avr_usart.h"

/*
 *Description: Echos a single char back to the term.
 */
void echo_char(char* cmd) {
    init_usart();
    
    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() {
    usart0_init();
    serial0_enable_parity_bit(2);   //odd parity
    sei();      //enable interrupts, not needed right now.

    while(1) {
        echo_char();
    }
}

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.

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.

TLDR; You should really use a decent external clock for async serial otherwise you are going to have a bad time.