Interrupt Driven UART Serial Communication for Atmel AVR
- Posted by admin
- on Feb, 26, 2013
- in Atmel AVR, Electronics, General Know How, UART
- Blog 1 Comment.
What are Interrupts ?
Consider that a teacher is instructing a Class. He/She wants to make sure that at all times everyone understands what he/she is explaining. He has two choices to ensure this:
- Ask every student in class if he/she has understood the concept.
- Any student raises his/her hand and asks him when he/she has a doubt.
So the thing is, the first of these two alternatives is called polling, Where Teacher asks or Polls each and every student, whereas the second method is called interruption where the student who wants teachers attention can ask for it by interrupting the teacher. So which one is better ??? Of course first approach is very tedious and time consuming.
In above consideration, lets take teacher as CPU which provide resources to his students (Peripheral devices).So the approach of interruption is much better in most of the cases where CPU can be utilized in more efficient manner
Upon receiving interrupt signal from the peripheral device, CPU stop doing its current routine and jumps to a different code segment or we can say it as an “Interrupt Service Routine”. ISR contains the tasks to be handled by CPU whenever it acknowledge some interrupt signal.
Why Interrupt Driven Serial Communication?
Once you have understood the basic concept of Interrupts, Lets try to understand why it is needed to perform Interrupt driven communication. In Two way communication each of the communication device can either transmit or receive at any particular time. Now, If the my micro controller wants to transmit something to PC, It can do so without any need of interrupt because When it sends, it sends and it goes out to the other end. Though AVR devices have Transmit complete Interrupt also but that is not of much use most of the times, Important is Receive complete interrupt.
Let assume that at any particular time the CPU of my micro controller is busy writing some data onto LCD display, at that same moment some Data Byte came onto Serial Port RXD pin and the data came onto the bus needs immediate attention as it may affect further routines of program, But wait a minute how do i came to know that something is coming Over USART RXD line ? I was busy talking to the LCD ! That is where RX completion interrupt comes into picture. If you use Interrupt driven Serial Communication, whenever some data comes over RXD line the USART peripheral generates an Interrupt request to the CPU and CPU stops doing whatever it is doing and puts it attention to the USART module. That way we don’t miss any important information coming at random time. Similar kind of concept apply to all different kind of interrupts.
Demo Code for Atmel AVR
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <compat/deprecated.h>
#define UART_BAUD_SELECT(baudRate,xtalCpu) (((xtalCpu) + 8UL * (baudRate)) / (16UL * (baudRate)) -1UL)
char ret;
void UART_TxChar(unsigned char data)
{
while(!(UCSRA & (1<<UDRE)));
UDR = data;
}
void UART_TxStr(const char *str)
{
int a=0;
while(str[a]!='\0')
UART_TxChar(str[a++]);
}
void UART_init(unsigned int baud)
{
UCSRA = 0;
UCSRB = (1<<TXEN) | (1<<RXEN) | (1<<RXCIE);
UCSRC = 1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0 ;
UBRRL = (unsigned char)UART_BAUD_SELECT(baud,F_CPU);
UBRRH = (unsigned char)(UART_BAUD_SELECT(baud,F_CPU)>>8);
}
int main(void)
{
sbi(DDRB, 0);
cbi(PORTB,0);
UART_init(9600);
sei();
while(1)
{
}
}
ISR(USART_RXC_vect)
{
ret = UDR;
if(ret=='a')
sbi(PORTB, 0);
else
cbi(PORTB, 0);
ret++;
UDR = ret;
}
Comments & Responses
Recent Posts
- Make In India - What it means to Indian Maker’s Community ?
- Breadboard Power Supplies - Read before you Buy One
- Product Review : Discover Wi-Fi extension board for stm32f4 discovery
- Product Review: OKI78SR-5/1.5 Switching Regulator from muRata Power Solutions
- Product Review - STM8S- Discovery by ST Microelectronics
- Know How : Using GSM Modem - Part 2/2
- Know How : Using GSM Modem - Part 1/2
- ST Microelectronics launches STM32 Nucleo Series
- SW18020P Vibration Switch Testing “Shake Switch”
- TCM8230MD Camera Breakout Board PCB, Can also mount AT45DB Series Flash Memory Chip
- IC Packages of Many Kinds - A comprehensive list
- Keil project setup for STM32F4 Discovery
- DIY - 12V 7.2Ah SMF Battery Charger Circuit
- Lonely Night Projects : DIY PMMC and Series Motor Model
- Toys from Trash with Arvind Gupta
- BodyCom Technology by Microchip for Embedded Security & Authentication
- Programmers for Atmel AVR Microcontrollers
- Nikola Tesla - The Greatest Inventor Ever Lived
- Crystal, Resonator and Oscillators, what’s the difference ?
- Pitfalls in STM32 Standard Peripheral Library & Possible Solutions
- Interrupt Driven UART Serial Communication for Atmel AVR
- DIY PICkit2
- What Thomas Edison expected job candidates to know !
- What does ‘AVR’ stands for ?
- Interfacing MCP4725, 12 bit DAC with AVR Microcontroller #avrPrayog
- Interfacing AT93C46 SPI EEPROM with avrPrayog
- How to recover the Unallocated space on Your SD card due to Raspberry Pi OS
- Driving an Alphanumeric LCD with avrPrayog
- Using Arduino/Freeduino as AVR ICSP Programmer
- DoCircuits : Turn your Web Browser into Prototyping Desk
Things You find here
- ADC Interfacing (1)
- Analog Design (4)
- Analog Designs (7)
- Arduino (9)
- ARM Microcontroller (12)
- ARM7 (6)
- Atmel AVR (8)
- Battery Chargers (2)
- Camera Interfacing (1)
- Display (1)
- EAGLE (3)
- EEPROM (2)
- Electrical Enginnering (6)
- Electronics (28)
- General Interest (15)
- General Know How (25)
- GSM Modem (2)
- Hello World (4)
- I2C (2)
- Interfacing Tutorial (15)
- Lonely Night Projects (4)
- LPC21xx (4)
- Make (4)
- mbed (1)
- Microchip (1)
- Microchip PIC (1)
- NXP (4)
- Open Source Ecology (2)
- Open Source Hardware (9)
- PCB Designing (2)
- Physical Computing (5)
- PIC Related (1)
- Power Supply (5)
- Product Review (2)
- Raspberry PI (1)
- Repair Tricks (2)
- SPI (2)
- ST Micro (8)
- STM32 (8)
- STM32F4 Doscovery (8)
- stm8 bit (1)
- Switch Mode Power supply (2)
- Technology (3)
- UART (3)
- Uncategorized (1)
Recently Said
- st1114g on How to recover the Unallocated space on Your SD card due to Raspberry Pi OS
- KpT, on How to recover the Unallocated space on Your SD card due to Raspberry Pi OS
- Neomanderx3 on I2C EEPROM Interfacing with STM32F4 Discovery
- Gábor on DIY PICkit2
- sapher on I2C EEPROM Interfacing with STM32F4 Discovery
- Tonino on DIY PICkit2
- nate on How to recover the Unallocated space on Your SD card due to Raspberry Pi OS
- mahd on TCM8230MD Camera Breakout Board PCB, Can also mount AT45DB Series Flash Memory Chip
- Andrew on How to recover the Unallocated space on Your SD card due to Raspberry Pi OS
- pavan on DIY - 12V 7.2Ah SMF Battery Charger Circuit




great tutorial, nice work.