When trying to create a timer interrupt you need to use registers to count clock cycles and the process kind of confuses me because this is my first time working with Arduino. Here is what I have so far:
#include <avr/interrupt.h>
const uint16_t PERIOD = ...; // I'm assuming that here I will have to calculate how long a
//clock cycle is and then multiply that by the number of cycles
//I need to reach 33 milliseconds. Am I right?
EMPTY_INTERRUPT(TIMERx_COMPA_vect);
void setup()
{
// Configure Timer x.
TCCRxA = 0; // undo the timer config done...
TCCRxB = 0; // ...by the Arduino core library
TCNTx = 0; // reset the timer
OCRxA = PERIOD - 1; // set the period
TIMSKx = _BV(OCIExA); // enable TIMERx_COMPA interrupt
TCCRxA = ...; // set the counting mode to CTC...
TCCRxB = ...; // ...and set the prescaler
}
void loop()
{
// Do the job that I need to do first
do_the_job();
// Sleep for the remaining time and wait for the interupt.
sleep_mode();
}
According to the tutorial I followed I’m supposed to set the counting mode to CTC and set the prescaller, but I’m not sure what values those will be or how to calculate them. Can anyone help me fill in the missing information? I’m having troulbe understanding the documenation.