sysprog-basic/timer/main.c

40 lines
867 B
C
Raw Normal View History

2023-10-30 09:47:13 +01:00
/*
* Use the timer to produce a sound in a speaker.
*
* Formular for the frequency:
* (F_CPU/Prescaler)/(wanted_frequency*2)+1
*
* Cut off the decimal part and see which is
* closer to the wanted frequency.
*/
#define F_CPU 16000000UL
#define __AVR_ATmega328P__
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
#define MINUTE 60 * 1000
const int TIME = 45 * MINUTE;
ISR(TIMER0_COMPA_vect) { PORTB ^= (1 << PORTB5); }
int main(void) {
DDRB = 0xff; // write to B
DDRD = 0; // read from D
// Timer 0
// Mode: Overflow, Interrupt
// Prescaler: /256
// 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) {
}
}