Serial communication with AVR microcontroller using interrupts

Summary of Serial communication with AVR microcontroller using interrupts


In this article the author explains using AVR USART receive interrupts instead of polling to improve efficiency. Enable RXCIE in UCSRB and set the global I-bit in SREG; initialize USART (baud rate, frame format, RXEN/TXEN) and enable RXCIE. When a byte arrives the USART_RXC_vect ISR reads UDR into a variable and can process or echo it by writing back to UDR. This frees the CPU from continually checking the RXC flag and lets interrupts handle incoming serial data.

Parts used in the Serial Reception using AVR USART Interrupts:

  • AVR microcontroller (ATmega16)
  • USB to serial or RS232 interface to PC
  • PC with HyperTerminal or terminal software
  • Power supply for microcontroller
  • Connecting wires and breadboard or PCB
In our previous articles on serial data transmission using AVR microcontroller we have demonstrated serial communication using the polling method. In Polling, the microcontroller waits for the RXC flag (in the case of serial receiver) to go high and then moves to the next instruction. This is not a good programming technique to keep the microcontroller busy to monitor the RXC flag. Alternatively, serial interrupts can be used to transmit and receive data. This increases the efficiency of the code and keeps the processor free for other tasks. The article explains serial data reception using interrupts concepts.It is recommended that the readers should study the article on AVR Interrupts before proceeding.
Serial communication with AVR microcontroller using interrupts
The UCSRB register has RXCIE (Reception Complete Interrupt Enable) bit, which is used to enable the serial reception interrupt. The I-bit of SREG register is must be set to enable global interrupt of ATmega16. The circuit diagram is same as used in previous article of USART.
Code Explanation
Step1: In order to use serial interrupts, the first step will be to initialize USART. In this case we need to add one more step to the initialization done in the polling method. The RXCIE (Receiver Complete Interrupt Enable) bit in UCSRB register is also set high to enable the serial receive interrupt. Every time one byte of the data is received serially, a serial receive interrupt is generated and the control transfers to the corresponding ISR. The RXC flag will go high to indicate the serial receive interrupt.
void usart_init()
{
UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN);   // Turn on the transmission reception ..
// circuitry and receiver interrupt
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value..
// into the low byte of the UBRR register
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value..
// into the high byte of the UBRR register
}
Serial communication with AVR microcontroller using interrupts schematic
Step2: Now define its ISR
When the serial receive interrupt comes, the serial data is present in UDR (no need monitor RXC flag) and needs to be read in a variable. (In this program the data which is received is sent back serially. The controller is connected to PC and the data which is sent is received on the HyperTerminal).
ISR (USART_RXC_vect)
{
unsigned char value;
value = UDR; // Fetch the received byte value into the variable “value”
UDR = value;   //Put the value to UDR
}

Quick Solutions to Questions related to Serial Reception using AVR USART Interrupts:

  • How do you enable serial receive interrupt on ATmega16?
    Set the RXCIE bit in the UCSRB register and enable global interrupts by setting the I-bit in SREG.
  • Can the microcontroller avoid polling the RXC flag when using interrupts?
    Yes, with RXCIE enabled the ISR runs when data arrives so the code does not need to poll the RXC flag.
  • What register contains the received serial data inside the ISR?
    The received byte is available in the UDR register and should be read from UDR inside the ISR.
  • How is an incoming byte echoed back to the sender in the example?
    Inside ISR the code reads value = UDR and then writes UDR = value to send it back.
  • What additional USART initialization step is required for interrupts compared to polling?
    In addition to usual USART setup, set the RXCIE bit in UCSRB to enable receive interrupts.
  • What must be enabled globally for USART interrupts to work?
    The global interrupt enable I-bit in the SREG register must be set.
  • Which USART control registers are set during initialization in the article?
    UCSRB is set to enable RXCIE, RXEN, TXEN; UCSRC is set for 8-bit character size; UBRRL/UBRRH are set for baud rate.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top