8. Consider a push button switch SwO connected to pin INT0 external interrupts o
ID: 2291421 • Letter: 8
Question
8. Consider a push button switch SwO connected to pin INT0 external interrupts of an ATMega8 AVR. 8 LEDS are connected to port PB0 PB7 on the same AVR. These LEDS are connected such a lit LED displays that the output port is on logic level 1. On start-up, TASKl is executed and continue indefinitely. If the push-button switch SW0 is pressed, TASKI is paused and TASK2 is carried out after which TASKl is resumed. TASK1- A roll action is performed using the LEDs: The first LED is lit and roll down to the last LED then back to the first LED. This operation is done indefinitely. TASK2- ALL the LEDs blink five (5) times » Write a complete C program that implements the desired tasksExplanation / Answer
Answer :- The code is written below-
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 4000000UL
#include <util/delay.h>
#define DataPort PORTB // Using PortB as our Dataport
#define DataDDR DDRB
//Interrupt Service Routine for INT0
ISR(INT0_vect)
{
unsigned char i, temp;
_delay_ms(500); // Software debouncing control
temp = DataPort; // Save current value on DataPort
/* This for loop blink LEDs on Dataport 5 times*/
for(i = 0; i<5; i++)
{
DataPort = 0x00;
_delay_ms(500); // Wait 0.5 seconds
DataPort = 0xFF;
_delay_ms(500); // Wait 0.5 seconds
}
DataPort = temp; //Restore old value to DataPort
}
int main(void)
{
DDRD = 1<<PD2; // Set PD2 as input (Using for interupt INT0)
PORTD = 1<<PD2; // Enable PD2 pull-up resistor
DataDDR = 0xFF; // Configure Dataport as output
DataPort = 0x01; // Initialise Dataport to 1
GICR = 1<<INT0; // Enable INT0
MCUCR = 1<<ISC01 | 1<<ISC00; // Trigger INT0 on rising edge
sei(); //Enable Global Interrupt
while(1)
{
if(DataPort >= 0x80)
DataPort = 1;
else
DataPort = DataPort << 1; // Shift to the left
_delay_ms(500); // Wait 5 seconds
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.