Can anyone explain how i can use the timer interrupts on a mega. In detail I want to use timer 0 (because it has an external clock source and Timer 5 is already in use for another function). I need an interrupt on OCR0A compare but don't know how to set the interrupt vector and attach it to my interrupt routine and cannot see any instructions for doing it.
Timer0 is used by the Arduino core to derive millis and micros so taking control of it will likely cause you other problems.
The other timers can also be configured to take and external pin source though it seems only T0 & T5 break out to arduino pins. Can you move the Timer5 code to another timer and use it for the external source?
17.4 Timer/Counter Clock Sources
The Timer/Counter can be clocked by an internal or an external clock source. The clock source
is selected by the Clock Select logic which is controlled by the Clock Select (CSn2:0) bits
located in the Timer/Counter control Register B (TCCRnB). For details on clock sources and
prescaler, see “Timer/Counter 0, 1, 3, 4, and 5 Prescaler” on page 169.
Hi,
Unfortunately I need two timers clocked externally and I am not bothered by losing the time functions as this application does not need them, though if they did I would derive them from other timers that can use the internal clock source.
I could feed the timer output into an external input pin (which I do now from an external timer) but I need an odd number, so having the timer toggle the output is of no use. I have tried to create the timing signal I need externally by creating quadrature phased signals using OCR0A and OCR0B and XOR them but both outputs seem to clear together even though the compare registers are set at different values. So my only solution so far is to use the interrupt on compare match on OCR0A.
I have been trying to read about it and found a timer3 library but there are aspects I do not understand, so I am looking for an idiots guide to explain how to do it.
I have sorted my own problem out. This is so simple, why is it not documented?
#include <avr/io.h> #include <avr/interrupt.h>
volatile unsigned int TBAddr ;
void setup() {
Serial.begin(115200);
//test timer0 for use in timebase address counter
// creating an overflow int and counting in s/w
TCCR0A = B01000010 ;
TCCR0B = B00000010 ; // prescale by 8 for testing
OCR0A = 128 ;
TIMSK0 = B00000010 ; //enable int on compare
DDRB = DDRB | B10000000 ; //OC0A output
}
ISR(TIMER0_COMPA_vect){
TBAddr = ++TBAddr ;
Serial.println(TBAddr,HEX);
}
void loop() {
// put your main code here, to run repeatedly:
It could be made more explicit such as a search for Timer interrupts could have a page that linked to the information rather than leaving people to guess and search through the internet to find references that might be valid.