When you enable the communication using the UART. You have the flexibility to either use the Polling or Interrupt method to continue with your programming.
Polling halts the execution of the program and waits for the UART peripheral to receive something so that program execution must continue. But it eats a lot of the computing time.
So, Interrupt Service Routine is written and implemented such the program execution does not stop. It will stop when there is an interrupt and when there is data in the UDR0 register of UART. Then the ISR will execute and then transfer the control to the main program. Which saves a lot of computing time.
you have to add an interrupt library in your program.
#include <avr/interrupt.h>
Then you need to enable the Global interrupt flag.
.
.
.
int main()
{
.
.
.
sei(); // This is Set Enable Interryupt
while(1)
{
// This is your application code.
}
}
Then you need to enable the UART receive complete interrupt. by setting ‘1’ to RXCIE0 bit of USCR0B register.
Write the ISR function which takes “USART0_RX_vect” as the argument.
char Received_char;
ISR(USART0_RX_vect)
{
Received_char = UDR0;
}
int main()
{
UCSR0B = (1 << RXCIE0)|(1<<RXEN0)|(1<<TXEN0);
.
.
.
sei();
while(1);
{
}
}
The above code shows you how to implement UART receive complete ISR. It is not a full initialisation code. You still have to write the UBRR and the frame control to enable the uart peripheral.
Leave a Reply