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.

asked Jul 25, 2015 at 18:00

1 Answer 1

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);
answered Jul 25, 2015 at 20:18
1
  • 1
    I normally assign to TCCR1A and TCCR1B rather than "or". That way I know it is set to a fixed value. Commented Jul 25, 2015 at 21:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.