Write a program using atmel AVR Using easy AVR v7 board IO
Write a program using (atmel AVR) (Using easy AVR v7 board):
- I/O Ports as inputs
Program should monitor all the buttons on PORTB and when a button is pressed, the corresponding LED should light up on the PORTA LEDs. For example, if the button connected to pin PB0 is pressed, then the LED connected to pin PA0 should light up.
Solution
#ifndef F_CPU
#define F_CPU 16000000UL // 16 MHz clock speed
#endif
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRA = 0xFF; //Makes all pins of PORTA output
DDRB = 0x00; //Makes all pins of PORTB input
while(1) //infinite loop
{
if(PINB & (1<<PB0) == 1) //If switch is pressed
{
PORTA |= (1<<PA0); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTA &= ~(1<<PA0); //Turns OFF LED
}
if(PINB & (1<<PB1) == 1) //If switch is pressed
{
PORTA |= (1<<PA1); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTA &= ~(1<<PA1); //Turns OFF LED
}
if(PINB & (1<<PB2) == 1) //If switch is pressed
{
PORTA |= (1<<PA2); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTA &= ~(1<<PA2); //Turns OFF LED
}
if(PINB & (1<<PB3) == 1) //If switch is pressed
{
PORTA |= (1<<PA3); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTA &= ~(1<<PA3); //Turns OFF LED
}
if(PINB & (1<<PB4) == 1) //If switch is pressed
{
PORTA |= (1<<PA4); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTA &= ~(1<<PA4); //Turns OFF LED
}
if(PINB & (1<<PB5) == 1) //If switch is pressed
{
PORTA |= (1<<PA5); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTA &= ~(1<<PA5); //Turns OFF LED
}
if(PINB & (1<<PB6) == 1) //If switch is pressed
{
PORTA |= (1<<PA6); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTA &= ~(1<<PA6); //Turns OFF LED
}
if(PINB & (1<<PB7) == 1) //If switch is pressed
{
PORTA |= (1<<PA7); //Turns ON LED
_delay_ms(3000); //3 second delay
PORTA &= ~(1<<PA7); //Turns OFF LED
}
}
}


