Chapter 5

Software UART Transmission

This module provides buffered, software-based, transmit-only UART functionality. TIMER0 is the baud-rate generator. By default, this module twiddles PORTEbit 1 or PORTDbit 1. By redefining a couple of functions you can make any output bit the transmit line.

5.1  Usage

To use the transmit-only UART functions, place this include in your source file:

  #include <tx-only.h>

5.2  Functions

void tx_only_init(uint8_t timer0_mode, uint8_t timer0_count)   function

This function initializes TIMER0 by placing it in the given mode, timer0_mode, and setting the counter value to timer0_count. Finally the OCIE0interrupt is enabled.

Only the three low-order bits of the timer0_mode argument are used. Register TCCR0is updated with these bits, plus bit CTC0. Register OCR0is updated with timer0_count.

For example, to simulate a 9600 baud UART transmission line on an ATmega103 running at 4 MHz, call tx_only_init() like this:

  tx_only_init(0x02, 4000000 / 8 / 9600);

void tx_only_clear()   function

This function clears the transmit buffer.

bool tx_only_chr(uint8_t c)   function

If there is room in the transmit buffer, this function places the given character, c, into the transmit buffer and the function then returns true. Otherwise, it returns false.

bool tx_only_str(const uint8_t* str)   function

If there is room in the transmit buffer, this function places the given string, str, into the transmit buffer and returns true. Otherwise, it returns false.

bool tx_only_str_P(PGM_P str)   function

This function is analogous to tx_only_str(), except that instead of taking a string in RAM, str points to a string stored in the program space.

void tx_only_port_low()   optional user-defined function

void tx_only_port_high()   optional user-defined function

These two functions are provided by the programmer to twiddle the bits of the transmission line. The “low” and “high” that appear in their names are in reference to the signal level, where “low” means a negative voltage, and “high” means a positive voltage.

These functions are optional. If you don’t provide them, the library supplies its own version that twiddles PORTEbit 1 (or if the microcontroller does not have PORTE, then PORTDbit 1).

5.3  Caveats

As written, this module has many limitations: