1

I'm trying to set up timer 1 to run an isr in CTC mode. no matter what i set OCR1A to it always executes at the same frequency and if i print out TCNT1 I get values higher than OCR1A.

here is my Code

    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();
    }

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
}

I'm using this to drive a stepper motor so using PWM mode wont work.

2

It sounds as though timer 1 has not in fact been set to wavegen mode 4. You can ensure that it is by also clearing the other bits of WGM1:

TCCR1A &= ~(_BV(WGM10) | _BV(WGM11));
TCCR1B &= ~_BV(WGM13);
  • 1
    I normally assign to TCCR1A and TCCR1B rather than "or". That way I know it is set to a fixed value. – Nick Gammon Jul 25 '15 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.