diff --git a/timer2/main.c b/timer2/main.c new file mode 100644 index 0000000..fb0da88 --- /dev/null +++ b/timer2/main.c @@ -0,0 +1,68 @@ +/* + * Use the timer to produce a sound in a speaker. + * + * Formular for the frequency: + * (F_CPU/wanted_frequency*2*Prescaler)+1 + * + * Cut off the decimal part and see which is + * closer to the wanted frequency. + * + * Task: Sum at start with 440 Hz, when a button is pushed, sum at 264 Hz. The + * other button reverses. + */ +#define F_CPU 16000000UL +#define __AVR_ATmega328P__ +#include +#include +#include + +#define MINUTE 60 * 1000 +const int TIME = 45 * MINUTE; + +int state = 0; + +// Timer0 interrupt +ISR(TIMER0_COMPA_vect) { + /* PORTB ^= (1 << PORTB5); */ +} + +// code for INT0 +ISR(INT0_vect) { + PORTB |= (1 << PORTB5); +} + +// 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; // pull down PORT B + + // enable external interruprs + EIMSK |= (1 << INT0); + EIMSK |= (1 << INT1); + + // trigger when anything changes + EICRA |= (1 << ISC00); // for INT0 + EICRA |= (1 << ISC10); // for INT1 + + // Timer 0 + // Mode: Overflow, Interrupt + // Prescaler: /1024 + // max: 70 + TCCR0A |= (1 << WGM01); // CTC Mode + OCR0A = 70; // max value for timer + TIMSK0 |= (1 << OCIE0A); // enable interrupt ISR_COMPA_vect + + sei(); // magic interrupt macro + + TCCR0B |= (1 << CS02); // enable with prescaler 256 + while (1) { + } +}