I'm trying to drive stepper motors using interrupts but I'm having trouble getting the interrupt configured correctly.
Here is my initialization code for the interrupt
void timer1_init()
{
// set up timer with prescaler = 8 and CTC mode
TCCR1B |= (1 << WGM12)|(0 << CS12)|(1 << CS11)|(0 << CS10);
// initialize counter
TCNT1 = 0;
// initialize compare value
OCR1A = 100;
// enable compare interrupt
TIMSK1 |= (1 << OCIE1A);
// enable global interrupts
sei();
}
Here is my ISR
ISR(TIMER1_COMPA_vect){
SET(MOTOR_PORT,LEFT_MOTOR_STEP); //set the pin high
delayMicroseconds(3); //keep high for a bit
CLR(MOTOR_PORT,LEFT_MOTOR_STEP); //set it low
}
No matter what i set OCR1A to it always steps at the same speed.
It isn't resetting TCNT1 like It should (if i'm reading the datasheet correctly).
I verified this by printing TCNT1 in the loop code and I am seeing values greater than 100.
Is my understanding of CTC mode incorrect or did i just miss something that i need to set?
Thanks