In: Electrical Engineering
An LED is connected to PD3 and a Button is connected to PD0. Write a C program that will wait for the button to be pressed and then released (make sure you debounce the press and release). Each time the button is pressed, the LED blinks a number of times corresponding to how many times the button has been pressed and released. For example, when the button is pressed and released for the first time, the LED will blink once; and when the button is pressed and released the second time, the LED will blink twice, etc. The button will be pressed once and release once each time. The LED shows a count (accumulated number of times Button was pressed).
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRD |= (1<<PD3); // Make third pin i.e PD3 as output to
connect LED
DDRD &= ~(1<<PD0); // Make first pin i.e is PD0 as input
to connect Butten Switch
unsigned int count = 0; //
initilize a count variable for make led glow number of time
propotional to switch
unsigned int i;
while(1) // Runs infinite loop
{
if(PIND & (1<<PD0) == 1) //If switch
is pressed
{
count++;
for(i=1;i<=count;i++)
{
PORTD |=
(1<<PD3); //Turns ON LED
_delay_ms(1000); //3 second delay
PORTC &=
~(1<<PD3); //Turns OFF LED
}
}
}
}