Professional Documents
Culture Documents
PWM in AVR v1.0
PWM in AVR v1.0
What do you exactly need? A constant time period square wave whose duty cycle we can controlincode,right? Clockperiodofamicrocontrolleris:
.V
I answer this in respect to our ROBOT here you are having 300RPM motor and voltage rating is 12 Volt so if you want to run motor to 150RPM then what will you do?? Reduce voltage to 6 voltbutthatsnotgoodideaRPMisnotlinearfunctionofvoltageandasvoltagedecreasefrom specifiedratingTorquewillalsogetreduce.SoherecomePWMconceptwhatyoucandoisyou canswitchonandswitchoffthemotorrepetitivelysuchthateffectivelyyouget50%ONtimeif youdoitreasonablyfastthenmotorseemsrunningcontinuous.
is io n
~1~
R ob
o.
co
E.g.InATmega8defaultclockfrequencyisnearabout1MegaHz.
Tsystem-clk=1/(1M Hz)=1 s
Togetdesirefrequencyyouneedtochangeclockingoftimerthatisdonebyprescalerso
** TocontrolDCmotorneeddualslopephasecorrectPWMthiscomplicatedterm
onlymeanthatcounterwillcountfromTOPtozeroandZerotoTOPintimeperiod.So timeperiodof
Time period of the square wave is decided by 2 variables Tclk TOP T= Tclk*TOP or T= Tclk*TOP *2
.V
We adjust both variables to get desired time period by changing some control bits in control registers. HereinthistutorialwewillsetTOP=255 DutycycleisdecidedbyonlyonevariableOCR Dutycycle=(OCR/TOP)
is io n
~2~
R ob
o.
co
if
then
TOP
Timer value
.V
is io n
R ob
~3~
o.
co
When if
then
o.
Intmain() { Step1SetPB1(OC1A)asoutputpin;
While(1) {
} Return0;
.V
//doanyjobhere
is io n
~4~
Now suppose you want to generate a square wave that has 50%dutycycleatpin15(OC1A)Thenhowyourcodewilllooklike
R ob
You must be wondering where where will this squre wave will appear.InavrmicrocontrollerthereiscertainPWMpinswhere thesewavewillapear.LikeinATmega8PIN15,16and17.Ifyou want to control only 2 DC motor then 15 and 16 pins are sufficient.
NowifyouconnectOscilloscopeatpin15youcanseeasquarewavewith50%dutycycle
co
Bit 7:6 COM1A1:0: Compare Output Mode for channel A Bit 5:4 COM1B1:0: Compare Output Mode for channel B
Compare Output Mode, Phase Correct and Phase and Frequency Correct PWM
.V
is io n
TheCOM1A1:0andCOM1B1:0controltheOutputComparePins(OC1AandOC1B(respectively) behavior. If one or both of the COM1A1:0 bits are written to one, the OC1A output overrides the normal port functionality of the I/O pin it is connected. Similarly for COM1B1:0. However, notethattheDataDirectionRegister(DDR)bitcorrespondingtotheOC1AorOC1Bpinmustbe written1inordertoenabletheoutputdriver.
R ob
~5~
o. w
Bit 3 FOC1A: Force Output Compare for channel A Bit 2 FOC1B: Force Output Compare for channel B
These bits are not used in phase correct PWM set write there bit 0;
Functionality of WGM11 and WGM10 will be given later
co
ThesebitsisnotusedinPWMsimplywritethesebit0;
The three clock select bits select the clock source to be used by the Timer/Counter, Clock Select Bit Description
Default No clock source is selected. as you select clock PWM will start
WGM
WGM13 0
stand for Wave generation mode. There are several PWM and time mode here we want to do setting for phase correct PWM for TOP=255 and for that you need to write
WGM12 0 WGM11 0 WGM10 1
.V
is io n
~6~
R ob
o.
co
.V
is io n
~7~
R ob
o.
co
DDRB|=(1<<PORTB1); Step2EnablePWM;
Step3SelectPhasecorrectPWMmodeandTOPvalue;
Here we will go for TOP=255 WGM13 0 WGM12 0 WGM11 0 WGM10 1
Step4SetOCRvalueTOP/2;
Code:
OCR1A=128;
.V
Code: TCCR1A=0xA1;
is io n
~8~
R ob
and COM1A11=1
#include<avr/io.h> Intmain() { Step1SetPB1(OC1A)asoutputpin; Step2EnablePWM; Step3SelectPhasecorrectPWMmodeandTOPvalue; Step4SetOCRvalueTOP/2; Step5Setprescalervalueandclocksource; Step6 StartPWM; While(1) { //doanyjobhere } Return0; }
o.
co
Code:
TCCR1B=0x05;
Step6StartsPWM;
Step5willdothisjob
#include<avr/io.h> intmain()
{
YoucanalsocheckPWMwithmultimeterhereyouwillsevoltagechanginginmultimeterbutif setsquarewavetimeperiodsufficientlow.youwillseeaveragevalueinmultimeter
.V
HAPPYPWMCODING
~9~
is io n
R ob
o.
co