Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Timer Prescaler problem

I am working on a project which requires use of 16bit timer . I am using Arduino Uno (Atmega 328p) board and Timer_1 (16 bit timer ) in Overflow mode. i want to use 1024 prescaler settings.

According to the datasheet of the controller i have to set [b]CS10 and CS12[/b] bits to use 1024 prescaler but the problem is OverFlow interrupt routine is never fired when 1024 or 256 prescaler is selected .

The Following code works just fine with Prescaler set to 8 or 64.

#include <inttypes.h> 
volatile uint8_t sCount = 0;
void setup()
{
 Timer_one_16_init(); // configure the timer 
 pinMode(13,OUTPUT);
}
void loop()
{
 // do nothing 
}
void Timer_one_16_init(){
 TCCR1A = 0;
 // set the pre-scaler to 1024 (slowest)
 TCCR1B |= (1<<CS12)|(1<<CS10);
 // set the bottom / starting value
 TCNT1 = 0x00;
 // set Overflow interrupt
 TIMSK1 |= (1<<TOIE1);
 // enable global interrupt 
 sei();
}
ISR(TIMER1_OVF_vect){
 sCount++;
 if (sCount == 60)
 {
 digitalWrite(13,!digitalRead(13));
 sCount = 0;
 }
}

i have no idea why this is happening. i have done everything which is given in the datasheet .

Am i doing something wrong ?

Thanks in advance

Answer*

Draft saved
Draft discarded
Cancel
5
  • Thanks it solved my problem. i have just checked the default value of TCCR1B . it is 0x33 . But in the datasheet default value of TCCR1B = 0x00. i was expecting the value of CS11 bit to be 0. Commented Aug 18, 2017 at 15:33
  • " i have just checked the default value of TCCR1B . it is 0x33 ." that means the code is setting WGM13. With the proposed "beginner" approach, WGM13 is cleared. To the extent that your code uses anything that requires WGM13 being set, like PWM, it wouldn't work. Commented Aug 18, 2017 at 16:54
  • "But in the datasheet default value of TCCR1B = 0x00. i was expecting the value of CS11 bit to be 0." you should never rely on the default values. and your code should only change the registers / bits to the extent that you need. For example, if you want to change prescalers, change only those registers / bits that are related to the registers / bits that need to be changed. Otherwise, your code may have unexpected impact on other parts of your code. Commented Aug 18, 2017 at 16:56
  • It is because of that, the code proposed to you is very dangerous: it is a bomb that will explode in your face when the conditions are "unexpectedly" right. Commented Aug 18, 2017 at 16:57
  • @parasbhanot: "But in the datasheet default value of TCCR1B = 0x00". This only applies after a reset. You can rely on this default if you do not use a bootloader nor the Arduino core. The Uno's bootloader does its best to leave every register at its reset state, but it could be unwise to rely on this. And if you use the main() from Arduino core (the "normal" Arduino practice, where you write setup() and loop() but not your own main()), then many registers will end up in the Arduino's default setting, which is not the same as the hardware-defined default. Commented Aug 18, 2017 at 19:32

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /