Skip to main content
Arduino

Return to Answer

Clear TCCR1A
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

The Arduino core library configures the Timer 1 for phase-correct PWM at about 490 Hz. Then you are setting some configuration bits in the timer's control registers without clearing the bits that had previously been set by the Arduino core. If you want to use the timer for your own purposes, and configure it by yourself, you should completely overwrite the core's configuration with your own. For that, use the = operator rather than |=, as in:

OCR1A = 99; //Count to 400us
TIMSK1 = (1 << OCIE1A); //Enable interrupt
TCCR1A = 0;
TCCR1B = (1 << WGM12) //Mode 4, CTC
 | (1 << CS11) | (1 << CS10); //Prescaler 64

The Arduino core library configures the Timer 1 for phase-correct PWM at about 490 Hz. Then you are setting some configuration bits in the timer's control registers without clearing the bits that had previously been set by the Arduino core. If you want to use the timer for your own purposes, and configure it by yourself, you should completely overwrite the core's configuration with your own. For that, use the = operator rather than |=, as in:

OCR1A = 99; //Count to 400us
TIMSK1 = (1 << OCIE1A); //Enable interrupt
TCCR1B = (1 << WGM12) //Mode 4, CTC
 | (1 << CS11) | (1 << CS10); //Prescaler 64

The Arduino core library configures the Timer 1 for phase-correct PWM at about 490 Hz. Then you are setting some configuration bits in the timer's control registers without clearing the bits that had previously been set by the Arduino core. If you want to use the timer for your own purposes, and configure it by yourself, you should completely overwrite the core's configuration with your own. For that, use the = operator rather than |=, as in:

OCR1A = 99; //Count to 400us
TIMSK1 = (1 << OCIE1A); //Enable interrupt
TCCR1A = 0;
TCCR1B = (1 << WGM12) //Mode 4, CTC
 | (1 << CS11) | (1 << CS10); //Prescaler 64
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

The Arduino core library configures the Timer 1 for phase-correct PWM at about 490 Hz. Then you are setting some configuration bits in the timer's control registers without clearing the bits that had previously been set by the Arduino core. If you want to use the timer for your own purposes, and configure it by yourself, you should completely overwrite the core's configuration with your own. For that, use the = operator rather than |=, as in:

OCR1A = 99; //Count to 400us
TIMSK1 = (1 << OCIE1A); //Enable interrupt
TCCR1B = (1 << WGM12) //Mode 4, CTC
 | (1 << CS11) | (1 << CS10); //Prescaler 64

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