buttons with interrupts

This commit is contained in:
Christoph J. Scherr 2023-10-27 11:59:35 +02:00
parent cc97a76e16
commit 148da01344
1 changed files with 25 additions and 10 deletions

View File

@ -1,21 +1,36 @@
/*
* Control PB5 with the two buttons. One button disables, the other enables.
* The board uses interrupts for this.
*/
#define F_CPU 16000000UL #define F_CPU 16000000UL
#define __AVR_ATmega328P__ #define __AVR_ATmega328P__
#include <avr/interrupt.h>
#include <avr/io.h> #include <avr/io.h>
int main(void) { // code for INT0
DDRB = 0xff; ISR(INT0_vect) { PORTB |= (1 << PORTB5); }
DDRD = 0;
PORTD |= (1 << PORTD2) | (1 << PORTD3); // code for INT1
ISR(INT1_vect) { PORTB &= ~(1 << PORTB5); }
int main(void) {
DDRB = 0xff; // write to B
DDRD = 0; // read from D
PORTD |= (1 << PORTD2) | (1 << PORTD3); // pull up D2 and D3
PORTB = 0; PORTB = 0;
// enable external interruprs
EIMSK |= (1 << INT0);
EIMSK |= (1 << INT1);
// trigger when anything changes
EICRA |= (1 << ISC00); // for INT0
EICRA |= (1 << ISC10); // for INT1
sei(); // interrupt magic macro
while (1) { while (1) {
if (!(PIND & (1 << PIND2))) { // do nothing
PORTB |= (1 << PORTB5);
}
if (!(PIND & (1 << PIND3))) {
PORTB &= ~(1 << PORTB5);
}
} }
} }