{"id":9767,"date":"2020-11-20T18:16:37","date_gmt":"2020-11-20T18:16:37","guid":{"rendered":"https:\/\/wolles-elektronikkiste.de\/timer-and-pwm-part-1-8-bit-timer0-2"},"modified":"2024-10-29T08:02:02","modified_gmt":"2024-10-29T08:02:02","slug":"timer-and-pwm-part-1-8-bit-timer0-2","status":"publish","type":"post","link":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2","title":{"rendered":"Timer and PWM &#8211; Part 1 (8-Bit Timer0\/2)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">About this post<\/h2>\n\n<p>In this article about timer and pulse width modulation (PWM) I will dive into the depths of the Arduino UNO and the ATmega328P respectively. The Arduino is a great invention that makes it easy to enter the world of microcontrollers. However, the price of simplification is that not all capabilities of the underlying microcontroller have been transferred to the Arduino world. Timer and PWM are definitely part of this. These features are still accessible (with few limitations), but only through the somewhat cryptic logic (bit) operations.<\/p>\r\n\r\n<p>I will try to explain the subject in a comprehensible way with many small examples. As a result, the post has become extremely long &#8211; more of a book chapter than a blog post. But I also want that more inexperienced &#8220;Arduinoists&#8221; understand it. However, to understand this article, you will need some knowledge of logic operations and port manipulation. If you still have some catching up to do, look at <a href=\"https:\/\/wolles-elektronikkiste.de\/logical-operations-and-port-manipulation?lang=en\" target=\"_blank\" rel=\"noopener noreferrer\">my last post<\/a>.<\/p>\r\n\r\n<p>Because of the size, I limit myself to the 8 bit timers in this article. I deal with the 16 bit timer in <a href=\"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-2-16-bit-timer1\" target=\"_blank\" rel=\"noopener noreferrer\">part 2<\/a>.<\/p>\n\n<h3 class=\"wp-block-heading\">Content<\/h3>\n\n<p>So, that&#8217;s what I will talk about:<\/p>\r\n<ul>\r\n \t<li>What are timers and PWM<\/li>\r\n \t<li>Which timer does the Arduino UNO (or ATmega 328 P) have?<\/li>\r\n \t<li>The four timer modes:\r\n<ul>\r\n \t<li>Normal<\/li>\r\n \t<li>CTC (clear timer on compare match)<\/li>\r\n \t<li>Fast PWM<\/li>\r\n \t<li>Phase-correct PWM<\/li>\r\n<\/ul>\r\n<\/li>\r\n \t<li>C &#8211; sketches in Atmel Studio<\/li>\r\n<\/ul>\n\n<h2 class=\"wp-block-heading\">What is a timer, what is PWM?<\/h2>\n\n<p>I start quite simply: first, a counter belongs to a timer. And it does what you would expect &#8211; namely it counts. It counts repeatedly up or down to a limit or up to adjustable limits. The counter becomes a timer when it counts time-dependently. Finally, the timer does not make sense until reaching the limit or an intermediate value can trigger an event.<\/p>\r\n\r\n<p>PWM is simply the technique of periodically switching the digital outputs of your microcontroller HIGH and LOW, i.e. generating square wave signals with certain patterns. And since this is supposed to happen rapidly and in the background, you use timers for it. You have certainly already generated PWM signals, e.g. via analogWrite, servo motor control or the tone function. This is ready-made food, so to speak. Here you will learn how to &#8220;cook&#8221; PWM signals yourself.<\/p>\n\n<h2 class=\"wp-block-heading\">The timers of the Arduino UNO<\/h2>\n\n<p>In this post (and part 2), I&#8217;ll cover the following timers:<\/p>\r\n<ul>\r\n \t<li>Timer0: 8 bit<\/li>\r\n \t<li>Timer1: 16 bit (<a href=\"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-2-16-bit-timer1\" target=\"_blank\" rel=\"noopener noreferrer\">part 2<\/a>)<\/li>\r\n \t<li>Timer2: 8 bits<\/li>\r\n<\/ul>\r\nThere are more specialized timers (e.B. watchdog timer). However, the ones mentioned here are those that are used for PWM and similar purposes.\n\n<h3 class=\"wp-block-heading\">The corresponding counters, control registers and I\/O pins<\/h3>\n\n<h4 class=\"wp-block-heading\">The Timer\/Counter Register TCNTx<\/h4>\n\n<p>Now it&#8217;s getting dry and theoretical. But don&#8217;t worry, the examples will make things clearer. Learn now &#8211; and understand later.<\/p>\r\n\r\n<p>Depending on their size, there are one or two counter registers for each timer, namely TCNT0 (Timer\/Counter 0), TCNT1L, TCNT1H and TCNT2. Since the Timer1 is 16 bit, it needs two registers. In the following, however, I limit myself to the two 8 bit timers.<\/p>\n\n<h4 class=\"wp-block-heading\">The Timer\/Counter Control Registers TCCRxy<\/h4>\n\n<p>The main settings for the timers are made in the Timer\/Counter Control Registers. TCCR0A and TCCR0B belong to Timer0, TCCR2A and TCCR2B belong to Timer2. Here is an example of the Timer2 Control Registers:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2A.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"95\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2A-1024x95.png\" alt=\"TCCR2A Control Register for Timer 2\" class=\"wp-image-4524\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2A-1024x95.png 1024w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2A-300x28.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2A-768x71.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2A.png 1195w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">TCCR2A Control Register for Timer2<\/figcaption><\/figure>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2B.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"87\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2B-1024x87.png\" alt=\"TCCR2B Control Register for Timer2\" class=\"wp-image-4525\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2B-1024x87.png 1024w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2B-300x26.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2B-768x66.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TCCR2B.png 1195w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">TCCR2B Control Register for Timer2<\/figcaption><\/figure>\n\n<p>You can also find the registers and their description in the rather elongated <a href=\"http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">data sheet<\/a> for the ATmega 48 \/ 88 \/ 168 \/ 328 family.<\/p>\r\n\r\n<p>The designations for the registers and bits are defined in the AVR libraries by #define instructions. And since these libraries are an integral part of the Arduino IDE, this makes access easy.<\/p>\r\n\r\n<p>Basically, the bits of the register are like switches: set bit = 1 = \u00a0 switch on, bit not set = 0 = switch off. How to combine the switches in a meaningful way is the subject of this article.<\/p>\n\n<h4 class=\"wp-block-heading\">The Timer\/Counter Interrupt Mask Registers TIMSKx<\/h4>\n\n<p>If you want a timer\/counter overflow or the match with a comparison value (Compare Match) to trigger an interrupt, you can set this in the corresponding registers TIMSK0 or TIMSK2. Here TIMSK2 as an example:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TIMSK2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"85\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TIMSK2-1024x85.png\" alt=\"\" class=\"wp-image-4534\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TIMSK2-1024x85.png 1024w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TIMSK2-300x25.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TIMSK2-768x64.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/TIMSK2.png 1188w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n<p>Setting TOIE2 (Timer\/Counter2 Overflow Interrupt Enable) causes a register overflow of TCNT2 (at 8 bits that&#8217;s after 255) to trigger an interrupt. Setting OCIE2A and OCIE2B bits (= Timer\/Counter2 Output Compare Match Interrupt Enable A or B) cause an interrupt to be triggered if TCNT2 matches the comparison values in the Output Compare Registers.<\/p>\r\n\r\nTIMSK0 is organized accordingly. Simply replace 2 with 0 each.\n\n<h4 class=\"wp-block-heading\">The Output Compare Register OCRxy<\/h4>\n\n<p>For Timer0 and the Timer2 there are the Output Compare Registers OCR0A and OCR0B respectively OCR2A and OCR2B. When you use them, the content in TCNT0 or TCNT2 is constantly compared with the OCR0A\/OCR0B or OCR2A\/OCR2B register values. What happens in case of a match is specified in the Timer\/Counter Control Registers (TCCRxy).<\/p>\n\n<h4 class=\"wp-block-heading\">The Output Compare Pins OCxy<\/h4>\n\n<p>The timers\/counters each have two pins assigned, OCxA and OCxB. In the Timer\/Counter Control Registers (TCCRxy) you specify the status of the pins for a compare match or timer overflow.<\/p>\r\n\r\n<p>The following diagram shows where these pins are located on the ATmega328P and which are the corresponding Arduino pins.<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Pinout-Atmega-328P-vs-Arduino-UNO.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"495\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Pinout-Atmega-328P-vs-Arduino-UNO-1024x495.png\" alt=\"\" class=\"wp-image-4356\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Pinout-Atmega-328P-vs-Arduino-UNO-1024x495.png 1024w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Pinout-Atmega-328P-vs-Arduino-UNO-300x145.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Pinout-Atmega-328P-vs-Arduino-UNO-768x371.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Pinout-Atmega-328P-vs-Arduino-UNO-1320x637.png 1320w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Pinout-Atmega-328P-vs-Arduino-UNO.png 1495w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Pinout des Atmega328P vs. Arduino UNO<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\">Overview of the settings<\/h2>\n\n<p>Hold out &#8211; I have to torment you a little with more information until we get to the examples.<\/p>\r\n\r\n<p>First, you set the Wave Form Generation Mode in the Timer\/Counter Control Registers (TCCRxy). The three bits WGMx0, WGMx1 and WGMx2 are responsible for this. Why they had to spread these bits into two registers is a mystery to me. Here is an overview for the Timer2:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/WGMTable_2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"927\" height=\"402\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/WGMTable_2.png\" alt=\"Wave Form Generation Mode Description for Timer 2 \" class=\"wp-image-4602\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/WGMTable_2.png 927w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/WGMTable_2-300x130.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/WGMTable_2-768x333.png 768w\" sizes=\"auto, (max-width: 927px) 100vw, 927px\" \/><\/a><figcaption class=\"wp-element-caption\">Wave Form Generation Mode Description for Timer2<\/figcaption><\/figure>\n\n<h3 class=\"wp-block-heading\">The Output Compare Modes<\/h3>\n\n<p>The Compare Output Mode, i.e. the effect of the bits COMxyz (with x = 0 or 2, y = A or B, z = 0 or 1) depends on the selected WGM mode.<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"949\" height=\"171\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2.png\" alt=\"Compare Output Mode for non-PWM (Normal Mode 0), Timer 2, COM2Ax\" class=\"wp-image-4530\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2.png 949w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2-300x54.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2-768x138.png 768w\" sizes=\"auto, (max-width: 949px) 100vw, 949px\" \/><\/a><figcaption class=\"wp-element-caption\">1a) Compare Output Mode for non-PWM (Normal Mode 0), Timer2, COM2Ax<\/figcaption><\/figure>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2B.png\"><img loading=\"lazy\" decoding=\"async\" width=\"945\" height=\"164\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2B.png\" alt=\"Compare Output Mode for non-PWM (Normal Mode 0), Timer 2, COM2Bx \" class=\"wp-image-4544\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2B.png 945w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2B-300x52.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_nonPWM_2B-768x133.png 768w\" sizes=\"auto, (max-width: 945px) 100vw, 945px\" \/><\/a><figcaption class=\"wp-element-caption\">1b) Compare Output Mode for non-PWM (Normal Mode 0), Timer2, COM2Bx<\/figcaption><\/figure>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"946\" height=\"226\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2.png\" alt=\"Compare Output Mode for Fast PWM (WGM modes 3 and 7), Timer 2, COM2Ax\" class=\"wp-image-4531\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2.png 946w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2-300x72.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2-768x183.png 768w\" sizes=\"auto, (max-width: 946px) 100vw, 946px\" \/><\/a><figcaption class=\"wp-element-caption\">2a) Compare Output Mode for Fast PWM (WGM modes 3 and 7), Timer2, COM2Ax<\/figcaption><\/figure>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2B.png\"><img loading=\"lazy\" decoding=\"async\" width=\"946\" height=\"195\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2B.png\" alt=\"Compare Output Mode for Fast PWM (WGM modes 3 and 7), Timer 2, COM2Bx \" class=\"wp-image-4533\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2B.png 946w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2B-300x62.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_FastPWM_2B-768x158.png 768w\" sizes=\"auto, (max-width: 946px) 100vw, 946px\" \/><\/a><figcaption class=\"wp-element-caption\">2b) Compare Output Mode for Fast PWM (WGM modes 3 and 7), Timer2, COM2Bx<\/figcaption><\/figure>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"949\" height=\"257\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2.png\" alt=\"Compare Output Mode for Phase Correct PWM (WGM Modes 1 and 5), Timer 2, COM2Ax\" class=\"wp-image-4532\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2.png 949w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2-300x81.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2-768x208.png 768w\" sizes=\"auto, (max-width: 949px) 100vw, 949px\" \/><\/a><figcaption class=\"wp-element-caption\">3a) Compare Output Mode for Phase Correct PWM (WGM Modes 1 and 5), Timer2, COM2Ax<\/figcaption><\/figure>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2B.png\"><img loading=\"lazy\" decoding=\"async\" width=\"945\" height=\"226\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2B.png\" alt=\" Compare Output Mode for Phase Correct PWM (WGM Modes 1 and 5), Timer 2, COM2Bx \" class=\"wp-image-4546\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2B.png 945w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2B-300x72.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/CompOutpMode_PhaseCorrectPWM_2B-768x184.png 768w\" sizes=\"auto, (max-width: 945px) 100vw, 945px\" \/><\/a><figcaption class=\"wp-element-caption\">3b) Compare Output Mode for Phase Correct PWM (WGM Modes 1 and 5), Timer2, COM2Bx<\/figcaption><\/figure>\n\n<p>These were the settings for Timer2. The good news: all previous tables of the Timer2 correspond to those of the Timer0. You only have to replace 2 with a 0 where 2 represents the timer.<\/p>\n\n<h3 class=\"wp-block-heading\">Prescaler settings<\/h3>\n\n<p>The speed at which the Timer\/Counter register TCNTx counts is based on the system clock. At the Arduino UNO, that&#8217;s 16 MHz. For many applications, this is far too fast. Therefore, there is the prescaler, which causes the counter to be incremented only every n<sup>th<\/sup> clock. You can make the settings for this in the Timer\/Counter Control Register B (TCCRxB) with the Clock Select Bits (CSx0, CSx1,CSx2). For the Timer2:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"942\" height=\"291\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit2.png\" alt=\"Setting the prescaler with the Clock Select bits for Timer 2\" class=\"wp-image-4551\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit2.png 942w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit2-300x93.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit2-768x237.png 768w\" sizes=\"auto, (max-width: 942px) 100vw, 942px\" \/><\/a><figcaption class=\"wp-element-caption\">Setting the prescaler with the Clock Select bits for Timer2<\/figcaption><\/figure>\n\n<p>For the Timer0 the table is different, more about this later. There is a reason why I start with the Timer2. More about that later, too.<\/p>\n\n<h2 class=\"wp-block-heading\">The timer in normal mode<\/h2>\n\n<p>Done, now it&#8217;s time to put it in practice. So complex the settings, so simple the sketches. The confusion will disappear.<\/p>\r\n\r\n<p>We start with normal mode. This is suitable for rather slow applications.<\/p>\n\n<h3 class=\"wp-block-heading\">The first sketch in Normal Mode<\/h3>\n\n<p>For the following sketch, you connect an LED to the Arduino Pin 7 (PD7). You set the control register A of the Timer2, i.e. TCCR2A, to 0. This deactivates the OC2A pin. All CS2x bits are set in Timer\/Counter Control Register B (TCCR2B). The prescaler is therefore 1024. No WGM2x bit is set, so Normal Mode is active. In the Timer\/Counter2 Interrupt Mask Register, the bit for the Timer Overflow Interrupt is set. This means that every time the TCNT2 overflows ( &gt; 255), an interrupt is triggered. What you do with the interrupt is defined in the ISR (Interrupt Service Routine). The ISR handles TIMER2_OVF_vect, i.e. the Timer2 Overflow Interrupt. A table of available interrupts can be found in the data sheet.<\/p>\r\n\r\n<p>In this case, the interrupt causes the pin 7 to be inverted. <em>Note: with DDRD |= (1&lt;&lt;PD7) pin 7 was defined as output. This setting should always be made after the other register settings.<\/em><\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"timer2_normal_mode_minimum_frequenz.ino\" data-enlighter-title=\"timer2_normal_mode_minimum_frequenz.ino\">void\u00a0setup(){\u00a0\r\n\u00a0\u00a0TCCR2A\u00a0=\u00a00x00;\u00a0\/\/\u00a0Wave\u00a0Form\u00a0Generation\u00a0Mode\u00a00:\u00a0Normal\u00a0Mode;\u00a0OC2A\u00a0disconnected\r\n\u00a0\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22)\u00a0+\u00a0(1&lt;&lt;CS21)\u00a0+\u00a0(1&lt;&lt;CS20);\u00a0\/\/\u00a0prescaler\u00a0=\u00a01024\r\n\u00a0\u00a0TIMSK2\u00a0=\u00a0(1&lt;&lt;TOIE2);\u00a0\/\/\u00a0interrupt\u00a0when\u00a0TCNT2\u00a0is\u00a0overflowed\r\n\u00a0\u00a0DDRD\u00a0|=\u00a0(1&lt;&lt;PD7);\u00a0\u00a0\/\/\u00a0Portmanipulation:\u00a0replaces\u00a0pinMode(7,\u00a0OUTPUT);\r\n}\u00a0\r\n\r\nvoid\u00a0loop()\u00a0{\u00a0\r\n\u00a0\/\/\u00a0do\u00a0something\u00a0else\r\n}\r\n\r\nISR(TIMER2_OVF_vect){\r\n\u00a0\u00a0\u00a0\u00a0PORTD\u00a0^=\u00a0(1&lt;&lt;PD7);\u00a0\/\/\u00a0toggle\u00a0PD7\r\n}<\/pre>\r\n&nbsp;\n\n<p>Upload the sketch and see what happens. This is the result on the oscilloscope:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2einfachst-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"515\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2einfachst-1.png\" alt=\"Minimum frequency in Normal Mode. The frequency refers to a HIGH\/LOW pair.\" class=\"wp-image-4605\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2einfachst-1.png 808w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2einfachst-1-300x191.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2einfachst-1-768x490.png 768w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/a><figcaption class=\"wp-element-caption\">Minimum frequency in Normal Mode. The frequency refers to a HIGH\/LOW pair.<\/figcaption><\/figure>\n\n<p>If you transfer the sketch to the Timer0, you will receive an error message:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Fehlermeldung.png\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"56\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Fehlermeldung.png\" alt=\"\" class=\"wp-image-4578\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Fehlermeldung.png 642w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Fehlermeldung-300x26.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/a><\/figure>\n\n<p>The problem is that in the Arduino environment the Timer0_OVF_vect is used for other things. That&#8217;s why I started with the Timer2. In other environments, such as Atmel Studio, the problem does not exist. At the end of the article, I will come back to that.<\/p>\n\n<h4 class=\"wp-block-heading\">Calculation of the frequency<\/h4>\n\n<p>You can see that the LED flashes very fast when using the last sketch. The Arduino clocks 16 million times per second. Due to the prescaler setting, TCNT2 is increased every 1024th clock. TCNT2 starts at 0 and overflows after <strong>255.<\/strong> That&#8217;s <strong>256<\/strong> steps (like a <code>for(i = 0; i&lt;256; i++)<\/code>). Therefore, the general formula for frequency f is:<\/p>\n\n<p><\/p>\r\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-78ecf46970cc958a429332cc0b56e9ef_l3.png\" height=\"43\" width=\"397\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#102;&#61;&#92;&#102;&#114;&#97;&#99;&#123;&#115;&#121;&#115;&#116;&#101;&#109;&#92;&#95;&#99;&#108;&#111;&#99;&#107;&#125;&#123;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#92;&#99;&#100;&#111;&#116;&#50;&#53;&#54;&#125;&#61;&#92;&#102;&#114;&#97;&#99;&#123;&#49;&#54;&#48;&#48;&#48;&#48;&#48;&#48;&#125;&#123;&#49;&#48;&#50;&#52;&#92;&#99;&#100;&#111;&#116;&#32;&#50;&#53;&#54;&#125;&#61;&#54;&#49;&#46;&#48;&#51;&#53;&#46;&#46;&#46;&#91;&#92;&#116;&#101;&#120;&#116;&#123;&#115;&#125;&#94;&#123;&#45;&#49;&#125;&#93; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\r\n\nGraphically, it looks like this:\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_Toggle.png\"><img loading=\"lazy\" decoding=\"async\" width=\"791\" height=\"434\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_Toggle.png\" alt=\"Graph 1: TNTC vs. pin status\" class=\"wp-image-4558\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_Toggle.png 791w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_Toggle-300x165.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_Toggle-768x421.png 768w\" sizes=\"auto, (max-width: 791px) 100vw, 791px\" \/><\/a><figcaption class=\"wp-element-caption\">Graph 1: TNTC vs. pin status<\/figcaption><\/figure>\n\n<p>TCNT starts at Bottom (here: 0) and counts to Top. In the WGM table you find for Normal Mode: Top = 0xFF and TOV flag set on MAX (=TOP). PD7 inverts with each overflow.<\/p>\n\n<h3 class=\"wp-block-heading\">Further downgrade of the frequency<\/h3>\n\n<p>So even with the maximum prescaler, the frequency for a blinksketch is still very high. To further slow down the process, we introduce a scale factor into the ISR (counter = 60). This results in a frequency of about 1. Strictly speaking, 61,035&#8230; \/ 60 [s<sup>-1<\/sup>].<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"timer2_normal_mode_verlangsamt.ino\" data-enlighter-title=\"timer2_normal_mode_verlangsamt.ino\">void\u00a0setup(){\u00a0\r\n\u00a0\u00a0TCCR2A\u00a0=\u00a00x00;\u00a0\/\/\u00a0Wave\u00a0Form\u00a0Generation\u00a0Mode\u00a00:\u00a0Normal\u00a0Mode,\u00a0OC2A\u00a0disconnected\r\n\u00a0\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22)\u00a0+\u00a0(1&lt;&lt;CS21)\u00a0+\u00a0(1&lt;&lt;CS20);\u00a0\/\/\u00a0prescaler\u00a0=\u00a01024\r\n\u00a0\u00a0TIMSK2\u00a0=\u00a0(1&lt;&lt;TOIE2);\u00a0\/\/\u00a0interrupt\u00a0when\u00a0TCNT2\u00a0is\u00a0overflowed\r\n\u00a0\u00a0DDRD\u00a0|=\u00a0(1&lt;&lt;PD7);\r\n}\u00a0\r\n\r\nvoid\u00a0loop()\u00a0{\u00a0\r\n\u00a0\/\/\u00a0do\u00a0something\u00a0else\r\n}\r\n\r\nISR(TIMER2_OVF_vect){\u00a0\u00a0\/\/\u00a0Interrupt\u00a0Service\u00a0Routine\u00a0\r\n\u00a0\u00a0static\u00a0int\u00a0counter\u00a0=\u00a00;\r\n\u00a0\u00a0counter++;\r\n\u00a0\u00a0if(counter==60){\r\n\u00a0\u00a0\u00a0\u00a0PORTD\u00a0^=\u00a0(1&lt;&lt;PD7);\u00a0\r\n\u00a0\u00a0\u00a0\u00a0counter\u00a0=\u00a00;\u00a0\r\n\u00a0\u00a0}\r\n}<\/pre>\r\n&nbsp;\n\n<h3 class=\"wp-block-heading\">Setting an exact frequency<\/h3>\n\n<p>In the next step, we want to set a frequency of exactly 1 Hz. For this, we use the option to define a starting value for the TCNTx register. Then it&#8217;s only <code>256 - starting_value<\/code> steps to the overflow. The general formula is:<\/p>\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 44px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-cc7614e4ed3755f20a39d8411bb619bc_l3.png\" height=\"44\" width=\"445\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#102;&#95;&#123;&#100;&#101;&#115;&#105;&#114;&#101;&#100;&#125;&#92;&#99;&#100;&#111;&#116;&#32;&#115;&#99;&#97;&#108;&#101;&#102;&#97;&#99;&#116;&#111;&#114;&#32;&#61;&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#115;&#121;&#115;&#116;&#101;&#109;&#92;&#95;&#99;&#108;&#111;&#99;&#107;&#125;&#123;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#92;&#99;&#100;&#111;&#116;&#40;&#50;&#53;&#54;&#45;&#115;&#116;&#97;&#114;&#116;&#92;&#95;&#118;&#97;&#108;&#117;&#101;&#41;&#125; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\r\n\n<p>With <em>f<sub>desired<\/sub><\/em> = 1 results:<\/p>\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-e5825d49e477c2c289edc45269045bf6_l3.png\" height=\"43\" width=\"360\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#115;&#116;&#97;&#114;&#116;&#92;&#95;&#118;&#97;&#108;&#117;&#101;&#61;&#50;&#53;&#54;&#45;&#92;&#102;&#114;&#97;&#99;&#123;&#115;&#121;&#115;&#116;&#101;&#109;&#92;&#95;&#99;&#108;&#111;&#99;&#107;&#125;&#123;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#92;&#99;&#100;&#111;&#116;&#32;&#115;&#99;&#97;&#108;&#101;&#102;&#97;&#99;&#116;&#111;&#114;&#125; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\n\n<p>The system clock is 16 MHz. But it&#8217;s still an equation with three unknowns. However, we know that the prescaler can take only certain values, that the starting value must be less than 256 and it must be an integral number. So let&#8217;s try a bit:<\/p>\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-746f0ca8c7ef65202b106c43c0d46d5e_l3.png\" height=\"43\" width=\"478\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#115;&#116;&#97;&#114;&#116;&#92;&#95;&#118;&#97;&#108;&#117;&#101;&#32;&#61;&#32;&#50;&#53;&#54;&#45;&#92;&#102;&#114;&#97;&#99;&#123;&#49;&#53;&#54;&#50;&#53;&#125;&#123;&#115;&#99;&#97;&#108;&#101;&#102;&#97;&#99;&#116;&#111;&#114;&#125;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#119;&#105;&#116;&#104;&#92;&#59;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#61;&#49;&#48;&#50;&#52; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-bbb3d62caee875979df9c5df6b15bbd8_l3.png\" height=\"43\" width=\"468\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#115;&#116;&#97;&#114;&#116;&#92;&#95;&#118;&#97;&#108;&#117;&#101;&#32;&#61;&#32;&#50;&#53;&#54;&#45;&#92;&#102;&#114;&#97;&#99;&#123;&#54;&#50;&#53;&#48;&#48;&#125;&#123;&#115;&#99;&#97;&#108;&#101;&#102;&#97;&#99;&#116;&#111;&#114;&#125;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#119;&#105;&#116;&#104;&#92;&#59;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#61;&#50;&#53;&#54; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\r\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-aa154f422e651997f95383018c1a096c_l3.png\" height=\"43\" width=\"460\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#115;&#116;&#97;&#114;&#116;&#92;&#95;&#118;&#97;&#108;&#117;&#101;&#32;&#61;&#32;&#50;&#53;&#54;&#45;&#92;&#102;&#114;&#97;&#99;&#123;&#50;&#53;&#48;&#48;&#48;&#48;&#125;&#123;&#115;&#99;&#97;&#108;&#101;&#102;&#97;&#99;&#116;&#111;&#114;&#125;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#119;&#105;&#116;&#104;&#92;&#59;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#61;&#54;&#52; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\r\n\n<p>The prescaler 1024 delivers &#8220;crooked&#8221; values. With a prescaler of 256, it looks better. If we take a scale factor of 500, we get a starting value of 131. With a prescaler of 64 and a scale factor of 1000, a starting value of 6 results. Both are good.<\/p>\r\n\r\n<p>If you don&#8217;t want to calculate, then you can use calculators, which you find e.g. <a href=\"http:\/\/evolutec.publicmsg.de\/index.php?menu=software&amp;content=prescalertools\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> or <a href=\"https:\/\/eleccelerator.com\/avr-timer-calculator\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. Although these calculators do not consider scale factors, they are still a help.<\/p>\r\n\r\n<p>The previous sketch is slightly changed. TCNT2 must be set back to the start value at the beginning and after each overflow.<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"timer2_normal_mode_genau_1Hz.ino\" data-enlighter-title=\"timer2_normal_mode_genau_1Hz.ino\">byte\u00a0counterStart\u00a0=\u00a0131;\u00a0\u00a0\/\/\u00a0alternative:\u00a06\r\nunsigned\u00a0int\u00a0scaleFactor\u00a0=\u00a0500;\u00a0\/\/\u00a0alternative:\u00a01000\r\n\r\nvoid\u00a0setup(){\u00a0\r\n\u00a0\u00a0TCCR2A\u00a0=\u00a00x00;\u00a0\/\/\u00a0OC2A\u00a0and\u00a0OC2B\u00a0disconnected;\u00a0Wave\u00a0Form\u00a0Generation\u00a0Mode\u00a00:\u00a0Normal\u00a0Mode\r\n\u00a0\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22)\u00a0+\u00a0(1&lt;&lt;CS21);\u00a0\/\/\u00a0prescaler\u00a0=\u00a0256\r\n\u00a0\u00a0\/\/\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22);\u00a0\/\/\u00a0prescaler\u00a0=\u00a064;\u00a0\r\n\u00a0\u00a0TIMSK2\u00a0=\u00a0(1&lt;&lt;TOIE2);\u00a0\/\/\u00a0interrupt\u00a0when\u00a0TCNT2\u00a0is\u00a0overflowed\r\n\u00a0\u00a0TCNT2\u00a0=\u00a0counterStart;\r\n\u00a0\u00a0DDRD\u00a0|=\u00a0(1&lt;&lt;PD7);\r\n}\u00a0\r\n\r\nvoid\u00a0loop()\u00a0{\u00a0\r\n\u00a0\/\/\u00a0do\u00a0something\u00a0else\r\n}\r\n\r\nISR(TIMER2_OVF_vect){\r\n\u00a0\u00a0static\u00a0int\u00a0counter\u00a0=\u00a00;\r\n\u00a0\u00a0TCNT2\u00a0=\u00a0counterStart;\r\n\u00a0\u00a0counter++;\r\n\u00a0\u00a0if(counter==scaleFactor){\r\n\u00a0\u00a0\u00a0\u00a0PORTD\u00a0^=\u00a0(1&lt;&lt;PD7);\r\n\u00a0\u00a0\u00a0\u00a0counter\u00a0=\u00a00;\u00a0\r\n\u00a0\u00a0}\r\n}<\/pre>\r\n&nbsp;\n\n<h3 class=\"wp-block-heading\">An application in normal mode: asynchronous LEDs<\/h3>\n\n<p>You may have noticed that the main loop was empty in the previous sketches. The control of the flashing LED happens in the background. If the blink code was part of the main loop, then all additional code has to be programmed &#8220;around&#8221; that. This can become quite challenging, if other time-dependent tasks shall take place in parallel.<\/p>\r\n\r\n<p>As a simple example, we let two LEDs flash asynchronously in the next sketch. Try programming this without a timer.<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"timer2_normal_mode_asynchrone_LEDs.ino\" data-enlighter-title=\"timer2_normal_mode_asynchrone_LEDs.ino\">byte\u00a0counterStart\u00a0=\u00a0131;\u00a0\u00a0\/\/\u00a0alternative:\u00a06\r\nunsigned\u00a0int\u00a0scaleFactor\u00a0=\u00a0500;\u00a0\/\/\u00a0alternative:\u00a01000\r\n\r\nvoid\u00a0setup(){\u00a0\r\n\u00a0\u00a0TCCR2A\u00a0=\u00a00x00;\u00a0\/\/\u00a0OC2A\u00a0and\u00a0OC2B\u00a0disconnected;\u00a0Wave\u00a0Form\u00a0Generator:\u00a0Normal\u00a0Mode\r\n\u00a0\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22)\u00a0+\u00a0(1&lt;&lt;CS21);\u00a0\/\/\u00a0prescaler\u00a0=\u00a0256\r\n\u00a0\u00a0\/\/\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22);\u00a0\/\/\u00a0prescaler\u00a0=\u00a064;\u00a0\r\n\u00a0\u00a0TIMSK2\u00a0=\u00a0(1&lt;&lt;TOIE2);\u00a0\/\/\u00a0interrupt\u00a0when\u00a0TCNT2\u00a0is\u00a0overflowed\r\n\u00a0\u00a0TCNT2\u00a0=\u00a0counterStart;\r\n\u00a0\u00a0DDRD\u00a0|=\u00a0(1&lt;&lt;PD7)\u00a0+\u00a0(1&lt;&lt;PD6);\u00a0\/\/\u00a0Pin\u00a06\u00a0und\u00a0Pin\u00a07\u00a0als\u00a0Ausgang\r\n}\u00a0\r\n\r\nvoid\u00a0loop()\u00a0{\u00a0\r\n\u00a0PORTD\u00a0^=\u00a0(1&lt;&lt;PD6);\r\n\u00a0delay(723);\r\n}\r\n\r\nISR(TIMER2_OVF_vect){\r\n\u00a0\u00a0static\u00a0int\u00a0counter\u00a0=\u00a00;\r\n\u00a0\u00a0TCNT2\u00a0=\u00a0counterStart;\r\n\u00a0\u00a0counter++;\r\n\u00a0\u00a0if(counter==scaleFactor){\r\n\u00a0\u00a0\u00a0\u00a0PORTD\u00a0^=\u00a0(1&lt;&lt;PD7);\r\n\u00a0\u00a0\u00a0\u00a0counter\u00a0=\u00a00;\u00a0\r\n\u00a0\u00a0}\r\n}<\/pre>\r\n&nbsp;\n\n<h2 class=\"wp-block-heading\">The timer in CTC mode<\/h2>\n\n<p>In &#8220;Clear Timer on Compare Match&#8221; mode, or CTC for short, TCNTx is reset to zero not after 256 steps, but after reaching the value stored in OCRxA. Again we want to have an LED flashing every second. The formula for frequency calculation is:<\/p>\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 44px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-c4e30b0646d10f167c2551bdaeebde30_l3.png\" height=\"44\" width=\"365\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#102;&#95;&#123;&#100;&#101;&#115;&#105;&#114;&#101;&#100;&#125;&#92;&#99;&#100;&#111;&#116;&#32;&#115;&#99;&#97;&#108;&#101;&#102;&#97;&#99;&#116;&#111;&#114;&#61;&#92;&#102;&#114;&#97;&#99;&#123;&#115;&#121;&#115;&#116;&#101;&#109;&#92;&#95;&#99;&#108;&#111;&#99;&#107;&#125;&#123;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#92;&#99;&#100;&#111;&#116;&#40;&#49;&#43;&#84;&#111;&#112;&#41;&#125; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\r\n\n<p>And why (1 + Top) and not just Top? Quite simply: because the 0 counts! Top is therefore with <em>f<sub>desired<\/sub><\/em> = 1:<\/p>\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-002639dc644848990af8b8f0138a1921_l3.png\" height=\"43\" width=\"279\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#84;&#111;&#112;&#61;&#92;&#102;&#114;&#97;&#99;&#123;&#115;&#121;&#115;&#116;&#101;&#109;&#92;&#95;&#99;&#108;&#111;&#99;&#107;&#125;&#123;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#92;&#99;&#100;&#111;&#116;&#32;&#115;&#99;&#97;&#108;&#101;&#102;&#97;&#99;&#116;&#111;&#114;&#125;&#45;&#49; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\n\n<p>A system clock of 16 MHz, a prescaler of 256 and a scale factor of 500 results in a top value of 124. We write this value into OCRxA register, here: OCR2A = 124.<\/p>\r\n\r\n<p>According to the WGM table, we have to set the bit WGM21 for the CTC mode. For the prescaler 256 we set CS22 and CS21. In the Timer\/Counter Interrupt Mask Register we set OCIE2A (Output Compare Interrupt Enable A, Timer2) because this time there is no timer overflow, but a compare match. Accordingly, we also have to change the ISR routine and pass TIMER2_COMPA_vect as parameter.<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"timer2_ctc_mode_genau_1Hz.ino\" data-enlighter-title=\"timer2_ctc_mode_genau_1Hz.ino\">unsigned\u00a0int\u00a0scaleFactor\u00a0=\u00a0500;\r\n\r\nvoid\u00a0setup(){\u00a0\r\n\u00a0\u00a0TCCR2A\u00a0=\u00a0(1&lt;&lt;WGM21);\u00a0\/\/\u00a0Wave\u00a0Form\u00a0Generation\u00a0Mode\u00a02:\u00a0CTC,\u00a0OC2A\u00a0disconnected\r\n\u00a0\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22)\u00a0+\u00a0(1&lt;&lt;CS21)\u00a0;\u00a0\/\/\u00a0prescaler\u00a0=\u00a0256\r\n\u00a0\u00a0TIMSK2\u00a0=\u00a0(1&lt;&lt;OCIE2A);\u00a0\/\/\u00a0interrupt\u00a0when\u00a0Compare\u00a0Match\u00a0with\u00a0OCR2A\r\n\u00a0\u00a0OCR2A\u00a0=\u00a0124;\r\n\u00a0\u00a0DDRD\u00a0|=\u00a0(1&lt;&lt;PD7);\r\n\u00a0\u00a0\r\n}\u00a0\r\n\r\nvoid\u00a0loop()\u00a0{\u00a0\r\n\u00a0\/\/\u00a0do\u00a0something\u00a0else\r\n}\r\n\r\nISR\u00a0(TIMER2_COMPA_vect){\u00a0\u00a0\/\/\u00a0Interrupt\u00a0Service\u00a0Routine\u00a0\r\n\u00a0\u00a0static\u00a0int\u00a0counter\u00a0=\u00a00;\r\n\u00a0\u00a0counter++;\r\n\u00a0\u00a0if(counter==scaleFactor){\r\n\u00a0\u00a0\u00a0\u00a0PORTD\u00a0^=\u00a0(1&lt;&lt;PD7);\u00a0\/\/\u00a0\r\n\u00a0\u00a0\u00a0\u00a0counter\u00a0=\u00a00;\u00a0\r\n\u00a0\u00a0}\r\n}<\/pre>\r\n&nbsp;\n\n<p>If we compare normal mode with CTC mode, we have counted in one case from a certain TCNT value to 255, and in the other case from 0 to the value stored in OCRA.<\/p>\n\n<h2 class=\"wp-block-heading\">The timer in Fast PWM mode<\/h2>\n\n<p>In Fast PWM mode, you usually work with the pins associated with the timer. This is important for the<\/p>\r\n<ul>\r\n \t<li>Timer0: OC0A (=PD6, Arduino Pin 6) \/ OC0B (=PD5, Arduino Pin 5)<\/li>\r\n \t<li>Timer2: OC2A (=PB3, Arduino Pin 11) \/ OC2B (=PD3, Arduino Pin 3)<\/li>\r\n<\/ul>\n\n<p>The PWM mode works in Mode 3 with a timer overflow after 255 (0xFF). In Mode 7, PWM mode works with a Compare Match. Top is the value stored in OCRxA.<\/p>\n\n<h3 class=\"wp-block-heading\">Fast PWM on OC2B<\/h3>\n\n<p>As an example, let&#8217;s take Mode 7. The target is a square wave signal with a frequency of 1 kHz at OC2B. Per period, the signal should be HIGH 20% of the time and 80% LOW. Another expression for this is: the duty cycle is 20%.<\/p>\r\n\r\n<p>We set the COM2B1 bit for this purpose. According to the table, this means: &#8220;clear OC2B at Compare Match, set OC2B at BOTTOM&#8221;. The Compare Match refers to the value stored in OCR2B. Graphically, it looks like this:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/PWM_20_80_OC2B.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"527\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/PWM_20_80_OC2B-1024x527.png\" alt=\"Graph 2: PWM signal on OC2B, Timer2\" class=\"wp-image-4581\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/PWM_20_80_OC2B-1024x527.png 1024w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/PWM_20_80_OC2B-300x154.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/PWM_20_80_OC2B-768x395.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/PWM_20_80_OC2B.png 1139w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Graph 2: PWM signal on OC2B<\/figcaption><\/figure>\n\n<p>First, we have to do some calculations again. We need the top value for the frequency and the value for OCR2B for the duty cycle. Because of the high frequency, we don&#8217;t need a scale factor. The following applies:<\/p>\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 44px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-01a9450589a2d54bf0bd426652063342_l3.png\" height=\"44\" width=\"255\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#102;&#95;&#123;&#100;&#101;&#115;&#105;&#114;&#101;&#100;&#125;&#61;&#92;&#102;&#114;&#97;&#99;&#123;&#115;&#121;&#115;&#116;&#101;&#109;&#92;&#95;&#99;&#108;&#111;&#99;&#107;&#125;&#123;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#92;&#99;&#100;&#111;&#116;&#40;&#49;&#43;&#84;&#111;&#112;&#41;&#125; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\r\n\n<p><p class=\"ql-center-displayed-equation\" style=\"line-height: 43px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/ql-cache\/quicklatex.com-eefa47457d62271fa19c6666dfc34749_l3.png\" height=\"43\" width=\"539\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#92;&#91; &#84;&#111;&#112;&#61;&#92;&#102;&#114;&#97;&#99;&#123;&#49;&#54;&#48;&#48;&#48;&#125;&#123;&#112;&#114;&#101;&#115;&#99;&#97;&#108;&#101;&#114;&#125;&#45;&#49;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#119;&#105;&#116;&#104;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#99;&#108;&#111;&#99;&#107;&#61;&#49;&#54;&#48;&#48;&#48;&#48;&#48;&#48;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#32;&#117;&#110;&#100;&#32;&#92;&#59;&#92;&#59;&#92;&#59;&#92;&#59;&#102;&#61;&#49;&#48;&#48;&#48; &#92;&#93;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p><\/p>\n\n<p>A prescaler of 64 results in 249 for top. That&#8217;s 250 steps. One-fifth of it is 50. Since the counter starts at 0, OCR2B is 49, at least theoretically. In practice, you have to try it out. I have hit the desired signal better with the pairing 249 \/ 50. Of course, it also depends on how exactly the microcontroller clocks.<\/p>\r\n\r\n<p>Here&#8217;s what the sketch looks like:<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"timer2_fast_pwm_mode_80_20_an_OC2B.ino\" data-enlighter-title=\"timer2_fast_pwm_mode_80_20_an_OC2B.ino\">\/\/\u00a0Period\u00a0=\u00a01\u00a0ms\u00a0=&gt;\u00a0Frequenz\u00a0=\u00a01kHz\r\nvoid\u00a0setup(){\u00a0\r\n\u00a0\u00a0\/\/\u00a0WGM22\/WGM21\/WGM20\u00a0all\u00a0set\u00a0-&gt;\u00a0Mode\u00a07,\u00a0fast\u00a0PWM\r\n\u00a0\u00a0TCCR2A\u00a0=\u00a0(1&lt;&lt;COM2B1)\u00a0+\u00a0(1&lt;&lt;WGM21)\u00a0+\u00a0(1&lt;&lt;WGM20);\u00a0\/\/\u00a0Set\u00a0OC2B\u00a0at\u00a0bottom,\u00a0clear\u00a0OC2B\u00a0at\u00a0compare\u00a0match\r\n\u00a0\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22)\u00a0+\u00a0(1&lt;&lt;WGM22);\u00a0\/\/\u00a0prescaler\u00a0=\u00a064;\u00a0\r\n\u00a0\u00a0OCR2A\u00a0=\u00a0249;\r\n\u00a0\u00a0OCR2B\u00a0=\u00a049;\r\n\u00a0\u00a0DDRD\u00a0|=\u00a0(1&lt;&lt;PD3);\r\n}\u00a0\r\n\r\nvoid\u00a0loop()\u00a0{}<\/pre>\r\n&nbsp;\n\n<p>&#8230; and this is what it looks like on the oscilloscope:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2_Fast_PWM_an_OC2B.png\"><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"515\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2_Fast_PWM_an_OC2B.png\" alt=\"PWM signal on OC2B\" class=\"wp-image-4603\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2_Fast_PWM_an_OC2B.png 808w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2_Fast_PWM_an_OC2B-300x191.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/timer2_Fast_PWM_an_OC2B-768x490.png 768w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/a><figcaption class=\"wp-element-caption\">PWM signal on OC2B<\/figcaption><\/figure>\n\n<h3 class=\"wp-block-heading\">Fast PWM on OC2A<\/h3>\n\n<p>For a PWM signal on OC2A, Mode 7 is of limited suitability, as the Compare Match is equal to Top. There is no possibility to divide the signal into a HIGH and a LOW part. For better understanding you might look into the corresponding tables. In Mode 3, the timer counts up to 255, so any duty cycles can be realized there. The disadvantage, however, is that not any frequency can be set because Top is fixed.<\/p>\r\n\r\n<p>PWM signals with 50% duty cycle, on the other hand, are easy to set on OC2A. This is what you do with the combination: Mode 7 \/ set COM2A0. According to the table, this means: Toggle O2CA on Compare Match. The period is doubled (frequency is halved).<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"timer2_fast_pwm_mode_50_50_OC2A.ino\" data-enlighter-title=\"timer2_fast_pwm_mode_50_50_OC2A.ino\">\/\/\u00a0Period\u00a0=\u00a02\u00a0ms\u00a0\/\u00a0Frequenz\u00a0=\u00a0500\u00a0Hz.\u00a0\r\nvoid\u00a0setup(){\u00a0\r\n\u00a0\u00a0\/\/\u00a0WGM22\/WGM21\/WGM20\u00a0all\u00a0set\u00a0-&gt;\u00a0Mode\u00a07,\u00a0fast\u00a0PWM,\u00a0TOP\u00a0=\u00a0OCR2A\r\n\u00a0\u00a0TCCR2A\u00a0=\u00a0(1&lt;&lt;COM2A0)\u00a0+\u00a0(1&lt;&lt;WGM21)\u00a0+\u00a0(1&lt;&lt;WGM20);\u00a0\/\/\u00a0Toggle\u00a0OC2A\u00a0at\u00a0compare\u00a0match\r\n\u00a0\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22)\u00a0+\u00a0(1&lt;&lt;WGM22);\u00a0\/\/\u00a0prescaler\u00a0=\u00a064;\u00a0\r\n\u00a0\u00a0OCR2A\u00a0=\u00a0249;\r\n\u00a0\u00a0DDRB\u00a0|=\u00a0(1&lt;&lt;PB3);\u00a0\/\/\u00a0PB3\u00a0=\u00a0OC2A\u00a0=\u00a0Arduino\u00a0Pin\u00a011\r\n}\u00a0\r\n\r\nvoid\u00a0loop()\u00a0{\u00a0\r\n}<\/pre>\r\n&nbsp;\n\n<h2 class=\"wp-block-heading\">AnalogWrite &#8211; a PWM application<\/h2>\n\n<p>You can see on the oscilloscope that analogWrite is not an analog but a PWM signal:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/analogWrite.png\"><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"515\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/analogWrite.png\" alt=\"\" class=\"wp-image-4607\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/analogWrite.png 808w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/analogWrite-300x191.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/analogWrite-768x490.png 768w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><\/a><figcaption class=\"wp-element-caption\">analogWrite(<em>pin<\/em>, 50) on the oscilloscope<\/figcaption><\/figure>\n\n<h3 class=\"wp-block-heading\">You don&#8217;t have an oscilloscope?<\/h3>\n\n<p>Some may be frustrated that they cannot check the result of the Fast PWM sketches because they do not have an oscilloscope. Three suggestions:<\/p>\r\n<ul>\r\n \t<li>also in Fast PWM mode you can work with Compare Match Interrupts, insert a counter in the ISR and observe the PWM signal in slow motion. You can even set OCIE2A and OCIE2B and use two ISR routines (TIMER2_COMPA_vect \/ TIMER2_COMPB_vect).<\/li>\r\n \t<li>uses the technology explained in my post about <a href=\"https:\/\/wolles-elektronikkiste.de\/en\/ir-remote-controls\" target=\"_blank\" rel=\"noopener noreferrer\">IR remote controls<\/a> to analyze fast signals<\/li>\r\n \t<li>buy a DSO 138 oscilloscope for less than 30 euros. Search for it on Amazon or eBay. This device works amazingly well:<\/li>\r\n<\/ul>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Mini-Oskar_klein.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"768\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Mini-Oskar_klein-1024x768.jpg\" alt=\"\" class=\"wp-image-4593\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Mini-Oskar_klein-1024x768.jpg 1024w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Mini-Oskar_klein-300x225.jpg 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Mini-Oskar_klein-768x576.jpg 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Mini-Oskar_klein-1536x1152.jpg 1536w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Mini-Oskar_klein-1320x990.jpg 1320w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Mini-Oskar_klein.jpg 2000w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">DSO 138 oscilloscope in action<\/figcaption><\/figure>\n\n<h2 class=\"wp-block-heading\">The timer in Phase Correct PWM Mode<\/h2>\n\n<p>In phase-correct PWM mode (Phase Correct PWM Mode), the counter counts up from BOTTOM to TOP and then down again to BOTTOM. As an example, let&#8217;s take the combination of Mode 5 and COM2B1 set, i.e.: &#8220;Clear OC2B when up-counting, set OC2B when down-counting&#8221;. <\/p>\r\n<p>Graphically, this looks like this:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_phase_correct_PWM.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"530\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_phase_correct_PWM-1024x530.png\" alt=\"Phase Correct PWM with Timer2 on OC2B.\" class=\"wp-image-4597\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_phase_correct_PWM-1024x530.png 1024w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_phase_correct_PWM-300x155.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_phase_correct_PWM-768x397.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Grafik_phase_correct_PWM.png 1131w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Graph 3: Phase Correct PWM with Timer2 on OC2B<\/figcaption><\/figure>\n\n<p>This halves the frequency compared to the Fast PWM method.<\/p>\r\n\r\n<p>Phase correct means that the reversal points from HIGH to LOW (or vice versa) are at the same distance before and after the bottom or top of the timer. This results in a symmetric signal, even if the OCR2B Compare value is changed dynamically. A very helpful animation, which shows the difference to the Fast PWM, can be found <a href=\"http:\/\/www.aquaticus.info\/pwm-modes\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. The phase-correct PWM is mainly used for motor controllers. If you want to know more about the PWM modes, see e.g. <a href=\"http:\/\/modelleisenbahn-steuern.de\/controller\/atmega8\/12-11-4-phasenkorrekt-pwm-modus-atmega8.htm\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. There is another PWM mode, namely the &#8220;phase- and frequency-correct PWM&#8221; mode. This subject will be treated <a href=\"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-2-16-bit-timer1\" target=\"_blank\" rel=\"noopener noreferrer\">part 2\u00a0<\/a> (16 bit timer). There I will also go into a little more detail about the differences of the PWM modes.<\/p>\r\n\r\n<p>And here&#8217;s a sketch example:<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"timer2_phase_correct_pwm_mode_80_20_an_OC2B.ino\" data-enlighter-title=\"timer2_phase_correct_pwm_mode_80_20_an_OC2B.ino\">\/\/\u00a0Period\u00a0=\u00a02\u00a0ms\u00a0\r\nvoid\u00a0setup(){\u00a0\r\n\u00a0\u00a0\/\/\u00a0WGM22\/WGM20\u00a0all\u00a0set\u00a0-&gt;\u00a0Mode\u00a05,\u00a0phase\u00a0correct\u00a0PWM\r\n\u00a0\u00a0TCCR2A\u00a0=\u00a0(1&lt;&lt;COM2B1)\u00a0+\u00a0(1&lt;&lt;WGM20);\u00a0\/\/\u00a0Set\u00a0OC2A\u00a0at\u00a0bottom,\u00a0clear\u00a0OC2B\u00a0at\u00a0compare\u00a0match\r\n\u00a0\u00a0TCCR2B\u00a0=\u00a0(1&lt;&lt;CS22)\u00a0+\u00a0(1&lt;&lt;WGM22);\u00a0\/\/\u00a0prescaler\u00a0=\u00a064;\u00a0\r\n\u00a0\u00a0OCR2A\u00a0=\u00a0249;\r\n\u00a0\u00a0OCR2B\u00a0=\u00a049;\r\n\u00a0\u00a0DDRD\u00a0|=\u00a0(1&lt;&lt;PD3);\r\n}\u00a0\r\n\r\nvoid\u00a0loop()\u00a0{\u00a0}<\/pre>\r\n&nbsp;\n\n<h2 class=\"wp-block-heading\">Timer 0 vs. Timer 2<\/h2>\n\n<p>As mentioned above, the structure of Timer0 and Timer2 is very similar. You can see the main difference in the Clock Select Bit table for the timer0:<\/p>\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit0.png\"><img loading=\"lazy\" decoding=\"async\" width=\"942\" height=\"289\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit0.png\" alt=\"Prescaler \/ Clock Select with the Clock Select Bits for Timer0\" class=\"wp-image-4618\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit0.png 942w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit0-300x92.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/ClockSelectBit0-768x236.png 768w\" sizes=\"auto, (max-width: 942px) 100vw, 942px\" \/><\/a><figcaption class=\"wp-element-caption\">Prescaler \/ Clock Select with the Clock Select Bits for Timer0<\/figcaption><\/figure>\n\n<p>There are fewer prescalers to choose from, instead you can use an external clock here. You connect it to T0 (PD4, Arduino pin 4). In addition, you can choose whether to count at the rising or falling edge.<\/p>\n\n<h3 class=\"wp-block-heading\">Simple example of external clocks<\/h3>\n\n<p>As a clock, we use a simple push button. The OC0A output pin (PD6, Arduino Pin 6) is connected to an LED.<\/p>\n<div class=\"wp-block-image size-large\">\n<figure class=\"aligncenter is-resized\"><a href=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Ext_Clock_an_T0.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"762\" src=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Ext_Clock_an_T0-1024x762.png\" alt=\"\" class=\"wp-image-4621\" style=\"width:512px;height:381px\" srcset=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Ext_Clock_an_T0-1024x762.png 1024w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Ext_Clock_an_T0-300x223.png 300w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Ext_Clock_an_T0-768x571.png 768w, https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Ext_Clock_an_T0.png 1151w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Timer0 with push button as external clock<\/figcaption><\/figure>\n<\/div>\n<p>Now let&#8217;s choose Mode 7 and set all CS0x bits (Clock on rising edge). OCR0A we set to 10.<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-group=\"Timer0_External_Clock_TOGGLE_OC0A.ino\" data-enlighter-title=\"Timer0_External_Clock_TOGGLE_OC0A.ino\">void setup(){\r\n  TCCR0A = (1&lt;&lt;COM0A0) + (1&lt;&lt;WGM01) + (1&lt;&lt;WGM00); \/\/ WGM 7: fast PWM; since WGM02 = 1 --&gt; Toggle OC0A on Compare Match;\r\n  TCCR0B = (1&lt;&lt;CS02) + (1&lt;&lt;CS01) + (1&lt;&lt;CS00) + (1&lt;&lt;WGM02);  \/\/ External clock source on T0 (rising edge)\r\n  OCR0A = 10;\r\n  TCNT0 = 0;\r\n  DDRD |= (1&lt;&lt;PD6);\r\n} \r\n\r\nvoid loop() { \r\n}<\/pre>\n\n<p>Theoretically, the LED should switch on or off every eleventh press of the push button. The LED switches accordingly earlier by pushing the button.<\/p>\r\n\r\n<p>Counting external events in the background can be very useful for certain applications. By the way, there are also special counter ICs, which I have described <a href=\"https:\/\/wolles-elektronikkiste.de\/en\/counter-ics\" target=\"_blank\" rel=\"noopener noreferrer\">here.<\/a><\/p>\n\n<p>If you use a clock quartz as a clock set, you can build a quartz watch. This is described in detail <a href=\"https:\/\/www.mikrocontroller.net\/articles\/AVR-GCC-Tutorial\/Die_Timer_und_Z%C3%A4hler_des_AVR#Timer2_im_Asynchron_Mode\" target=\"_blank\" rel=\"noopener noreferrer\">here.<\/a> Pretty cool.<\/p>\n\n<h2 class=\"wp-block-heading\">Help for the Timer and PWM programming<\/h2>\n\n<p>Timer and PWM programming can be quite annoying and confusing. A great help is the tool &#8220;Arduino Web Timers&#8221; by <a href=\"https:\/\/github.com\/dbuezas\" target=\"_blank\" rel=\"noopener\">David Buezas<\/a>, that is available <a href=\"https:\/\/dbuezas.github.io\/arduino-web-timers\/#mcu=ATMEGA328P&amp;timer=0\" target=\"_blank\" rel=\"noopener\">here<\/a>. I really highly recommend it.<\/p>\r\n\n<h2 class=\"wp-block-heading\">Using Microchip (Atmel) Studio<\/h2>\n\n<p>At the very beginning I mentioned that the Timer0 Overflow Interrupt is not accessible in the Arduino environment. If you use the free software Microchip Studio (formerly known as Atmel Studio), you do not have this limitation. An introduction to Microchip Studio can be found <a href=\"https:\/\/wolles-elektronikkiste.de\/en\/atmel-studio-7-an-introduction\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>. You have to invest some time to get used to it, but it&#8217;s worth it from my point of view.<\/p>\r\n\r\n<p>The sketch for the two asynchronously flashing LEDs, transferred to the Timer0, looks like this in Atmel Studio:<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-group=\"Asynchronous_LEDs.cpp\" data-enlighter-title=\"Asynchronous_LEDs.cpp\">#include\u00a0&lt;avr\/io.h&gt;\r\n#include\u00a0&lt;util\/delay.h&gt;\r\n#include\u00a0&lt;avr\/interrupt.h&gt;\r\nuint8_t\u00a0counterStart\u00a0=\u00a0131;\r\nuint16_t\u00a0scaleFactor\u00a0=\u00a0500;\r\n\r\nint\u00a0main(void)\r\n{\r\n\u00a0\u00a0TCCR0A\u00a0=\u00a00x00;\u00a0\/\/\u00a0OC0A\u00a0and\u00a0OC0B\u00a0disconnected;\u00a0Wave\u00a0Form\u00a0Generation\u00a0Mode\u00a00:\u00a0Normal\u00a0Mode\r\n\u00a0\u00a0TCCR0B\u00a0=\u00a0(1&lt;&lt;CS02);\u00a0\/\/\u00a0prescaler\u00a0=\u00a0256\r\n\u00a0\u00a0TIMSK0\u00a0=\u00a0(1&lt;&lt;TOIE0);\u00a0\/\/\u00a0interrupt\u00a0when\u00a0TCNT0\u00a0is\u00a0overflowed\r\n\u00a0\u00a0TCNT0\u00a0=\u00a0counterStart;\r\n\u00a0\u00a0DDRB\u00a0|=\u00a0(1&lt;&lt;PB1)\u00a0+\u00a0(1&lt;&lt;PB0);\r\n\u00a0\u00a0sei();\u00a0\u00a0\/\/\u00a0activate\u00a0interrupts\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0while\u00a0(1)\r\n\u00a0\u00a0{ \r\n\u00a0\u00a0\u00a0\u00a0PORTB\u00a0^=\u00a0(1&lt;&lt;PB0);\r\n\u00a0\u00a0\u00a0\u00a0_delay_ms(723); \r\n\u00a0\u00a0}\r\n}\r\n\r\n\r\nISR(TIMER0_OVF_vect)\r\n{\r\n\u00a0\u00a0static\u00a0int\u00a0counter\u00a0=\u00a00;\r\n\u00a0\u00a0TCNT0\u00a0=\u00a0counterStart;\r\n\u00a0\u00a0counter++;\r\n\u00a0\u00a0if(counter==scaleFactor)\r\n\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0PORTB\u00a0^=\u00a0(1&lt;&lt;PB1);\r\n\u00a0\u00a0\u00a0\u00a0counter\u00a0=\u00a00;\r\n\u00a0\u00a0}\r\n}\r\n<\/pre>\r\n&nbsp;\n\n<h2 class=\"wp-block-heading\">Final words<\/h2>\n\n<p>So, I hope one or the other has persevered up to here. Personally, I found it very exciting when I had my first experiences with the timers and got a deeper insight into the inner structure of the ATmega328P.<\/p>\r\n\r\n<p>What follows is, as announced above, <a href=\"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-2-16-bit-timer1\" target=\"_blank\" rel=\"noopener noreferrer\">part 2<\/a>, which deals with the 16 bit Timer1.<\/p>\n\n<h2 class=\"wp-block-heading\">Acknowledgement<\/h2>\n\n<P>I owe the hourglass in the post image &#8220;<a href=\"https:\/\/pixabay.com\/de\/users\/derGestalterCottbus-5321869\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3308818\" target=\"_blank\" rel=\"noopener noreferrer\">derGestalterCottbus<\/a>&#8221; on <a href=\"https:\/\/pixabay.com\/de\/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=3308818\" target=\"_blank\" rel=\"noopener noreferrer\">Pixabay.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Timer and PWM functions of the ATmega328P have only been partially transferred to the Arduino world. I show in detail how to use them in the Arduino IDE. <\/p>\n","protected":false},"author":1,"featured_media":8307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[543],"tags":[989,980,991,1006,988,997,992,1004,999,985,1001,983,973,977,979,993,996,1005],"class_list":["post-9767","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-boards-and-microcontrollers","tag-compare-match-en","tag-counter-en","tag-ctc-en-2","tag-duty-cycle-en","tag-fast-pwm-en-2","tag-isr-en","tag-normal-mode-en","tag-output-compare-en-2","tag-phase-correct-pwm-en-2","tag-phasenkorrekte-pwm-en","tag-prescaler-en","tag-pwm-en","tag-timer-en","tag-timer-0-en-2","tag-timer-2-en-2","tag-timer0_ovf_vect-en","tag-timer2_ovf_vect-en-2","tag-wave-form-generation-mode-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Timer and PWM - Part 1 (8-Bit Timer0\/2) Timer and PWM - Part 1 (8-Bit Timer0\/2)%&#8226;Wolles Elektronikkiste% % &#8226; Wolles Elektronikkiste<\/title>\n<meta name=\"description\" content=\"Timer and PWM functions of the ATmega328P have only been partially transferred to the Arduino world. I show in detail how to use them in the Arduino IDE.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Timer and PWM - Part 1 (8-Bit Timer0\/2) Timer and PWM - Part 1 (8-Bit Timer0\/2)%&#8226;Wolles Elektronikkiste% % &#8226; Wolles Elektronikkiste\" \/>\n<meta property=\"og:description\" content=\"Timer and PWM functions of the ATmega328P have only been partially transferred to the Arduino world. I show in detail how to use them in the Arduino IDE.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2\" \/>\n<meta property=\"og:site_name\" content=\"Wolles Elektronikkiste\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-20T18:16:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-29T08:02:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Beitragsbild.png\" \/>\n\t<meta property=\"og:image:width\" content=\"812\" \/>\n\t<meta property=\"og:image:height\" content=\"810\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Wolfgang Ewald\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Wolfgang Ewald\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"20 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2\"},\"author\":{\"name\":\"Wolfgang Ewald\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"headline\":\"Timer and PWM &#8211; Part 1 (8-Bit Timer0\\\/2)\",\"datePublished\":\"2020-11-20T18:16:37+00:00\",\"dateModified\":\"2024-10-29T08:02:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2\"},\"wordCount\":3371,\"commentCount\":22,\"publisher\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"image\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/Beitragsbild.png\",\"keywords\":[\"compare match\",\"counter\",\"CTC\",\"duty cycle\",\"fast PWM\",\"ISR\",\"normal mode\",\"output compare\",\"phase-correct PWM\",\"phasenkorrekte PWM\",\"prescaler\",\"PWM\",\"timer\",\"timer 0\",\"timer 2\",\"TIMER0_OVF_vect\",\"TIMER2_OVF_vect\",\"wave form generation mode\"],\"articleSection\":[\"Boards and Microcontrollers\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2\",\"name\":\"Timer and PWM - Part 1 (8-Bit Timer0\\\/2) Timer and PWM - Part 1 (8-Bit Timer0\\\/2)%&#8226;Wolles Elektronikkiste% % &#8226; Wolles Elektronikkiste\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/Beitragsbild.png\",\"datePublished\":\"2020-11-20T18:16:37+00:00\",\"dateModified\":\"2024-10-29T08:02:02+00:00\",\"description\":\"Timer and PWM functions of the ATmega328P have only been partially transferred to the Arduino world. I show in detail how to use them in the Arduino IDE.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2#primaryimage\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/Beitragsbild.png\",\"contentUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/12\\\/Beitragsbild.png\",\"width\":812,\"height\":810},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\\\/timer-and-pwm-part-1-8-bit-timer0-2#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Startseite\",\"item\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Timer and PWM &#8211; Part 1 (8-Bit Timer0\\\/2)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#website\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en\",\"name\":\"Wolles Elektronikkiste\",\"description\":\"Die wunderbare Welt der Elektronik\",\"publisher\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/en#\\\/schema\\\/person\\\/b774e4d64b4766889a2f7c6e5ec85b46\",\"name\":\"Wolfgang Ewald\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"url\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"contentUrl\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\",\"width\":512,\"height\":512,\"caption\":\"Wolfgang Ewald\"},\"logo\":{\"@id\":\"https:\\\/\\\/wolles-elektronikkiste.de\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/cropped-Logo-1.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Timer and PWM - Part 1 (8-Bit Timer0\/2) Timer and PWM - Part 1 (8-Bit Timer0\/2)%&#8226;Wolles Elektronikkiste% % &#8226; Wolles Elektronikkiste","description":"Timer and PWM functions of the ATmega328P have only been partially transferred to the Arduino world. I show in detail how to use them in the Arduino IDE.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2","og_locale":"en_US","og_type":"article","og_title":"Timer and PWM - Part 1 (8-Bit Timer0\/2) Timer and PWM - Part 1 (8-Bit Timer0\/2)%&#8226;Wolles Elektronikkiste% % &#8226; Wolles Elektronikkiste","og_description":"Timer and PWM functions of the ATmega328P have only been partially transferred to the Arduino world. I show in detail how to use them in the Arduino IDE.","og_url":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2","og_site_name":"Wolles Elektronikkiste","article_published_time":"2020-11-20T18:16:37+00:00","article_modified_time":"2024-10-29T08:02:02+00:00","og_image":[{"width":812,"height":810,"url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Beitragsbild.png","type":"image\/png"}],"author":"Wolfgang Ewald","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Wolfgang Ewald","Est. reading time":"20 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2#article","isPartOf":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2"},"author":{"name":"Wolfgang Ewald","@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"headline":"Timer and PWM &#8211; Part 1 (8-Bit Timer0\/2)","datePublished":"2020-11-20T18:16:37+00:00","dateModified":"2024-10-29T08:02:02+00:00","mainEntityOfPage":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2"},"wordCount":3371,"commentCount":22,"publisher":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"image":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2#primaryimage"},"thumbnailUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Beitragsbild.png","keywords":["compare match","counter","CTC","duty cycle","fast PWM","ISR","normal mode","output compare","phase-correct PWM","phasenkorrekte PWM","prescaler","PWM","timer","timer 0","timer 2","TIMER0_OVF_vect","TIMER2_OVF_vect","wave form generation mode"],"articleSection":["Boards and Microcontrollers"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2#respond"]}]},{"@type":"WebPage","@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2","url":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2","name":"Timer and PWM - Part 1 (8-Bit Timer0\/2) Timer and PWM - Part 1 (8-Bit Timer0\/2)%&#8226;Wolles Elektronikkiste% % &#8226; Wolles Elektronikkiste","isPartOf":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#website"},"primaryImageOfPage":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2#primaryimage"},"image":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2#primaryimage"},"thumbnailUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Beitragsbild.png","datePublished":"2020-11-20T18:16:37+00:00","dateModified":"2024-10-29T08:02:02+00:00","description":"Timer and PWM functions of the ATmega328P have only been partially transferred to the Arduino world. I show in detail how to use them in the Arduino IDE.","breadcrumb":{"@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2#primaryimage","url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Beitragsbild.png","contentUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/12\/Beitragsbild.png","width":812,"height":810},{"@type":"BreadcrumbList","@id":"https:\/\/wolles-elektronikkiste.de\/en\/timer-and-pwm-part-1-8-bit-timer0-2#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Startseite","item":"https:\/\/wolles-elektronikkiste.de\/en"},{"@type":"ListItem","position":2,"name":"Timer and PWM &#8211; Part 1 (8-Bit Timer0\/2)"}]},{"@type":"WebSite","@id":"https:\/\/wolles-elektronikkiste.de\/en#website","url":"https:\/\/wolles-elektronikkiste.de\/en","name":"Wolles Elektronikkiste","description":"Die wunderbare Welt der Elektronik","publisher":{"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wolles-elektronikkiste.de\/en?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/wolles-elektronikkiste.de\/en#\/schema\/person\/b774e4d64b4766889a2f7c6e5ec85b46","name":"Wolfgang Ewald","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","url":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","contentUrl":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png","width":512,"height":512,"caption":"Wolfgang Ewald"},"logo":{"@id":"https:\/\/wolles-elektronikkiste.de\/wp-content\/uploads\/2019\/03\/cropped-Logo-1.png"}}]}},"_links":{"self":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/9767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/comments?post=9767"}],"version-history":[{"count":0,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/posts\/9767\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/media\/8307"}],"wp:attachment":[{"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/media?parent=9767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/categories?post=9767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wolles-elektronikkiste.de\/en\/wp-json\/wp\/v2\/tags?post=9767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}