Laboratory: Introduction to Mechatronics Instructor TA: Edgar Martinez Soberanes (eem370@mail.usask.ca) 2017-03-15 Lab 5. DC Motor Speed Control Using PWM
Lab Sessions Lab 1. Introduction to the equipment and tools to be used in the lab, which include the development board (PICDEM 2 Plus),the microcontroller (PIC 16F1937), and the USB Oscilloscope (Analog Discovery). Lab 2. Experiment 2: Transistor Operation, Experiment 3: Passive filters, and an Op-Amp circuit experiment. Lab 3. Experiment 7: LED Control and Interruptions. Lab 4. Experiment 10: Stepper Motor Motion Control. Lab 5. Experiment 11: DC Motor Speed Control Using PWM. Lab 6. Experiment 12: Closed Loop DC Motor Position Control. 2
Lab 1. Outline Objective. Lecture o o Pulse Width Modulation (PWM). H-Bridge Circuit Time for the experiments. 3
Objective Objectives o To control the speed of a DC motor using a PWM signal from the PIC microcontroller in conjunction with an H-bridge amplifier circuit 4
Pulse Width Modulation (PWM) PWM signals are ON OFF signals (Pulses) whose HIGH or ON duration is changed (Width Modulation) in accordance with our requirements. o PWM is one of the simple and most commonly used techniques to produce analog voltages from digital signals. o Applications: Digital to Analog Converter (DAC), DC Motor Speed Control, Sine Wave Inverters, Brightness control etc. 5
This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. 6
The PWM frequency is generally constant in the khz range. It has to be high enough to be seen by the powered system as a continuous signal. Duty Cycle is the percentage time that the signal is in the ON state versus the OFF state. DDDDDDDD CCCCCCCCCC % = PPPPPPPPPP WWWWWWWWW PPPPPPPPPPPP 100 7
PWM in the PIC microcontroller PWM signal can be easily generated using CCP modules available in the PIC microcontroller. CCP stands for Capture / Compare / PWM, which means that it can be used for Capture or Compare or PWM operations. The PWM standard mode makes use of one of the 8-bit timers (Timer2/4/6) to specify the PWM period. The period, duty cycle, and resolution are controlled by the following registers: o PRx registers o TxCON registers o CCPRxL registers o CCPxCON registers 8
The PWM period is specified by the PRx register of Timer2/4/6. The PWM period can be calculated using the formula: PPPPPP PPPPPPPPPPPP = (PPPPPP + 1)(4) 1 FFFFFFFF TTTTTTTT PPPPPPPPPPPPPPPP VVVVVVVVVV The PWM duty cycle is specified by writing a 10-bitvalue to two registers: CCPRxL register and DCxB<1:0> bits of the CCPxCON register. TheCCPRxL contains the eight MSbs and the DCxB<1:0>bits of the CCPxCON register contain the two LSbs. PPPPPPPPPP wwwwwwwwww = (CCCCCCCC) 1 FFFFFFFF TTTTTTTT PPPPPPPPPPPPPPPP VVVVVVVVVV 9
Example Suppose PR register of Timer2 has a value of 0x50 and the Internal clock is set at 4MHz. We want to determine the PWM Period and the CCPR value that would result in a Duty Cycle of 50%. PPPPPP PPPPPPPPPPPP = (PPPPPP + 1)(4) 1 FFFFFFFF TTTTTTTT PPPPPPPPPPPPPPPP VVVVVVVVVV = 80 + 1 4 1 4MMMMMM 1 = 8888 nnnn PPPPPPPPPP wwwwwwwwww = (CCCCCCCC) 1 FFFFFFFF TTTTTTTT PPPPPPPPPPPPPPPP VVVVVVVVVV CCPR = PPPPPPPPPP wwwwwwwww FFFFFFFF TTTTTTTT PPPPPPPPPPPPPPPP VVVVVVVVVV CCPR = 40.5 nnnn 4MMMMMM 1 = 111111 = 0000 11111111 00000000 10
Summary To setup a PWM signal in the PIC: 1.Select a timer to use (Timer 2/4/6) 2.Set the PWM period by writing a value in the PRx register of the Timer chosen. 3.Define the Duty Cycle by writing the CCPR value. 11
H-Bridge Circuit The H-bridge circuit enables a voltage to be applied across a load in either direction. In DC motor applications this is required to be able to change direction of rotation. A H-bridge comprises four MOSFET power transistors (two PNP and two NPN) 12
PWM Signal 1 PWM Signal 2 13
14
Code Example 15
Thanks 16