[en] Arduino - Timer1 - 8, 9 and 10-bit PWM

Sdílet
Vložit
  • čas přidán 31. 05. 2024
  • In Arduino we use 8-bit PWM. We use it because the function analogWrite is so programmed. However, the ATmega328P microcontroller also has better options. Timer1 is 16-bit and is therefore able to generate higher resolution PWM. We will learn most about its possibilities in the datasheet in Table 15-5 in the TCCR1A register. We have 16 modes available that we can use.
    Arduino uses mode 1. According to the table we can decrypt it as PWM, phase correct, 8-bit. According to the table, we can see that just below mode 1 there are others that have 9 or 10 bit resolution. We see that there are several modes in the table with quite similar names. How to choose between them? Because we will use LEDs in this example, we don't have to worry too much about it and we will use the simplest of them to understand. We will use Fast PWM mode. It differs from the other modes in that it has twice the frequency. In this video, we will not change the frequency relative to the Arduino, because the original setting will suit us for a small LED. But it is good to know that the frequency is affected by the prescaler setting, which can be set using the CS bits in the TCCR1B register. And for each mode, you will also find a formula in the datasheet to calculate the frequency.
    Let's go back to the PWM settings themselves. For this example, we will use modes 5, 6 and 7. We will switch them one by one and watch the brightness of the LED change. We will compare the brightness with the reference value on timer 2, which will still have 8 bits. We should see that in 8-bit mode, both LEDs should light approximately the same, and after switching to 9 bits, the brightness should decrease and after switching to 10 bits, the brightness should decrease even more. Why does this happen? Because at a higher bit resolution, the timer counts down to a larger number and at the same PWM value that is set in the OCR register, there will be a higher ratio between the signal off and the on. To see this best, we will use the minimum PWM value that I set to 1 with the analogWrite function.
    The connection of the electrical circuit does not need special explanation. The LED together with the resistor is on pin 9 and 11. On pin 9 there is a PWM output for timer 1 and on pin 11 there is a PWM output for timer 2. The brightness of the LED on pin 9 changes as expected and we can also check it on the serial port. A description of the program together with the source codes can be found in the linked article, where everything is explained in detail.
    Article
    www.arduinoslovakia.eu/blog/2...
  • Věda a technologie

Komentáře • 1

  • @sewingmachinesindetail
    @sewingmachinesindetail Před 7 měsíci

    Thank you very much for this video. I am very new to Arduino, but have been designing a lot of electronics including microprocessors - but mostly 30 years back. I have looked at Arduino control of a more powerful DC-motor for a sewing machines. It should be able to control a peak power of about 400 W. A module called IBT-2 can do that, but its max frequency is only 25 kHz. So I look for a way to make about 20 kHz PWM-signal to get rid of audio noise from motor. So I guess, that it would be possible with a setting of no prescaling and using the 9 bit option for PWM and set some start and stop counter values, so you get 20 kHz. Do you agree?