Lecture21 - 0306-250 Assembly Language Programming Lecture...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0306-250 Assembly Language Programming Lecture Twenty-One: Pulse-Width Modulation (PWM) (Text Section 14.10) •HSC12 Pulse-Width Modulation Module (PWM) •Registers •Algorithm HCS12 PWM Module Pulse-width modulation module (PWM) • Generates up to six independent PWM waveforms • Useful for controlling certain devices – Stepper motors – Servo motors • Data sheet link on course resources web page 2 HCS12 Block Diagram PWM MC9S12C Family Reference Manual, p. 20 3 PWM Block Diagram PWM_8B6C Block User Guide V01.14 (S12PWM8B6CV1), p. 14. 4 PWM Registers PWM_8B6C Block User Guide V01.14 (S12PWM8B6CV1), pp. 17-18. 5 Pulse-Width Modulated Waveform tDuty tPeriod DutyCycle = 100 t Duty t Period % 6 PWM Prescale Clock Select Register 7 6 5 4 3 2 1 0 0 PCKB2 PCKB1 PCKB0 0 PCKA2 PCKA1 PCKA0 ClockScaleExponent = PCK2 : PCK1: PCK0 BusClockRate 1 f ExternalOscillator ClockRate = ClockScaleExponent = 2 ClockScaleExponent 2 2 Set clocks A and B — Want 125 kHz for Clock A? PCKB2:PCKB1:PCKB0: ClockBScaleExponent: 4 = %100 — Want 125 kHz for Clock B? PCKA2:PCKA1:PCKA0: ClockBScaleExponent: 4 = %100 PWM_CLKS_DIV16: EQU $44 movb #PWM_CLKS_DIV16,PWMPRCLK 7 PWM Scale A Register 7 6 5 4 3 2 1 0 SCLA7 SCLA6 SCLA5 SCLA4 SCLA3 SCLA2 SCLA1 SCLA0 ì 256 : SCLA7 : 0 = 0 ARateDivisor = í îSCLA7 : 0 : otherwise ClockARate ScaledARate = 2 ´ ARateDivisor Want 12.5 kHz from 125-kHz clock A? SCLA7:0: ClockADivisor: 5 PWM_SCLK_DIV10: EQU 5 movb #PWM_SCLK_DIV10,PWMSCLA 8 PWM Clock Select Register 7 6 5 4 3 2 1 0 0 0 PCLK5 PCLK4 PCLK3 PCLK2 PCLK1 PCLK0 Select Clock A or Clock SA: channels 0-1,4-5 Select Clock B or Clock SB: channels 2-3 — Want to select Clock A for channel 0? PCLK0 : ClockA¢/ClockSA: 1 — Want to let other channels (not used) default? Other bits: 0 movb #mPWMCLK_PCLK0,PWMCLK 9 PWM Polarity Select Register 7 6 5 4 3 2 1 0 0 0 PPOL5 PPOL4 PPOL3 PPOL2 PPOL1 PPOL0 Select active-low or active-high duty period — Want channel 0 high during duty period? PPOL0 : Value during duty period: 1 — Want default for other channels (not used)? Other bits: 0 movb #mPWMPOL_PPOL0,PWMPOL 10 PWM Channel Period Registers 7 6 5 4 3 2 1 0 PWM PERn7 PWM PERn6 PWM PERn5 PWM PERn4 PWM PERn3 PWM PERn2 PWM PERn1 PWM PERn0 Select channel n period — Want 20-ms period for channel 0? PWMPER0 : Clock ticks per period: 250 PWM_20ms: EQU 250 movb #PWM_20ms,PWMPER0 11 PWM Channel Duty Period Registers 7 6 5 4 3 2 1 0 PWM DTYn7 PWM DTYn6 PWM DTYn5 PWM DTYn4 PWM DTYn3 PWM DTYn2 PWM DTYn1 PWM DTYn0 Select channel n duty period — Want 2 ms for channels 0? PWMDTY0 : Clock ticks per duty period: 25 PWM_2ms: EQU 25 movb #PWM_2ms,PWMDTY0 12 PWM Center Align Enable Register 7 6 5 4 3 2 1 0 0 0 CAE5 CAE4 CAE3 CAE2 CAE1 CAE0 Control PWM waveform alignment — Want left alignment for all channels (default)? CAE0 : CAE1 : CAE2 : CAE3 : CAE4 : CAE5 : Left¢/Center alignment for Channel 0: Left¢/Center alignment for Channel 1: Left¢/Center alignment for Channel 2: Left¢/Center alignment for Channel 3: Left¢/Center alignment for Channel 4: Left¢/Center alignment for Channel 5: 0 0 0 0 0 0 PWM_ALIGN_L: EQU %00000000 movb #PWM_ALIGN_L,PWMCAE 13 PWM Control Registers 7 6 5 4 3 2 1 0 0 CON45 CON23 CON01 PSWAI PFRZ 0 0 Control PWM — Want six separate 8-bit channels? CON45: Separate¢/Concatenate channels 4 and 5: 0 CON23: Separate¢/Concatenate channels 2 and 3: 0 CON01: Separate¢/Concatenate channels 0 and 1: 0 — Want defaults for other signals? PSWAI: Continue¢/Stop PWM in wait mode: 0 PFRZ: Continue¢/Stop PWM in freeze mode: 0 PWM_8b: EQU %00000000 movb #0,PWMCTL 14 PWM Enable Register 7 6 5 4 3 2 1 0 0 0 PWME5 PWME4 PWME3 PWME2 PWME1 PWME0 Enable PWM — Want channel 0 enabled? PWME0 : Enable Channel 0: 1 movb #mPWME_PWME0,PWME 15 Servo Positions • Servo position determined by duty cycle • Create constant table for desired positions Duty periods in terms of clock cycles • Need to calibrate values to actual servo PWM_2ms: EQU 25 PWM_1ms: EQU 12 ;Table of duty clocks ;for desired servo positions PWMDutyTable: DC.B PWM_2ms DC.B ((3*(PWM_2ms-PWM_1ms)/4)+PWM_1ms) DC.B (((PWM_2ms-PWM_1ms)/2)+PWM_1ms) DC.B (((PWM_2ms-PWM_1ms)/4)+PWM_1ms) DC.B PWM_1ms 16 PWM Algorithm • Initialization (once in program) – – – – – – – – Write PWMPRCLK Write PWMSCLA Write PWMCLK Write PWMPOL Write PWMPER0 Write PWMDTY0 Write PWMCAE Write PWMCTL • Enable PWM – Write PWME • Change position of Channel 0 servo – Write new duty cycle to PWMDTY0 17 ...
View Full Document

This note was uploaded on 05/06/2010 for the course EECC 0306-250 taught by Professor Roymelton during the Fall '10 term at RIT.

Ask a homework question - tutors are online