AVR304: Half Duplex Interrupt Driven Software UART. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Similar documents
AVR305: Half Duplex Compact Software UART. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1309: Using the XMEGA SPI. 8-bit Microcontrollers. Application Note. Features. 1 Introduction SCK MOSI MISO SS

AVR317: Using the Master SPI Mode of the USART module. 8-bit Microcontrollers. Application Note. Features. Introduction

AVR353: Voltage Reference Calibration and Voltage ADC Usage. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1510: Xplain training - XMEGA USART. 8-bit Microcontrollers. Application Note. Prerequisites. 1 Introduction

AVR32701: AVR32AP7 USB Performance. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1301: Using the XMEGA DAC. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR033: Getting Started with the CodeVisionAVR C Compiler. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR1900: Getting started with ATxmega128A1 on STK bit Microcontrollers. Application Note. 1 Introduction

AVR32138: How to optimize the ADC usage on AT32UC3A0/1, AT32UC3A3 and AT32UC3B0/1 series. 32-bit Microcontrollers. Application Note.

AVR1318: Using the XMEGA built-in AES accelerator. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Using CryptoMemory in Full I 2 C Compliant Mode. Using CryptoMemory in Full I 2 C Compliant Mode AT88SC0104CA AT88SC0204CA AT88SC0404CA AT88SC0808CA

AVR319: Using the USI module for SPI communication. 8-bit Microcontrollers. Application Note. Features. Introduction

8-bit. Application Note. Microcontrollers. AVR282: USB Firmware Upgrade for AT90USB

AVR1600: Using the XMEGA Quadrature Decoder. 8-bit Microcontrollers. Application Note. Features. 1 Introduction. Sensors

AVR2006: Design and characterization of the Radio Controller Board's 2.4GHz PCB Antenna. Application Note. Features.

APPLICATION NOTE. Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Atmel AVR 8-bit Microcontroller. Introduction.

AVR1922: Xplain Board Controller Firmware. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR32788: AVR 32 How to use the SSC in I2S mode. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

Application Note. 8-bit Microcontrollers. AVR272: USB CDC Demonstration UART to USB Bridge

AVR055: Using a 32kHz XTAL for run-time calibration of the internal RC. 8-bit Microcontrollers. Application Note. Features.

AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Application Note. 8-bit Microcontrollers. AVR270: USB Mouse Demonstration

General Porting Considerations. Memory EEPROM XRAM

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

8-bit Microcontroller. Application Note. AVR400: Low Cost A/D Converter

3-output Laser Driver for HD-DVD/ Blu-ray/DVD/ CD-ROM ATR0885. Preliminary. Summary. Features. Applications. 1. Description

8-bit Microcontroller. Application Note. AVR415: RC5 IR Remote Control Transmitter. Features. Introduction. Figure 1.

AVR115: Data Logging with Atmel File System on ATmega32U4. Microcontrollers. Application Note. 1 Introduction. Atmel

AVR2004: LC-Balun for AT86RF230. Application Note. Features. 1 Introduction

Application Note. C51 Bootloaders. C51 General Information about Bootloader and In System Programming. Overview. Abreviations

8-bit RISC Microcontroller. Application Note. AVR182: Zero Cross Detector

USER GUIDE EDBG. Description

8-bit RISC Microcontroller. Application Note. AVR910: In-System Programming

Application Note. 8-bit Microcontrollers. AVR293: USB Composite Device

AVR287: USB Host HID and Mass Storage Demonstration. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Atmel AVR4920: ASF - USB Device Stack - Compliance and Performance Figures. Atmel Microcontrollers. Application Note. Features.

AVR151: Setup and Use of the SPI. Introduction. Features. Atmel AVR 8-bit Microcontroller APPLICATION NOTE

8051 Flash Microcontroller. Application Note. A Digital Thermometer Using the Atmel AT89LP2052 Microcontroller

AVR32100: Using the AVR32 USART. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

Atmel AVR4903: ASF - USB Device HID Mouse Application. Atmel Microcontrollers. Application Note. Features. 1 Introduction

APPLICATION NOTE Atmel AT02509: In House Unit with Bluetooth Low Energy Module Hardware User Guide 8-bit Atmel Microcontroller Features Description

AT91 ARM Thumb Microcontrollers. AT91SAM CAN Bootloader. AT91SAM CAN Bootloader User Notes. 1. Description. 2. Key Features

8-bit RISC Microcontroller. Application Note. AVR236: CRC Check of Program Memory

AVR245: Code Lock with 4x4 Keypad and I2C LCD. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Atmel AVR4921: ASF - USB Device Stack Differences between ASF V1 and V2. 8-bit Atmel Microcontrollers. Application Note. Features.

AVR32110: Using the AVR32 Timer/Counter. 32-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR030: Getting Started with IAR Embedded Workbench for Atmel AVR. 8-bit Microcontrollers. Application Note. Features.

AVR1003: Using the XMEGA Clock System. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

32-bit AVR UC3 Microcontrollers. 32-bit AtmelAVR Application Note. AVR32769: How to Compile the standalone AVR32 Software Framework in AVR32 Studio V2

AVR1321: Using the Atmel AVR XMEGA 32-bit Real Time Counter and Battery Backup System. 8-bit Microcontrollers. Application Note.

AT89C5131A Starter Kit... Software User Guide

How To Use An Atmel Atmel Avr32848 Demo For Android (32Bit) With A Microcontroller (32B) And An Android Accessory (32D) On A Microcontroller (32Gb) On An Android Phone Or

AVR125: ADC of tinyavr in Single Ended Mode. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR ONE!... Quick-start Guide. EVK Windows 32104B AVR ONE! 02/10

AVR106: C functions for reading and writing to Flash memory. 8-bit Microcontrollers. Application Note. Features. Introduction

AT91 ARM Thumb Microcontrollers. Application Note. Interfacing a PC Card to an AT91RM9200-DK. Introduction. Hardware Interface

AVR442: PC Fan Control using ATtiny13. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AT91SAM ARM-based Flash MCU. Application Note

8-bit Microcontroller. Application Note. AVR222: 8-point Moving Average Filter

APPLICATION NOTE. Atmel AT02985: User s Guide for USB-CAN Demo on SAM4E-EK. Atmel AVR 32-bit Microcontroller. Features. Description.

Application Note. Atmel ATSHA204 Authentication Modes. Prerequisites. Overview. Introduction

Atmel AVR1017: XMEGA - USB Hardware Design Recommendations. 8-bit Atmel Microcontrollers. Application Note. Features.

APPLICATION NOTE. Atmel AVR443: Sensor-based Control of Three Phase Brushless DC Motor. Atmel AVR 8-bit Microcontrollers. Features.

AVR131: Using the AVR s High-speed PWM. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

AVR241: Direct driving of LCD display using general IO. 8-bit Microcontrollers. Application Note. Features. Introduction AVR

SMARTCARD XPRO. Preface. SMART ARM-based Microcontrollers USER GUIDE

8-bit RISC Microcontroller. Application Note. AVR155: Accessing an I 2 C LCD Display using the AVR 2-wire Serial Interface

APPLICATION NOTE. Authentication Counting. Atmel CryptoAuthentication. Features. Introduction

8-bit Microcontroller. Application Note. AVR105: Power Efficient High Endurance Parameter Storage in Flash Memory

3-output Laser Driver for HD-DVD/ Blu-ray/DVD/ CD-ROM ATR0885. Preliminary. Summary

Application Note. Atmel CryptoAuthentication Product Uses. Atmel ATSHA204. Abstract. Overview

AT15007: Differences between ATmega328/P and ATmega328PB. Introduction. Features. Atmel AVR 8-bit Microcontrollers APPLICATION NOTE

APPLICATION NOTE. Secure Personalization with Transport Key Authentication. ATSHA204A, ATECC108A, and ATECC508A. Introduction.

8-bit Microcontroller. Application Note. AVR314: DTMF Generator

APPLICATION NOTE. Atmel AT04389: Connecting SAMD20E to the AT86RF233 Transceiver. Atmel SAMD20. Description. Features

APPLICATION NOTE. Atmel LF-RFID Kits Overview. Atmel LF-RFID Kit. LF-RFID Kit Introduction

How to Calculate the Capacitor of the Reset Input of a C51 Microcontroller 80C51. Application Note. Microcontrollers. Introduction

8-bit Microcontroller. Application Note. AVR201: Using the AVR Hardware Multiplier

Step Motor Controller. Application Note. AVR360: Step Motor Controller. Theory of Operation. Features. Introduction

AVR315: Using the TWI Module as I2C Master. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

256K (32K x 8) Battery-Voltage Parallel EEPROMs AT28BV256

AVR127: Understanding ADC Parameters. Introduction. Features. Atmel 8-bit and 32-bit Microcontrollers APPLICATION NOTE

AVR106: C Functions for Reading and Writing to Flash Memory. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

8-bit Microcontroller. Application Note. AVR286: LIN Firmware Base for LIN/UART Controller. LIN Features. 1. Atmel LIN/UART Controller

Application Note. 8-bit Microcontrollers. AVR280: USB Host CDC Demonstration. 1. Introduction

USER GUIDE. ZigBit USB Stick User Guide. Introduction

Application Note. USB Microcontrollers. USB PC Drivers Based on Generic HID Class. 1. Introduction

AVR223: Digital Filters with AVR. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Application Note. 8-bit Microcontrollers. AVR307: Half Duplex UART Using the USI Module

AT88CK490 Evaluation Kit

Tag Tuning/RFID. Application Note. Tag Tuning. Introduction. Antenna Equivalent Circuit

APPLICATION NOTE. AT07175: SAM-BA Bootloader for SAM D21. Atmel SAM D21. Introduction. Features

Flexible Active Shutter Control Interface using the MC1323x

Quick Start Guide. CAN Microcontrollers. ATADAPCAN01 - STK501 CAN Extension. Requirements

AVR311: Using the TWI Module as I2C Slave. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

APPLICATION NOTE. Atmel AT01095: Joystick Game Controller Reference Design. 8-/16-bit Atmel Microcontrollers. Features.

8-bit Microcontroller. Application. Note. AVR204: BCD Arithmetics. Features. Introduction. 16-bit Binary to 5-digit BCD Conversion bin2bcd16

Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers. 8-bit Atmel Microcontrollers. Application Note.

Transcription:

AVR304: Half Duplex Interrupt Driven Software UART Features Runs asynchronously (interrupt driven) Capable of handling baud rates of up to 38.4 kbaud @8MHz clock frequency Runs on any AVR device with 8-bit timer/counter and external interrupt 1 Introduction Lots of control applications communicate serially in one direction at a time only (half duplex). This application note describes how to make a half duplex UART on any AVR device using the 8-bit Timer/Counter0 and an external interrupt. This software can be used to implement a serial port on a device with no hardware UART, or it can be used to implement a second serial-port on AVR devices already equipped with a UART. Idle line is signaled by holding the line at logical one. The start bit is always a zero, and the UART receiver will detect the start of a frame by the first falling edge. Following the start bit come the data bits, followed by a stop bit which is always a logical one. This stop bit is held stable at one until the next start bit is sent. 8-bit Microcontrollers Application Note Figure 1-1. UART Communication Frame Format Figure 1-2. Serial frame of ASCII A ($41) Rev.

2 Theory of Operation Asynchronous serial data communication follows some simple rules on data transfer. Data is transmitted sequentially, one bit at a time. To inform the receiver that a new byte is arriving, each byte is placed between so-called start- and stop bits. This construction is called a frame. The frame format is shown in Figure 1-1 and Figure 1-2. The frame has 1 start-bit, 8 data bits, and 1 stop-bit. This frame type is implemented in this application note. The frame format can be extended, and might also include parity bits and more stop bits. In asynchronous transmissions, no separate clock is provided to the receiver. Correct reception of data is guaranteed by keeping all bit lengths equal. The receiver will synchronize from the first falling edge of the start bit, and find the next sampling time with its own timer. The bit length is determined by the baud rate used for the communication. In the case of a UART, the baud rate is equal to the number of bits transmitted per second. The transmitter and receiver have to be set up using the same baud rate equally for correct reception. As seen in Figure 1-1, the frame starts with a falling edge. This falling edge generates an initial interrupt (using external interrupt) in the receiver. The interrupt starts Timer/Counter0, which is set to time out in exactly 1.5 bit lengths. This 1.5 bit length delay is required to generate the next sampling event in the middle of the first data bit. The next 8 interrupts are generated after a predefined delay (1 bit length) using counter/timer 0. Figure 2-1 shows the flowchart for receiving serial data. Transmitting data is even easier, as all bits have equal length and the timer can be preset at a constant delay (1 bit-length). The first bit is the start-bit. This is always a logical zero (or space). This bit informs the receiver that data is coming. Then the data bits can be shifted out, LSB first (least significant bit) first, MSB last. Finally, the last bit must be a stop bit so the receiver can separate the data bytes. This is always a logical one (or mark). Figure 2-2 shows the flowchart for transmitting serial data. Figure 2-1. Flowchart for receiving serial data. WAIT FOR START-BIT WAIT 1.5 BIT LENGTHS SAMPLE DATA BIT WAIT 1 BIT LENGTH INCREMENT BIT-COUNTER BIT-COUNTER < 8? Y N FINISHED BYTE RECEIVED 2 AVR304

AVR304 Figure 2-2. Flowchart for transmitting serial data. SEND START BIT (LOW) WAIT 1 BIT LENGTH SHIFT OUT DATA BIT INCREMENT BIT-COUNTER BIT-COUNTER < 8? Y N SEND THE STOP BIT (HIGH) FINISHED BYTE TRANSMITTED 3 Connection The RS-232 standard requires a voltage level of -15/+15V. To generate these signaling levels, a separate interface circuit is needed which converts the MCU's voltage to the RS-232 voltage. An example of a single chip interface circuit is MAXIM's MAX233. It operates from a single 5V power supply, and has an onchip DC- DC converter to convert the 5 volts to the RS-232 signaling levels. The receive pin (RX) must be connected to the INT0-pin because of the external interrupt. It is not important which pin is used as the transmit pin (TX). Figure 3-1 shows how the MCU should physically be connected to an RS-232 line. Figure 3-1. Physical connection to an RS232 serial line. 4 Implementation These software UART routines use Timer/Counter 0 and one external interrupt. The clock provided to the MCU will limit the maximum baud rate obtainable. This software UART is capable of handling baud rates up to 38.400 kbit/s, at 8MHz clock frequency. At this speed nearly all computing power is used, but the MCU is still available for other tasks between each byte being transmitted. 3

The bit length is determined by the number of cycles (C N) required to generate another overflow. With the Timer/Counter. N is the value loaded in the timer/counter compare register, and C is the Timer/Counter 0 prescaling factor, as described in the T/C Prescaler in the AVR datasheet. The value N can be calculated from the following formula, where Xtal is the frequency of the system: N = Xtal BaudRate C Note that the prescaling factor C should be one of the values 1, 8, 64, 256, or 1024. 5 initsoftwareuart( ) FUNCTION INITIALIZE UART Before data can be transferred using the UART, the UART has to be initialized by calling the function initsoftwareuart(). This function will set up the Timer/Counter prescaler, and enable the Timer/counter and external interrupt needed for communication. Upon return from the subroutine, a ' enable_interrupt()' instruction should follow to enable global interrupts. This will enable the UART. By issuing a ' disable_interrupt()' instruction at a later time, the UART can be disabled. Figure 5-1. initsoftwareuart( ) Flow Chart. 6 putchar(const unsigned char) FUNCTION - TRANSMITTING A BYTE This routine is used when the software needs to transmit data. It waits for the IDLE state, then sets it to TRANSMIT, disables the external interrupt (to disable reception when transmitting), sets the correct baud rate (t/c0 interrupt) and outputs the start bit. 4 AVR304

AVR304 Figure 6-1. putchar Flow Chart. 7 state enumerate The state enum is used by all functions. It has five states implemented: 1. IDLE: the system is in idle state and both receiving and transmitting is possible. 2. TRANSMIT: transmitting bits. INT0 interrupts are disabled, system can not receive. 3. TRANSMIT_STOP_BIT: state for stop bit. This is to ensure at least one stop bit is transmitted. After one stop bit is transmitted, the system state is set to IDLE. 4. RECEIVE: receiving bits. When interrupted on INT0, the system changes state to RECEIVE, and receives 8 bits before changing state to DATA_PENDING. No sending can occur at the same time. 5. DATA_PENDING: when done receiving. Received data is stored in SwUartRXData and are ready to be processed. After data is processed the state should be set to IDLE. This state does not stop new receptions from occurring, so the data may be lost if not processed right away. State transition is shown in Figure 7-1. 5

Figure 7-1. State transition diagram. 8 Timer0_Compare_interrupt( ) INTERRUPT SERVICE ROUTINE This routine takes care of sending and receiving each bit in the transmission. The routine is called automatically on Timer/Counter compare match, to send or receive the next bit. The Timer/Counter overflow interrupt is enabled by putchar or Extern0_interrupt, when transmitting or receiving the start bit respectively. The routine handles the next bit, before the routine exits. If the bit handled was the stop bit, the Timer/Counter overflow interrupt is disabled, and the external interrupt is again enabled. 9 Extern0_interrupt( ) INTERRUPT SERVICE ROUTINE The external interrupt 0 is active whenever the UART is idle. Upon an external interrupt, the Extern0_interrupt( ) routine is called. This routine initiates the reception of serial data (an alternative function name would be: uart_reception ). An external interrupt occurs on a falling edge on the 'INT0 pin (a falling edge marks the beginning of the start-bit see Figure 1-1). This activates the Timer/Counter overflow interrupt and generates a 1.5 bit delay for the first start bit. Before exiting, the external interrupt is disabled to prevent falling edges in the incoming byte from reinitializing the receiver. 6 AVR304

AVR304 Figure 9-1. Extern0_interrupt Flow Chart. Extern0_interrupt state = RECEIVE DISABLE EXTERNAL INTERRUPT RESET COUNTER TIMER 0 SET COMPARE VALUE TIMER 0 SwUartRXBitCount = 0 CLEAR TIMER INTERRUPT BITS ENABLE TIMER INTERRUPTS RETURN 10 Example Program 11 Summary There is an example program included in this application note. The program will wait for a character. Upon reception, the software UART returns the message: 'atmel avr' if a was sent. Atmel avr if A was sent and Unknown command for all other cases. In this application note, a software UART has been implemented. The MCU is capable of using 38400 baud at 8 MHz clock frequency. The UART is initialized by calling initsoftwareuart() and enabling global interrupts. If the UART is idle, it will automatically receive incoming data. To transmit data, a subroutine called putchar() is called with the data passed to the function as an unsigned char. 7

Disclaimer Headquarters International Atmel Corporation 2325 Orchard Parkway San Jose, CA 95131 USA Tel: 1(408) 441-0311 Fax: 1(408) 487-2600 Atmel Asia Room 1219 Chinachem Golden Plaza 77 Mody Road Tsimshatsui East Kowloon Hong Kong Tel: (852) 2721-9778 Fax: (852) 2722-1369 Atmel Europe Le Krebs 8, Rue Jean-Pierre Timbaud BP 309 78054 Saint-Quentin-en- Yvelines Cedex France Tel: (33) 1-30-60-70-00 Fax: (33) 1-30-60-71-11 Atmel Japan 9F, Tonetsu Shinkawa Bldg. 1-24-8 Shinkawa Chuo-ku, Tokyo 104-0033 Japan Tel: (81) 3-3523-3551 Fax: (81) 3-3523-7581 Product Contact Web Site www.atmel.com Technical Support avr@atmel.com Sales Contact www.atmel.com/contacts Literature Request www.atmel.com/literature Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL S TERMS AND CONDITIONS OF SALE LOCATED ON ATMEL S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Unless specifically provided otherwise, Atmel products are not suitable for, and shall not be used in, automotive applications. Atmel s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. 2008 Atmel Corporation. All rights reserved. Atmel, logo and combinations thereof, and others, are the registered trademarks or trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.