Timer 1 AVR328P

Hello,

I would like to use timer 1 to generate a t = 10ms signal (100Hz). I set different registers as I did with timer 2, but the behaviour is very different. Look at my code

void startTimer1B (){
  cli();                                     //stop interrupts
  TCCR1A = 0;                        // set entire TCCR1A register to 0
  TCCR1B = 0;                        // same for TCCR1B
  TCCR1C = 0;                        // same for TCCR1C
  TCCR1A |= (1 << COM1B0);  // Toggle OC1B on Compare Match dans un premier temps pour tester la durée de chaque seuil
  TCCR1A |= (1 << COM1A0);  
  TCCR1A |= (1 << WGM12);   // CTC mode  TOP on OCRA ?????
  OCR1A = 78;
  TCCR1B |= (1 << CS10);       // Clock select bit From prescaler (1024)
  TCCR1B |= (1 << CS12);       // Clock select bit From prescaler (1024)
  
  TIMSK1 |= (1 << OCIE1B);    // Timer 1 Interrupt on compare match B
  sei();                                    //allow interrupts
}

With timer 2 the frequency is ok, but with timer 1 the value OCR1A = 78 isn't take into account, and TCNT1 goes from 0 to 0xffff ? So the result is a 0.1 hertz signal !

Do you understand my mistake ?

I think WGM12 is part of TCCR1B not TCCR1A

CommonRodent:
I think WGM12 is part of TCCR1B not TCCR1A

Great ! You are the best !! :wink: :wink:

Thanks a lot

All three counter/timers in the ATmega328 are different in detail alas...

The nice thing about the ATmega2560 is that it has several identical 16 bit timers
just like timer1 in the '328. Shame the 328's timer2 isn't like timer1 really.

Yes you're right ! But I had read carefully technical documentation !
When you read again your own code, you didn't see mistakes, you need an outside view !

:slight_smile: :slight_smile: