15-09-2021

Software UART Library. The mikroC PRO for AVR provides routines for implementing Software UART communication. These routines are hardware independent and can be used with any MCU. The Software UART Library provides easy communication with other devices via the RS232 protocol. The software-UART available here is based on the 'gerneric software uart' by Colin Gittins which can be downloaded from the iar.com website in the application-notes section. I have ported/adapted this code to AVRs using avr-gcc/avr-libc and fixed some smaller mistakes.

The relevant sections look like this: ``` /. Software UART. This feature provides a software implementation of a UART allowing you to. perform serial communications. It supports speeds of up to 250Kbps, can be. configured to use a single IO pin and has an option to be interrupt driven. AVR UART Code for STK200 and STK300 in WinAVR C This UART code for AVR will work on STK200 and STK300 AVR boards and on most other UART circuits. An initial value is transmitted every 250mS. When a byte is received on UART, the interrupt is triggered and value received is transmitted instead. The value is displayed on LEDs as a test.

I've written a few software UARTs for AVR MCUs. All of them have bit-banged the output, using cycle-counted assembler busy loops to time the output of each bit. The code requires interrupts to be disabled to ensure accurate timing between bits. This makes it impossible to receive data at the same time as it is being transmitted, and therefore the bit-banged implementations have been half-duplex. By using the waveform generator of the timer/counter in many AVR MCUs, I've found a way to implement a full-duplex UART, which can simultaneously send and receive at up to 115kbps when the MCU is clocked at 8Mhz.
Avr Software Uart In CAvr Software Uart In CI expect most AVR developers are familiar with using PWM, where the output pin is toggled at a given duty cycle, independent of the code execution. The technique behind my full-duplex UART is using the waveform generation mode so the timer/counter hardware sets the OC0A pin at the appropriate time for each bit to be transmitted. TIM0_COMPA interrupt runs after each bit is output. The ISR determines if the next bit is a 0 or a 1. For a 1 bit, TCCR0A is configured to set OC0A on compare match. For a 0 bit, TCCR0A is configured to clear OC0A on compare match. The ISR also updates OCR0A with the appropriate timer count for the next bit. To allow for simultaneous receiving, the TIM0_COMPA transmit ISR is made interruptible (the first instruction is 'sei').
The receiving is handled by PCINT0, which triggers on the received start bit, and TIM0_COMPB interrupt which runs for each received bit. I wrote this ISR in assembler in order to ensure the received bit is read at the correct time, taking into consideration interrupt latency. If any other interrupts are enabled, they must be interruptible (ISR_NOBLOCK if written in C). I've implemented a two-level receive FIFO, which can be queried with the rx_data_ready() function. A byte can be read from the FIFO with rx_read().
Avr Software Uart In CThe code is written to work with the ATtiny13, ATtiny85, and ATtiny84. Only PCINT0 is supported, which on the t84 means that the receive pin must be on PORTA. With a few modifications to the code, PCINT1 could be used for receiving on PORTB with the t84. The total time required for both the transmit and the receive ISRs is 52 cycles. Adding an average interrupt overhead of 7 cycles for each ISR means that there must be at least 66 cycles between bits. At 8Mhz this means the maximum baud rate is 8,000,000/66 = 121kbps. The lowest standard baud rate that can be used with an 8Mhz clock is 9600bps.
The wgmuart application implements an example echo program running at the default baud rate of 57.6kbps. In addition to echoing back each character received, it prints out a period '.' every second along with toggling an LED.

Avr Software Uart In CI've published the code on github.

The microcontroller PIC16F84A has no hardware UART module, but we can use software UART to send/receive data to/from the PC via RS232 cable (COM port). This topic shows a simple example for the use of the UART protocol with the PIC16F84A MCU.

Hardware Required:

  • PIC16F84A microcontroller
  • 8MHz crystal
  • 2 x 22pF ceramic capacitors
  • 10K ohm resistor
  • MAX232 — datasheet
  • 4 x 10uF polarized capacitor
  • Female RS232 connector
  • Breadboard
  • 5V power source
  • Jumper wires

UART Example for PIC16F84A microcontroller circuit:

To interface the PIC16F84A MCU with the PC we need MAX232 signal level converter as shown in the circuit diagram. There are 2 lines between the PIC16F84A and the MAX232 and also two lines between the MAX232 and the female COM connector. One line for data transmit and the other one for data receive.
The female COM connector is connected to the PC using RS232 cable, this cable has to be male-female because the PC RS232 port type is male.
In this example the PIC16F84A MCU runs with 8MHz crystal oscillator.

Avr Software Uart

UART Example for PIC16F84A microcontroller C code:
The function #use rs232(xmit = PIN_B4, rcv = PIN_B5, baud = 2400) is used to configure the UART protocol. Here software UART is used.
where:
xmit is the transmit pin (RB4)
rcv is the receive pin (RB5)
2400 is the baud rate.
The functions used in the C code are:
printf: sends a string of characters over RS232 transmission pin (TX).
putc: send a character over RS232 transmission pin (TX).
getc(): read character if available.
Complete C code for CCS C compiler is below.