PWM Programming in dsPIC
PWM Programming in dsPIC
dsPIC30F Programming with MPLAB
PWM: Pulse Width Modulation
1) Asst. Prof. Dr. Kittiphan Techakittiroj, Ph.D
2) Dr. Narong Aphiratsakun, D.Eng
Assumption University
Faculty of Engineering
a) Why PWM
1
5/8/2012
b) PWM Configuration
ConfigIntMCPWM(config)
CloseMCPWM()
2
5/8/2012
config1
fg
//On/Off PWM module
PWM_EN : Enable PWM
PWM_DIS : Disable PWM
//Idle Mode
PWM_IDLE_CON : TPWM still working in idle mode
PWM_IDLE_STOP : PWM stop working in idle mode
//Output Prescalar
PWM_OP_SCALE1 : Prescalar 1:1
PWM_OP_SCALE2 : Prescalar 1:2
:
:
PWM_OP_SCALE15 : Prescalar 1:15
PWM_OP_SCALE16 : Prescalar 1:16
config1
fg
//Input Prescalar
PWM_IPCLK_SCALE1 : Prescalar 1:1
PWM_IPCLK_SCALE4 : Prescalar 1:4
PWM_IPCLK_SCALE16 : Prescalar 1:16
PWM_IPCLK_SCALE64 : Prescalar 1:64
//PWM Mode
PWM MOD FREE
PWM_MOD_FREE : Free mode (Edge aligned )
PWM_MOD_SING : Single event
PWM_MOD_UPDN : Count up and count down (center aligned)
PWM_MOD_DBL : Center aligned with double update
3
5/8/2012
config2
fg
//Complementary PWM or independent PWM
PWM_MOD3_COMP : PWM3 as module (dead band will be set)
PWM_MOD2_COMP : PWM2 as module (dead band will be set)
PWM_MOD1_COMP : PWM1 as module (dead band will be set)
PWM_MOD3_IND : PWM3 as independent
PWM_MOD2_IND : PWM2 as independent
PWM_MOD1_IND : PWM1 as independent
config2
fg
//PWM Pins Enable
PWM_PEN1H : PWM1H is set to PWM
PWM_PDIS1H : PWM1H is set to general IO
PWM_PEN3L : PWM3L is set to PWM
PWM_PDIS3L : PWM3L is set to general IO
PWM_PEN2L : PWM2L is set to PWM
PWM_PDIS2L : PWM2L is set to general IO
PWM PEN1L
PWM_PEN1L : PWM1L is set to PWM
PWM_PDIS1L : PWM1L is set to general IO
4
5/8/2012
//Output
p Synchronous
y mode
PWM_OSYNC_PWM : Register OVDCON is updated with PWM timer
PWM_OSYNC_Tcy : Register OVDCON is updated with next cycle
clock
//PWM is updated
PWM_UEN : Update PWM is enable
PWM_UDIS : Update PWM is disable
FCY
PTPER = 4
⎛ FPWM ⎞
⎜ ⎟ *( PTMR Pr escaler )
⎝ PLL ⎠
Desired switching frequency will depends on PLL: Take PLL4 for this case.
With crystal = 7.3728 MHz, and choose Prescaler as 1, desired switching
frequency = 1000 Hz.
⎛ 7.3728
7 3728M M ⎞
⎜ ⎟
PTPER = ⎜ 4 ⎟ − 1 = 7371.8 ≈ 7372
⎜ ⎛ 1000 ⎞ *(1) ⎟
⎜⎜ 4 ⎟ ⎟
⎝⎝ ⎠ ⎠
5
5/8/2012
period
From calculation, with crystal of 7.3728 MHz, desire frequency is 1000 Hz.
Set Prescalar 1:1, PLL to 4 and
period = 7372
sptime
sptime = 0x0;
b) PWM pins
PWM 1‐3
6
5/8/2012
b) Setting PWM
where
dutycyclereg = 1; //PWM1
dutycycle = (period/2)<<1; //PDCx<15:1>
updatedisable = 0;
Lab8A) PWM
Task1: Obtain PWM output from RE0 and RE1 with desire frequency
of 2 KHz. Measure with oscilloscope.
a) When pressing SW0: set duty cycle to 0%.
b) When pressing SW1: set duty cycle to 90%.
c) When pressing SW2: set duty cycle to 50%.
7
5/8/2012
Q1 and Q4 are “ON”, Q2 and Q3 are “OFF” : Motor rotate in clockwise direction
Q2 and Q3 are “ON”, Q1 and Q4 are “OFF” : Motor rotate in counter clockwise
di ti
direction
NOTE:
Q1 and Q3 Can’t be “ON” at the same time!
Q2 and Q4 Can’t be “ON” at the same time!
8
5/8/2012
9
5/8/2012
10
5/8/2012
Previous Task: Obtain PWM output from RE0 and RE1 with desire frequency
of 2 KHz.
a) When pressing SW0: set duty cycle to 0%.
b) When
Wh pressing
i SW1:
SW1 sett duty
d t cycle
l to
t 90%.
90%
c) When pressing SW2: set duty cycle to 50%.
L293D may burn if you did not stop it before reversal of rotation!! (50฿)!!
11