Teacher wish that Each student understand Equally

Interrupt Driven UART Serial Communication for Atmel AVR

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:

Teacher wish that Each student understand Equally

  • 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 &amp; (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)&gt;&gt;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;

}
  • vinit

    great tutorial, nice work. :)

  • naqi

    Awesome. (y)
    it helped me alot.
    Thanks buddy