40 lines
853 B
C
40 lines
853 B
C
|
|
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include "mystd.h"
|
|
#include "utils.h"
|
|
|
|
#define PRESCALER 1024
|
|
#define TIMER_FREQ (F_CPU / PRESCALER)
|
|
|
|
// at a high level:
|
|
// Set the OC1A (PB1) pin as output
|
|
// set the TIMER1 mode to COMPARE (CTC)
|
|
// say to compare against OC1A
|
|
// set the value to be compated at X count
|
|
// say the presacler for the timer is 512
|
|
//
|
|
// all these information are on page ~140
|
|
void timer1_init(void) {
|
|
// Set PB1 (OC1A) as output
|
|
DDRB |= _BV(PB1);
|
|
|
|
// CTC mode (WGM12 = 1)
|
|
TCCR1B |= _BV(WGM12);
|
|
|
|
// Toggle OC1A on compare match (COM1B0 = 1)
|
|
TCCR1A |= _BV(COM1A0);
|
|
|
|
// Set compare values
|
|
OCR1A = TIMER_FREQ * 2;
|
|
|
|
// Start timer with prescaler 1024 (CS12 | CS10)
|
|
TCCR1B |= _BV(CS12) | _BV(CS10);
|
|
|
|
// set OCR1A interrupt
|
|
TIMSK1 |= _BV(OCIE1A);
|
|
|
|
|
|
// HELLO NO `sei()` SO WE DO IT BY HAND
|
|
SREG |= _BV(SREG_I);
|
|
}
|