Documente Academic
Documente Profesional
Documente Cultură
What do you exactly need? A constant time period square wave whose duty cycle we can controlincode,right? Clockperiodofamicrocontrolleris:
.V
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
Youmustbewonderingwherewherewillthissqurewavewill 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
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