0% found this document useful (0 votes)
27 views2 pages

Final Prince Sharma

This code uses an AVR microcontroller to blink three LEDs in sequence when a push button is pressed. It defines pin numbers for the LEDs and button, sets the button pin as an interrupt, and increments a count variable each time the button is pressed. A blink_led function then illuminates the corresponding LED based on the count value before waiting half a second.

Uploaded by

p20rincesharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Final Prince Sharma

This code uses an AVR microcontroller to blink three LEDs in sequence when a push button is pressed. It defines pin numbers for the LEDs and button, sets the button pin as an interrupt, and increments a count variable each time the button is pressed. A blink_led function then illuminates the corresponding LED based on the count value before waiting half a second.

Uploaded by

p20rincesharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Task Code (C language)

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

// Define LED pin numbers


#define LED_PIN_1 PB0
#define LED_PIN_2 PB1
#define LED_PIN_3 PB2

// Define push button pin number


#define BUTTON_PIN PD2 // External Interrupt 0

volatile int count = 0;


void blink_led(int n);

int main(void) {
// INTERNAL INPUT PULLUP RESISTER FOR SWITCH
DDRD &= ~(1 << BUTTON_PIN);
PORTD |= (1 << BUTTON_PIN);

// 3 LEDs
DDRB |= (1 << LED_PIN_1) | (1 << LED_PIN_2) | (1 << LED_PIN_3);

// Configure External Interrupt 0


EICRA |= (1 << ISC01);
EICRA &= ~(1 << ISC00);

EIMSK |= (1 << INT0); // Enable External Interrupt 0

sei(); // Enable global interrupts

while (1)
{
// Main logic of code
}

return 0;
}

ISR(INT0_vect) {
_delay_ms(50); // Code logic for Debouncing
if (!(PIND & (1 << BUTTON_PIN))) {
if (count == 3) {// if 3rd led is on
count = 0;
} else {
count++;
}
blink_led(count);
_delay_ms(500);
}
}

void blink_led(int n) {
switch (n) {
case 0:
PORTB &= ~(1 << LED_PIN_1);
PORTB &= ~(1 << LED_PIN_2);
PORTB &= ~(1 << LED_PIN_3);
break;
case 1:
PORTB |= (1 << LED_PIN_1);
break;
case 2:
PORTB &= ~(1 << LED_PIN_1);
PORTB |= (1 << LED_PIN_2);
break;
case 3:
PORTB &= ~(1 << LED_PIN_2);
PORTB |= (1 << LED_PIN_3);
break;
}
}

Pin Connection
Component Connection Arduino Pin ATmega328P Pin
LED 1 Anode (+) D8 PB0
LED 2 Anode (+) D9 PB1
LED 3 Anode (+) D10 PB2
Push Button One side D2 PD2
Other side GND GND

You might also like