You are on page 1of 10

PWM IN AVR

Developed by: Krishna Nand Gupta Prashant Agrawal Mayur Agarwal

PWM (pulse width Modulation)


What is PWM?

Frequency = (1/T) Duty Cycle = (Thigh/T) What is need of PWM?

What do you exactly need? A constant time period square wave whose duty cycle we can controlincode,right? Clockperiodofamicrocontrolleris:

How PWM is implemented in AVR?

.V

IanswerthisinrespecttoourROBOThereyouarehaving300RPMmotorandvoltageratingis 12Voltsoifyouwanttorunmotorto150RPMthenwhatwillyoudo??Reducevoltageto6 voltbutthatsnotgoodideaRPMisnotlinearfunctionofvoltageandasvoltagedecreasefrom specifiedratingTorquewillalsogetreduce.SoherecomePWMconceptwhatyoucandoisyou canswitchonandswitchoffthemotorrepetitivelysuchthateffectivelyyouget50%ONtimeif youdoitreasonablyfastthenmotorseemsrunningcontinuous.

is io n
~1~

Tclk =1/(clock frequency)

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

ToimplementPWMthesevariablesarerequired: TOP=NumberofclockcyclesforonetimeperiodofPWM Nt=T/Tclk OCR=NumberofclockcyclesforOnTimeofPWM OCR(outputcompareregister) OCR=Ton/Tclk Timer_value=Itisvalueoftimer,thatcountsfromTOPtozeroandZerotoTOPineach cycle.

(dual slope phase correct PWM)

o.

co

Tclk = prescaler * Tsystem-clk

Duty cycle = (OCR/TOP)


And

if

then

TOP

Timer value

.V

is io n

R ob
~3~

o.

co

When if

Timer_value OCR Timer_value < OCR

then

PWM Output=Low PWM Output=High

o.

Intmain() { Step1SetPB1(OC1A)asoutputpin;

While(1) {

} Return0;

.V

Step2EnablePWM; Step3SelectPhasecorrectPWMmodeandTOPvalue; Step4SetOCRvalueTOP/2; Step5Setprescalervalueandclocksource; Step6StartPWM;

//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

YouhavelearntPWMjustcrazysyntaxpartofAVRPWMsettingisleft. NOTE:forthesakeofsimplicityofthistutorialhereIwillconsideronlyphasecorrectPWMtolearnmore aboutTimerandPWMgothroughdatasheetofATmega8

16-bit Timer/Counter Register Description


Timer/Counter 1 Control Register A TCCR1A

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.IfoneorbothoftheCOM1A1:0bitsarewrittentoone,theOC1Aoutputoverrides thenormalportfunctionalityoftheI/Opinitisconnected.SimilarlyforCOM1B1: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

Timer/Counter 1 Control Register B TCCR1B

Bit 7, BIT 6, BIT5

ThesebitsisnotusedinPWMsimplywritethesebit0;

Bit 2:0 CS12:0: Clock Select

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

*****For rest of the mode check ATmega8 data sheet

.V

Bit WGM13, WGM12 in TCCR1B and WGM11, WGM10 in TCCR1A

is io n
~6~

R ob

o.

co

Timer/Counter 1 TCNT1H and TCNT1L

Output Compare Register 1 A OCR1AH and OCR1AL

Output Compare Register 1 B OCR1BH and OCR1BL

.V

is io n
~7~

R ob

o.

co

Now we return to our code

DDRB|=(1<<PORTB1); Step2EnablePWM;

For this you need to write COM1A10=0

Step3SelectPhasecorrectPWMmodeandTOPvalue;
Here we will go for TOP=255 WGM13 0 WGM12 0 WGM11 0 WGM10 1

Step4SetOCRvalueTOP/2;
Code:

Step5Setprescalervalueandclocksource; Herewewillsetprescaler1024forthatweneedtowriteCS12=1,CS11=0,CS10=1 ****FordetailseeClockSelectBit

OCR1A=128;

.V

Code: TCCR1A=0xA1;

is io n
~8~

Step1SetPB1(OC1A)asoutputpin; Fordetailchecki/otutorial Code:

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

DDRB|=(1<<PORTB1); TCCR1A=0xA1; OCR1A=128; TCCR1B=0x05; While(1) { //anyjob } Return0;

R ob

o.

co

You might also like