/* button.c inbuilt LED is On "1" when button connected to PD2-pin is pressed "0" */ // Arduino UNO R3 // AVR_ATmega328P #define F_CPU 16000000UL #include // register names-addresses and bit names-numbers int main() { DDRD &= ~( 1 << PD2 ); // PD2 "0" direction is input PORTD |= ( 1 << PD2 ); // "1" activate button pullup resistor DDRB |= ( 1 << PB5 ); // PB5 "1" LED is output PORTB |= ( 1 << PB5 ); // LED "1" is on PORTB &= ~( 1 << PB5 ); // LED "0" is off while(1) { if( ( PIND & ( 1 << PD2 ))==0 ) PORTB |= ( 1 << PB5 ); // Button pressed "0" LED "1" on else PORTB &= ~( 1 << PB5 ); // LED "0" off } return 0; // never reached } /* *********************************** */ /* HARDWARE */ /* *********************************** */ /* Chip ATMega328 Arduino Uno R3 stackable header _______ Digital: _____/ \__ Analog: ______________ ______________ -|D00 >RXD A5|- | \/ | -|D01 -|D02 A3|- -(D00)-|02 PD0/RXD SDA/PC4 27|-(A4)- -|D03~ A2|- -(D01)-|03 PD1/TXD PC3 26|-(A3)- -|D04 A1|- Butt ->-(D02)-|04 PD2/INT0 PC2 25|-(A2)- -|D05~ A0|- -(D03)-|05 PD3/INT1/PWM PC1 24|-(A1)- -|D06~ | Power: -(D04)-|06 PD4 PC0 23|-(A0)- -|D07 Vin|- +5V ---|07 VCC GND 22|--- Gnd | GND|--- GND Gnd ---|08 GND AREF 21|--- Vin -|D08 GND|- Xtal |X|--|09 PB6/OSC1 AVCC 20|--- +5V -|D09~ +5V|--- +5V 16MHz |X|--|10 PB7/OSC2 SCK/PB5 19|-(D13)->- LED -|D10~ +3.3V|- -(D05)-|11 PD5/PWM MISO/PB4 18|-(D12)- -|D11~ Res|- -(D06)-|12 PD6/PWM PWM/MOSI/PB3 17|-(D11)- -|D12 IOREF|- -(D07)-|13 PD7 PWM/SS'/PB2 16|-(D10)- LED -<-|D13 LED --- | -(D08)-|14 PB0 PWM/PB1 15|-(D09)- -|GND | |______________________________| -|AREF | -|SCL | -|SDA | |________________| */