I am new to MCUs. I did notice that the AVR chip has something called Pulse Width Modulation. Could someone tell me what PWM is and how its used? Or if there is a web site with a description.
admin's test signature
PWM stands for Pulse Width Modulation.
Basically it is a square wave with a varying duty cycle. First you setup the frequency of the pulse and then you set the duty cycle.
By adding a low pass filter on the end of the output your MCU can output an analog output. If you check the Atmel webpage they use PWM to output a sample of voice recording. (See appnote 335)
What else can it be used for?
I use it for motor control. Attach the output pin to the base of a transistor, then when you set the duty cycle you are setting the voltage and hence speed of the motor.
Also the servos in RC cars a planes set there angles using the duration of the high pulse. This is a great example. A servo requires a pulse every 20ms. If the pulse is up for 1.5 ms the angle is 0. When the pulse is extended to 1.75ms the servo moves 90 degrees clockwise, and if the pulse is shortend to 1.25ms the servo moves 90 degrees counter clockwise.
So now we setup up the PWM frequency to be 1/20ms=50Hz. If we have an 8-bit PWM channel then to 0 the servo position we put
We put 19 into the PWM register. As we increment the register the servo moves clockwise at about 28 degrees per bit.
Hope this wasn't too much,
unfortunattly (and it's not explained explicitly in the user manual) you can't use
the PWM in every frequency you want to, but only in a few frequencys (fclk/2 , fclk/4 ..)!!!!!!!!!!!!!
it's a huge disadvantage .
I'm trying to get the PWM working using the Codevision compiler. Unfortunately it does not have and "canned" configuration or update routines that it ships with and the documentation I can find with the compiler is not clear on how to setup a pwm and what registers to write the duty cycle and frequency to so that the duty cycle register can be updated on the fly for a poor mans DAC. If there was some canned code just for PWM setup / duty cycle updating it would be very helpful.
I am a teaching assistant on an Embedded Systems course that uses the AVR (4414) to control a small model car. There is one servo, and one servo-like motor on the car, both using PWM for control.
There has been some, but very little success in using the inbuild PWM mode to drive the servos. The reason for this is the difficulty in obtaining the proper pulse frequency with "standard" frequencies (old cars used 2MHz, new ones we have use 4MHz). You can get close, but not really close enough for proper control.
We bypass this problem by simulating the PWM by hand. You can still cause OC1B and OC1A to toggle using a series of compare matches, and setting the TCCR1A/TCCR1B so that the proper toggle occurs.
At 4MHz, we need to use CK/8 for the TC1 since full pulse lengh doesn't fit so well at CK (16 bit limit). This gives about 2e-6s steps (though as you can imagine, this is hardly required) for the pulse width, and hence very fine control. Usually something like 0.05ms works fine as a step for an RC servo.
If anybody would like example code, please contact me directly by email at Marcin.Dobrucki@hut.fi, I will not post the code here as some of our students might be reading ;)
© 2020 Microchip Technology Inc.