Timer0 ISR Routine Not Triggering
for led screen project, i'm trying implement bit angle modulation control brightness of leds. part of requires use of interrupts refresh screen , control how long leds on.
i decided use timer0 (and yes know messes delay() , millis()), , able timer running. whatever reason though, cannot seem isr fire. i've read through datasheet '328 , seems flag trigger interrupt automatically set upon compare match tcnt0 , ocr0a. know matching because have oc0a pin toggling , can see waveform on scope, isr still won't go. i've looked @ many many sites online , compared code , should fine far can tell. missed something....can , see if screwed somewhere?
i decided use timer0 (and yes know messes delay() , millis()), , able timer running. whatever reason though, cannot seem isr fire. i've read through datasheet '328 , seems flag trigger interrupt automatically set upon compare match tcnt0 , ocr0a. know matching because have oc0a pin toggling , can see waveform on scope, isr still won't go. i've looked @ many many sites online , compared code , should fine far can tell. missed something....can , see if screwed somewhere?
code: [select]
/*
* mastercode.cpp
*
* created: 5/7/2014 4:09:49 pm
* author: brandon
*/
#include <avr/io.h>
#include "arduino.h"
#include "masterspi.h"
#define refresh 100 //refresh frequency in hertz
volatile int timer[4];
volatile int pos=0;
void initializeinterrupt(int refreshrate)
{
//timer[] corresponds bits 3, 2, 1, 0 respectively bam
//don't worrry part
timer[0] = 1000000/(refreshrate*128);
timer[1] = 1000000/(refreshrate*256);
timer[2] = 1000000/(refreshrate*512);
timer[3] = 1000000/(refreshrate*1024);
//disable global interrupts , clear control registers
cli();
tccr0a = 0;
tccr0b = 0;
//set compare match max (random test val see if working)
ocr0a = 10;
//turn on ctc mode , toggle oc0a upon match (just testing purposes)
tccr0a |= ((1<<wgm01)|(1<<com0a0));
//sets prescaler 1024
tccr0b |= ((1<<cs02)|(1<<cs00));
//enables interrupts comp0a
timsk0 |= (1<<ocie0a);
//enable global interrupts
sei();
}
int main(void)
{
masterspi spi;
spi.setup();
//set intpin output , low
ddrc = (1<<portc2);
portc &= ~(1<<portc2);
//test oc0a...ignore
ddrd |= (1<<portd6);
initializeinterrupt(100);
while(1)
{
//do stuff here...
}
}
isr(timer0_compa_vect)
{
//toggle pin high or low
if(portc2)
portc &= ~(1<<portc2);
else
portc |= (1<<portc2);
/*
//update ocr0a value
pos++;
ocr0a = timer[pos];
if(pos==3)
pos=0;
*/
}
when define main() function, ide doesn't add one. it's main() function calls init(). yours not. significant? quite possibly.
Arduino Forum > Using Arduino > Programming Questions > Timer0 ISR Routine Not Triggering
arduino
Comments
Post a Comment