Monday, January 31, 2022

Atmega16 external interrupt programming example

Atmega16 has up to 21 interrupt sources. Here I will discuss only the external interrupt of this controller. External interrupt consist of three source sources:

  1. INT0 - External interrupt request 0
  2. INT1 - External interrupt request 1, and
  3. INT2 - External interrupt request 2.

First I will show how to use INT0 source. It has four sense that determine whether the interrupt is triggering. It is the Interrupt Sense Control register bits of the MCU Control Register (MCUCR).

Atmega16 external interrupt programming example
MCU Control Register (MCUCR)

Atmega16 external interrupt programming example
 Interrupt Sense Control register bits

The General Interrupt Control Register (GICR) turn on and off these external interrupt source, including the INT0 source.

Atmega16 external interrupt programming example
General Interrupt Control Register – GICR

They also have their corresponding interrupt flag in the General Interrupt Flag Control (GIFR).

Atmega16 external interrupt programming example
General Interrupt Flag Control (GIFR)

There are three bit corresponding to INTF1, INTF0, and INTF2. These bits will be cleared after the Interrupt Service Routine (ISR) executed. Optionally the programmer can clear any flag by setting it. However it is uncommon from most of microcontrollers.

In C programming the interrupt is very easy to program that I will show it in this programming example.

In this example I use only the INT0 source. The interrupt sense is the falling edge of INT0, as I will select it in program. Each time the INT0 occurs, and output LED connects to PD7 will toggle.

Atmega16 external interrupt programming example
Schematic Diagram

The source code is very simple. It contains a little lines of code.

  1. /*
  2.  * int0Example1.c
  3.  *
  4.  * Created: 5/27/2022 6:45:11 PM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #include <avr/interrupt.h>
  11.  
  12. int main(void)
  13. {
  14. //PortD output
  15. DDRD=0xFF;
  16. //RD2 input
  17. DDRD&=~(1<<2);
  18. //Turn on RD2
  19. PORTD|=(1<<2);
  20. //Select falling edge of INT0
  21. MCUCR|=(1<<ISC01);
  22. //Enable INT0 request
  23. GICR|=(1<<INT0);
  24. //Enable interrupt
  25. sei();
  26. //Clear Flag
  27. GIFR|=(1<<INTF0);
  28. while (1)
  29. {
  30. }
  31. }
  32.  
  33. //Interrupt Service Routine - ISR
  34. ISR(INT0_vect){
  35. //Toggle PD7
  36. PORTD^=0x80;
  37. }
  38.  

Click here to download it source file.

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)