0% found this document useful (0 votes)
266 views

PWM Programming in dsPIC

This document discusses pulse width modulation (PWM) using a dsPIC30F microcontroller. It explains why PWM is used, how to configure PWM with MPLAB, and how to calculate the switching frequency. Applications of PWM for motor speed control using an H-bridge motor driver circuit and L293D chip are also covered. The document provides code examples and instructions for a laboratory experiment to control a DC motor's speed and direction using PWM.

Uploaded by

Arkendu Mitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views

PWM Programming in dsPIC

This document discusses pulse width modulation (PWM) using a dsPIC30F microcontroller. It explains why PWM is used, how to configure PWM with MPLAB, and how to calculate the switching frequency. Applications of PWM for motor speed control using an H-bridge motor driver circuit and L293D chip are also covered. The document provides code examples and instructions for a laboratory experiment to control a DC motor's speed and direction using PWM.

Uploaded by

Arkendu Mitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

5/8/2012

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

a) Switching frequency and duty cycle

b) PWM Configuration

OpenMCPWM(period, sptime, config1, config2, config3)

ConfigIntMCPWM(config)

CloseMCPWM()

2
5/8/2012

b) OpenMCPWM: Enable PWM module

OpenMCPWM(period, sptime, config1, config2, config3)

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

b) OpenMCPWM: Enable PWM module

OpenMCPWM(period, sptime, config1, config2, config3)

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

b) OpenMCPWM: Enable PWM module

OpenMCPWM(period, sptime, config1, config2, config3)

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

//PWM Pins Enable


PWM_PEN3H : PWM3H is set to PWM
PWM_PDIS3H : PWM3H is set to general IO
PWM_PEN2H : PWM2H is set to PWM
PWM_PDIS2H : PWM2H is set to general IO

b) OpenMCPWM: Enable PWM module

OpenMCPWM(period, sptime, config1, config2, config3)

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

b) OpenMCPWM: Enable PWM module

OpenMCPWM(period, sptime, config1, config2, config3)


config3
//Special
PWM_SERVOPS1 : Prescalar 1:1
PWM_SERVOPS2 : Prescalar 1:2
:
:
PWM_SERVOPS15 : Prescalar 1:15
PWM_SERVOPS16 : Prescalar 1:16

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

b) Switching Frequency calculation

Switching frequency of PWM can be set by:

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

b) OpenMCPWM: Enable PWM module

OpenMCPWM(period, sptime, config1, config2, config3)

period
From calculation, with crystal of 7.3728 MHz, desire frequency is 1000 Hz.
Set Prescalar 1:1, PLL to 4 and
period = 7372

From calculation, with crystal of 4 MHz, desire frequency is 1000 Hz.


Set Prescalar 1:1, PLL to 4 and
pperiod = ????

sptime
sptime = 0x0;

b) PWM pins

PWM 1‐3

6
5/8/2012

b) Setting PWM

**Add pwm.h in the header

OpenMCPWM(period, 0x0, config1, config2, config3)

SetDCMCPWM(dutycyclereg, dutycycle, updatedisable);

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%.

Show each state in Hyperterminal. Draw your output waveform with


specified duty cycle and switching frequency.

Note: Set RE0 and RE1 as PWM function.

7
5/8/2012

c) PWM Application: Motor Control (H-bridge)

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!

c) PWM Application: Motor Control

Line driver (buffer) is used to prevent damaged to the micro controller.

H-bridge is used to drive motor in both direction.

8
5/8/2012

c) PWM Application: Motor Control (buffer: 74LS244)

c) PWM Application: Motor Control (Motor Driver:


L293D)
Do not look at pins arrangement with this diagram

9
5/8/2012

c) PWM Application: Motor Control (Motor Driver:


L293D)

c) PWM Application: Motor Control with L293D

CW: IN1 (0), IN2(1)


CCW: IN1 (1), IN2(0) Switching frequency affect the characteristics
of motor and loss of the power
Speed control from duty cycle

10
5/8/2012

Lab8B) Speed Control of 5.9V motor

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%.

Task2: Modification of the existing codes to:


Add slow stop IN1(0) and IN2(0) to stop the motor before change the direction
of rotation.
a) When pressing SW0: set duty cycle to 0%.
b)) When ppressing
g SW1: slow stop,p, CW and set dutyy cycle
y to 45%.
c) When pressing SW2: slow stop, CCW and set duty cycle to 85%.

Show each state (direction and duty cycle) in Hyperterminal.


Note: Set RE0 and RE1 as PWM function.

L293D may burn if you did not stop it before reversal of rotation!! (50฿)!!

11

You might also like