ColorGrayscaleEE 200 Spring 2014Lab 18.EE 200Design ToolsLaboratory 18Professor Jeffrey SchianoDepartment of Electrical Engineering1EE 200 Spring 2014Lab 18.Laboratory 18 Topics• Pulse Width Modulation– Concept, motivation, and terminology– Realization using the dsPIC33 and C Programming Language– Realization using the myDAQ and the LabVIEW Programming Environment2EE 200 Spring 2014Lab 18.Pulse Width Modulation (PWM)• Common technique for controlling power flow• Drive the load with a square-wave cycle of fixed amplitude• Duty-cycle determines power delivered to the load3EE 200 Spring 2014Lab 18.PWM Motivation4SVcRLR1ΩLVSV[V]t10SVLR1ΩLVSV[V]t1025% Duty CycleWasted energy!EE 200 Spring 2014Lab 18.PWM Terminology5Duty Cycle = 100Davg H LV =DV + (1-D)VOn OffPeriod (T)TonVHVLonTFractional Duty Cycle = D = , 0 D 1TEE 200 Spring 2014Lab 18.dsPIC33 PWM Peripheral Pins6AN0/OA2OUT/RA0AN1/C2IN1+/RA1MCLRPGED3/VREF-/AN2/C2IN1-/SS1/RPI32/CTED2/RB0PGEC3/VREF+/AN3/OA1OUT/RPI33/CTED1/RB1PGEC1/AN4/C1IN1+/RPI34/RB2PGED1/AN5/C1IN1-/RP35/RB3VSSOSC1/CLKI/RA2OSC2/CLKO/RA3FLT32/RP36/RB4CVREF20/RP20/T1CK/RA4VDDPGED2/ASDA2/RP37/RB5AVDDAVSSPWM1LRPI47/ /T5CK/RB15PWM1HRPI46/ /T3CK/RB14RPI45/ /CTPLS/PWM2L RB13PWM2HRPI44/ /RB12PWTD M3I/RP43/ /L RB11PWTD M3O/RP42/ /H RB10VCAPVSSTMS/ASDA1/SDI1/RP41/RB9TCK/CVREF10/ASCL1/SDO1/RP40/T4CK/RB8SCK1/RP39/INT0/RB7PGEC2/ASCL2/RP38/RB6 Pins are up to 5V tolerant12345678910111213142827262524232221201918171615dsPIC33EP64MC502-I/SPEE 200 Spring 2014Lab 18.PWM Module Architecture7EE 200 Spring 2014Lab 18.dsPIC33 PWM Module Timing8PTPERPDC1EE 200 Spring 2014Lab 18.PWM Control Registers (x = 1,2, or 3)9• PTPER: PWM period as a multiple of clock cycles• PDCx: fractional duty cycle as a multiple of clock cycles• PTCON: PWM Time Base Control Register• PTCON2: PWM Primary Master Clock Divider Select Register• PWMCONx: PWM Control Register• IOCONx: PWM I/O Control Register• FCLCONx:PWM Fault Current-Limit RegisterEE 200 Spring 2014Lab 18.Determining PTPER and PDCx• FOSC = 7.37 MHz (internal RC oscillator)• Clock Period = 1/ (7.37 MHz)• PWM period = PTPER / (7.37 MHz)– Example: PWM period = 1 ms when PTPER = 7370• Duty Cycle = 100 PDCx / PTPER– Example: Duty cycle is 75% when PDCx = 0.75*PTPER• PTPER and PDCx are 16 bit registers– Values range from 0 to 216-1 – Max PWM period at FOSC is 8.5 ms10EE 200 Spring 2014Lab 18.PTCON11• Page 230 of dsPIC33 data sheet• For basic PWM operation, only the enable bit is of interest• Disable PWM Module– PTCONbits.PTEN = 0• Enable PWM Module– PTCONbits.PTEN = 1EE 200 Spring 2014Lab 18.PTCON212• Page 232 of dsPIC33 data sheet• To obtain larger PWM periods (lower frequencies) it is possible to divide down the master clock frequency• For our exercise we will divide by 1 (default value)EE 200 Spring 2014Lab 18.PWMCONx13• Page 235 of dsPIC33 data sheet• For basic PWM operation only the ITB and MDCS bits are of interest• Set ITB and MDCS to zero– PWMCONxbits.ITB = 0– PWMCONxbits.MDCS = 0– These are default valuesEE 200 Spring 2014Lab 18.IOCONx14• Page 240 of dsPIC33 data sheet• Assign control of the peripheral pins to the PWM output using PENH and PENL• Set the PWM mode using PMODEE 200 Spring 2014Lab 18.dsPIC33 PWM Modes• PWM mode determines relationship between the outputs PWMxL and PWMxH• PWM Modes:– Redundant– Complementary– Push-Pull15EE 200 Spring 2014Lab 18.Redundant PWM Mode:PWMxH = PWMxL16EE 200 Spring 2014Lab 18.FCLCONx17• Page 243 of dsPIC33 data sheet• For our exercise, disable the fault input– FCLCONbits.FLTMOD = 3EE 200 Spring 2014Lab 18.PWM Write-Protected Registers18• IOCONx and FCLCONx are write protected• Enable access by clearing the PWMLOCK configuration bit• Method for clearing PWMCLOCK– Use the Configuration Bits window in MPLAB X IDE to generate required source code – Copy the source code into the C programEE 200 Spring 2014Lab 18.PWMLOCK Configuration Bit19• Configuration registers allow the user to customize operation of the microcontroller• Device configuration registers are non-volatile locations in program memory that hold settings for the dsPIC33 during power-down• There are seven device configuration registers• PWMCLOCK is bit 6 in the FOSCSEL configuration register– FOSCSEL: Oscillator Source Selection Register– See Table 27-1, page 376, in the dsPIC33 data sheetEE 200 Spring 2014Lab 18.Configuration Byte Register Maps20EE 200 Spring 2014Lab 18.Configuration Bits Window21EE 200 Spring 2014Lab 18.Set PWMLOCK Off22EE 200 Spring 2014Lab 18.Source Code for Setting PWMLOCK23EE 200 Spring 2014Lab 18.Clearing PWMLOCK in C24• Add the following code before the main functionEE 200 Spring 2014Lab 18.Exercise 1: Overview• Write and implement C-code for controlling LED intensity using PWM1H at pin 252512111019876543217181920212223242526272813141615dsPIC33EP64MC502SSVPWM1H330LEDto myDAQ AI 1+to myDAQ AGND and AI 1-EE 200 Spring 2014Lab 18.Exercise 1 Supporting Circuit26EE 200 Spring 2014Lab 18.Exercise 1: PWM Signal Specs• PWM period of 1 ms• Alternately dim and brighten the LED with a period of 500 ms• Drive the LED with a 25% duty cycle for 250 ms, then set the duty cycle to 75% for 250 ms; repeat27EE 200 Spring 2014Lab 18.Exercise 1: Register Values• Clock Period = 1/ 7.37 MHz• Set PTPER = 7370 so that PWM period is 1 ms• SET PDC1 = 0.75*PTPER = 5527 for 75% Duty Cycle• SET PDC1 = 0.25*PTPER = 1843 for 25% Duty Cycle28EE 200 Spring 2014Lab 18.Exercise 1: C Code29EE 200 Spring 2014Lab 18.Exercise 1• Open project PWM_Exercise_1.X and complete the code• Verify code by observing– LED connected to PWM1H – myDAQ Oscilloscope (Channel AI 1)30EE 200 Spring 2014Lab 18.PWM using the myDAQ• Consult Table 3, page 17, in the myDAQ User Guide and Specifications document31EE 200 Spring 2014Lab 18.DAQmx Data Acquisition Palette• Data Acquisition VIs for developing instrumentation, acquisition, and control applications32EE 200 Spring 2014Lab 18.Required DAQmx VIs• DAQmx Create Virtual Channel• DAQmx Timing• DAQmx Start Task• DAQmx Write• DAQmx Stop• DAQmx Clear Task33EE 200 Spring 2014Lab 18.DAQmx Create Virtual Channel34EE 200 Spring 2014Lab 18.DAQmx Timing (Implicit)35EE 200 Spring 2014Lab 18.DAQmx Start Task36EE 200 Spring 2014Lab 18.DAQmx Write37EE 200 Spring 2014Lab 18.DAQmx Stop Task38EE 200 Spring 2014Lab 18.DAQmx Clear Task39EE 200
View Full Document