Skip to main content
Arduino

Return to Answer

Specify the appropriate WGM mode.
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

On the Uno (ATmega328P microcontroller), timers 0 and 2 are 8-bit only: they cannot count beyond 255. Timer 1, on the other hand, is 16-bits. This is the most convenient timer for generating low frequencies.

Speaking of frequencies... One LED period involves two toggles (turn on, then turn off). Thus, if you want the LED to blink at 5 Hz, the toggle action should happen at 10 Hz. Given that the CPU is clocked at 16 MHz, this means you have to toggle the LED every 1,600,000 CPU cycles.

If you set the prescaler to 1,024, you then you have to count 1,600,000/1,024 = 1,562.5 timer cycles. You cannot count a non-integer number of cycles, but you probably do not care about such accuracy. If you do care, set the prescaler to 256 and count 1,600,000/256 = 6,250 timer cycles by setting the compare match register to 6,249.

One last issue: keep in mind that, in normal counting mode, the timer counts up to its maximum value (65,535, or 255 on an 8-bit timer) and then restarts at zero. If you want it to count only up the value set in the compare match register, you should choose anthe appropriate waveform generation mode: CTC orthe fast PWM mode that lets you control the TOP value with OCR1A.

On the Uno (ATmega328P microcontroller), timers 0 and 2 are 8-bit only: they cannot count beyond 255. Timer 1, on the other hand, is 16-bits. This is the most convenient timer for generating low frequencies.

Speaking of frequencies... One LED period involves two toggles (turn on, then turn off). Thus, if you want the LED to blink at 5 Hz, the toggle action should happen at 10 Hz. Given that the CPU is clocked at 16 MHz, this means you have to toggle the LED every 1,600,000 CPU cycles.

If you set the prescaler to 1,024, you then you have to count 1,600,000/1,024 = 1,562.5 timer cycles. You cannot count a non-integer number of cycles, but you probably do not care about such accuracy. If you do care, set the prescaler to 256 and count 1,600,000/256 = 6,250 timer cycles by setting the compare match register to 6,249.

One last issue: keep in mind that, in normal counting mode, the timer counts up to its maximum value (65,535, or 255 on an 8-bit timer) and then restarts at zero. If you want it to count only up the value set in the compare match register, you should choose an appropriate waveform generation mode: CTC or fast PWM.

On the Uno (ATmega328P microcontroller), timers 0 and 2 are 8-bit only: they cannot count beyond 255. Timer 1, on the other hand, is 16-bits. This is the most convenient timer for generating low frequencies.

Speaking of frequencies... One LED period involves two toggles (turn on, then turn off). Thus, if you want the LED to blink at 5 Hz, the toggle action should happen at 10 Hz. Given that the CPU is clocked at 16 MHz, this means you have to toggle the LED every 1,600,000 CPU cycles.

If you set the prescaler to 1,024, you then you have to count 1,600,000/1,024 = 1,562.5 timer cycles. You cannot count a non-integer number of cycles, but you probably do not care about such accuracy. If you do care, set the prescaler to 256 and count 1,600,000/256 = 6,250 timer cycles by setting the compare match register to 6,249.

One last issue: keep in mind that, in normal counting mode, the timer counts up to its maximum value (65,535, or 255 on an 8-bit timer) and then restarts at zero. If you want it to count only up the value set in the compare match register, you should choose the appropriate waveform generation mode: the fast PWM mode that lets you control the TOP value with OCR1A.

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

On the Uno (ATmega328P microcontroller), timers 0 and 2 are 8-bit only: they cannot count beyond 255. Timer 1, on the other hand, is 16-bits. This is the most convenient timer for generating low frequencies.

Speaking of frequencies... One LED period involves two toggles (turn on, then turn off). Thus, if you want the LED to blink at 5 Hz, the toggle action should happen at 10 Hz. Given that the CPU is clocked at 16 MHz, this means you have to toggle the LED every 1,600,000 CPU cycles.

If you set the prescaler to 1,024, you then you have to count 1,600,000/1,024 = 1,562.5 timer cycles. You cannot count a non-integer number of cycles, but you probably do not care about such accuracy. If you do care, set the prescaler to 256 and count 1,600,000/256 = 6,250 timer cycles by setting the compare match register to 6,249.

One last issue: keep in mind that, in normal counting mode, the timer counts up to its maximum value (65,535, or 255 on an 8-bit timer) and then restarts at zero. If you want it to count only up the value set in the compare match register, you should choose an appropriate waveform generation mode: CTC or fast PWM.

lang-cpp

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