Thursday, June 16, 2022

Programming All External Interrupt Sources of Atmega16

In previous post, I show a simple programming example of INT0. As it's already stated, the Atmega16 has up to 3 external interrupt sources. In this example, I use all its external interrupt sources, the INT0, the INT1, and the INT2.

I select the low logic level of these interrupt source to trigger the ISR. Whenever any interrupt occurs, its corresponding output LEDs will toggle its logic state. The output LEDs are PB5, PB6, and PB7.

Programming All External Interrupt Sources of Atmega16
Program simulation

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
 * atmega16AllExtIntEx.c
 *
 * Created: 6/10/2022 7:06:23 PM
 * Author : Admin
 */ 

#include <avr/io.h>

#include <avr/interrupt.h>

int main(void)
{
	
	//PD2 and PD3 Input
	DDRD&=~(1<<2);
	DDRD&=~(1<<3);
	//Turn on PD2 and PD3 high
	PORTD|=(1<<2)|(1<<3);
	//PortB output
	DDRB=0xFF;
	//PB2 input
	DDRB&=~(1<<2);
	//Turn PB2 high
	PORTB|=(1<<2);
	//Enable INT0...2 request
	GICR|=(1<<INT0)|(1<<INT1)|(1<<INT2);
	//Enable interrupt
	sei();
	//Clear Flag
	GIFR|=(1<<INTF0);
	while (1)
	{
	}
	return 0;
}

//Interrupt Service Routine - ISR
ISR(INT0_vect){
	//Toggle RB5
	PORTB^=0x20;
}

ISR(INT1_vect){
	//Toggle RB6
	PORTB^=0x40;
}

ISR(INT2_vect){
	//Toggle RB7
	PORTB^=0x80;
}

Click here to download its zip file.

No comments:

Post a Comment

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)