# Understanding PWM after years of use

Usually you just use the PWM or the timer, but recently when I needed to do more complex stuff with timers(Zero board) I could see there are some things I really don't get:

1.Why Timer resolution matter ?

Timer resolution is how much it can count to before it overflow. So for example if you want to create a square wave, with a 48Mhz clock, if the resolution is 8bits, it counts to 255, and if its 16bits its 65,535, but why is this matter, when you can get the interrupt every tick(at 48Mhz), then you write a code that divided it by any number you like by incrementing an integer on the interrupt, then skip every 2 ticks is to divided it by 2, and every 4 is to divided it by 4, you can now go forever and have as many bits resolution you like?? (is it all about the fact that the processor do this calculation in the background, not spending time?)

1. Why PWM timer pins matter ?

Why there must be a physical connection between a PWM signal generated by a timer, to pin on the chip? when you count, you can then output high/low at every pin you like, is there any different ?

1. Why PWM channels matter ?

What are channels even means? what really matter is how many independent timers you have, if you have 3 then you can create 3 PWM pins. But if you have 1 timer and 6 channel PWM what is it matter ? the whole 6 would have the same frequency because you have 1 timer, isn't it ??

1. Maybe I am wrong but if you want a 50% duty cycle, you can only set the timer counter in increments of 2, which means you can count : 2,4,6,8,10... but then you get such a bad resolution for the first frequencies, and all the good resolution is down there in the big numbers, so for example 48Mhz between counter 10 to 12 is going from 4.8Mhz to 3Mhz- and you can never get the middle frequencies . Is there a way to solve that ??

There something wrong with my conception of the PWM thing I think.

1) Because you need the resolution for the PWM. If you just want a square wave and don't care about the duty cycle things are easier of course. And what you describe is called software PWM. Of course you can do that, but the cool thing about hardware PWM is that is takes no CPU time at all. You set it up and it just runs. If you set it to 40% then it will stay there, even if you turn off interrupts and have an empty loop. It's all in hardware.

2) Again, the beauty of hardware. If you want to do it in software then of course you can use any pin you like.

3) The channels that are on the same timer indeed have the same frequency. But in most cases that doesn't matter (or is even desired) because you're interested in the duty cycle. That's where the information is with PWM. So 6 channels on a single timer is just fine, you can set duty cycle for each channel so control what you like.

4) Why do you keep changing frequency? That's not where the information of PWM is. It's Pulse Width Modulation, not Pulse Frequency Modulation.

So yeah, the biggest things you have wrong are - PWM pins are PWM pins because it's a [u]hardware[/u] feature of the pin. Of course, with enough speed you can do all kind of stuff with every IO in software but that's not the point - PWM is about changing the duty cycle, not about changing the frequency. Frequency is more or less a fixed parameter (you set it but don't control with it).

Ok thats what I thought, its all about the CPU, you free up space and its in the background.
Sometimes you want to output square waves in different frequencies and analyse other stuff, not everything in the world is about PWM
(in this case you use PWM with constant duty cycle and change the frequency)

Thanks.

Then you don't use PWM, then you (ab)use the PWM hardware ;) What you do then is in software but isn't PWM.

Keep in mind that the PWM frequency is important in many situations...

for example, if you're driving a motor or solenoid, the 'magnetics'could 'sing' or resonate with the PWM frequency, or if the frequency is too high, and there's significant capacitance or inductance in your load - the response can become sluggish.

For a basic understanding, notwithstanding the above... take a look at 'duty cycle'.