03 Experiment
Controlling ON/OFF of an LED using switch
Source and description
http://sincgrid.in/avr-tutorials/avr-tutorial-experiment-3/
http://sincgrid.in/avr-tutorials/avr-tutorial-experiment-3/
// this code sets PD2 to an input with a pull-up disabled
#include <avr/io.h>
int main(void)
{
DDRD &= ~(1 << DDD2); // Clear the PD2 pin
// PD2 is now an input
PORTD |= ~(1 << PORTD2); // turn On the Pull-up
// to see output
/* NANO pin 13 (DDB5), set pin 5 of PORTB for output*/
DDRB |= _BV(DDB5);
while(1)
{
if( (PIND & (1<<PIND2)) > 0)
{
// do something if PD2 is ON
/* NANO pin 13 (DDB5), set pin 5 low to turn led off */
PORTB &= ~_BV(PORTB5);
}else{
// do something if PD2 is OFF
/* NANO pin 13 (DDB5), set pin 5 high to turn led on */
PORTB |= _BV(PORTB5);
}
}
}