add beep
This commit is contained in:
parent
148da01344
commit
dc31d518c1
44
beep/main.c
44
beep/main.c
|
@ -1,21 +1,51 @@
|
|||
/*
|
||||
* Control PB5 with the two buttons. One button disables, the other enables.
|
||||
* The board uses interrupts for this. PB5 will quickly oscilate between on and
|
||||
* off to make cool sounds with a summer.
|
||||
*/
|
||||
#define F_CPU 16000000UL
|
||||
#define __AVR_ATmega328P__
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
int main(void) {
|
||||
DDRB = 0xff;
|
||||
DDRD = 0;
|
||||
int beep = 0;
|
||||
int sound = 0;
|
||||
|
||||
PORTD |= (1 << PORTD2) | (1 << PORTD3);
|
||||
// code for INT0
|
||||
ISR(INT0_vect) { beep = 1; }
|
||||
|
||||
// code for INT1
|
||||
ISR(INT1_vect) { beep = 0; PORTB &= ~(1 << PORTB5); sound = 0;}
|
||||
|
||||
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;
|
||||
|
||||
// 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) {
|
||||
if (!(PIND & (1 << PIND2))) {
|
||||
if (beep) {
|
||||
PORTB |= (1 << PORTB5);
|
||||
}
|
||||
if (!(PIND & (1 << PIND3))) {
|
||||
_delay_us(sound);
|
||||
PORTB &= ~(1 << PORTB5);
|
||||
_delay_us(sound);
|
||||
sound++;
|
||||
if (sound > 1000) {
|
||||
sound = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue