第10章S12脉宽调制模块PULSEWIDTHMODULATION脉冲宽度调制(PWM)是工业控制和机电产品中最常用的技术。例如控制电动机的速度和伺服电机的旋转角度等。什么是PWM?periodonTTRatioWidthPulseTonToffTperiodAverage0%10%50%90%99%应用:D/A转换马达控制音调生成正弦波生成占空比=高电平时间/周期=Ton/Tperiod10.1PWM概述•S12可提供优异的频率高、分辨率高、占空比可调、范围宽的PWM信号。主要特点:8个带周期占空比可程控的PWM独立通道4个可程控选择的时钟源每个PWM通道有专用的计数器PWM每个通道脉冲极性可以选择每个PWM通道可使能/禁止周期和占空比双缓冲每个通道有中心对齐和边缘对齐方式分辨率:8位(8通道),16位(4通道)带中断功能的紧急切断占空比可调范围0~100%10.2PWM结构和原理通道7周期和占空比计数器通道6周期和占空比计数器...通道0周期和占空比计数器管脚使能选择极性选择对齐方式选择管脚管脚10.2.1PWM工作原理开始周期TPWMTPWM占空比TDutyPWMDTYxPWMPERxPWMPERxfClock,TClock8-bitCounterPWMCNTx时钟8-bitCompare=PWMDTYx8-bitCompare=PWMPERx0x000x00ResetPin当8位计数器的值等于PWMDTY(dutycycle寄存器)时,电平从有效跳到无效;当8位计数器的值等于PWMPER(周期寄存器)时,电平从无效跳到有效,然后计数器清0,重新开始计数PWMPERxPWMDTYxPWM计时器通道8-BitCounterPWMCNTx8-BitCompare=PWMDTYx8-BitCompare=PWMPERxPWMCNT0@$_0CPWMCNT1@$_0DPWMCNT2@$_0EPWMCNT3@$_0FPWMCNT4@$_10PWMCNT5@$_11PWMCNT6@$_12PWMCNT7@$_13PWMDTY0@$_1CPWMDTY1@$_1DPWMDTY2@$_1EPWMDTY3@$_1FPWMDTY4@$_20PWMDTY5@$_21PWMDTY6@$_22PWMDTY7@$_23PWMPER0@$_14PWMPER1@$_15PWMPER2@$_16PWMPER3@$_17PWMPER4@$_18PWMPER5@$_19PWMPER6@$_1APWMPER7@$_1B极性位PolarityBit=1Duty=HighTimeChannelxx=0...7DoubleBufferedDoubleBufferedPWMDTYxPWMPERx占空比寄存器周期寄存器对齐方式选择-左对齐ClockSourceE=100nsDutyCycle=75%Period=400ns%100_:1%100_:0),,,(_PWMPERxPWMDTYxCycleDutyPolarityPWMPERxPWMDTYxPWMPERxCycleDutyPolarityPWMPERxSBSABAClockFrequencyPWMx时钟源=E=10MHz(100nsperiod)PPOLx=0PWMPERx=4PWMDTYx=1PWMx频率=10MHz/4=2.5MHzPWMx周期=400nsPWMx占空比=¾*100%=75%Start对齐方式选择-中央对齐PWMDTYxE=100nsPeriod=PWMPERx*2=800ns%100_:1%100_:02),,,(_PWMPERxPWMDTYxCycleDutyPolarityPWMPERxPWMDTYxPWMPERxCycleDutyPolarityPWMPERxSBSABAClockFrequencyPWMx时钟源=E=10MHz(100nsperiod)PPOLx=0PWMPERx=4PWMDTYx=1PWMx频率=10MHz/8=1.25MHzPWMx周期=800nsPWMx占空比=¾*100%=75%StartPWMDTYxE=100nsPWMPERxPWMPERxDutyCycle=75%16位分辨率两个8位PWM通道可以组合成一个16位PWM通道Period/DutyComparePWMCNT6PWMCNT7PWM7ClockSource7CONxxPWMExPPOLxPCLKxCAExPWMxOUTPUTCON67PWME7PPOL7PCLK7CAE7PWM7CON45PWME5PPOL5PCLK5CAE5PWM5CON23PWME3PPOL3PCLK3CAE3PWM3CON01PWME1PPOL1PCLK1CAE1PWM116位PWM组合•软件可选择通道组合.•在组合模式下,可以左对齐或中心对齐输出•增加了分辨率,周期可以更长CLOCKSOURCE5PWCNT4PWCNT5PERIOD/DUTYCOMPARECLOCKSOURCE7PWCNT6PWCNT7PERIOD/DUTYCOMPARECLOCKSOURCE3PWCNT2PWCNT3PERIOD/DUTYCOMPARECLOCKSOURCE1PWCNT0PWCNT1PERIOD/DUTYCOMPAREPWM7PWM5PWM3PWM1PWM时钟选择1.ClockA(Ch0,Ch1,Ch4,Ch5)2.ClockSA(scaledA;Ch0,Ch1,Ch4,Ch5)3.ClockB(Ch2,Ch3,Ch6,Ch7)4.ClockSB(scaledB;Ch2,Ch3,Ch6,Ch7)4个时钟源:BusClockDividebyPrescalerTaps:248163264128预分频PWMPRCLK@$_03ClockSAClockSBClockAFurtherDivisionoftheclock:2468...512ClockBFurtherDivisionoftheclock:2468...512PWMSCLA@$_08PWMSCLB@$_09分频PWM时钟选择–预分频寄存器0PCKB2PCKB1PCKB00PCKA2PCKA1PCKA0RWReset:00000000=UnimplementedorReservedPWMPRCLK@$_03PCKx2PCKx1PCKx0ValueofClockx000BusClock001BusClock/2010BusClock/4011BusClock/8100BusClock/16101BusClock/32110BusClock/64111BusClock/128可在任意时刻读写!x=AorB设置PRESCALE寄存器:PWMPRCLK=0x22;//B=Bus/4,A=Bus/4PWMPRCLK=0x07;//B=Bus,A=Bus/128SoftwareExamplesBit0Bit1Bit2Bit3Bit4Bit5Bit6Bit0PWM时钟选择–分频寄存器总线时钟fBusDividebyPrescalerTaps:2481632641288-BitDownCounterBit7Bit6Bit5Bit4Bit3Bit2Bit1Bit0PWMSCLx分频值SxClockxClockPWMSCLxPWMSCLxxClockSxClock_2_2__PWMSCLx=$00PWMSCLxvalueis256可在任意时刻读写!x=AorB/2ClockSxClockxClockx=fBus/PWMPRCLKCount=1PWMPRCLK@$_03PWMSCLB@$_09PWMSCLA@$_08例如:要求:ClockSx=1kHz总线时钟=16MHzPrescaler=128Prescaler=64Clockx=125kHzClockx=250kHzPWMSCLx=63PWMSCLx=125ClockSx=992HzClockSx=1000HzPCLK6PCLK5PCLK4PCLK3PCLK2PCLK1RWReset:00000000PWMCLK@$_02PCLK7Bit0Bit1Bit2Bit3Bit4Bit5Bit6Bit0PCLK5—通道5时钟选择1=时钟源为SA0=时钟源为APCLK4—通道4时钟选择1=时钟源为SA0=时钟源为APCLK3—通道3时钟选择1=时钟源为SB.0=时钟源为BPCLK2—通道2时钟选择1=时钟源为SB0=时钟源为BPCLK1—通道1时钟选择1=时钟源为SA0=时钟源为APCLK0—通道0时钟选择1=时钟源为SA0=时钟源为APCLK7—通道7时钟选择1=通道7的时钟源为SB0=通道7的时钟源为B.PCLK6—通道6时钟选择1=时钟源为SB.0=时钟源为B使能/禁止PWMPWME6PWME5PWME4PWME3PWME2PWME1PWME0RWReset:00000000PWME@$_00PWME71=通道x使能.下一个时钟开始输出PWM0=通道x禁止通道7通道6通道0...使能/禁止PWM通道:PWME5=1;//EnablePWMchannel5PWME3=0;//DisablePWMchannel3PWME=0xFF//Enableall8PWMchannelsPWME=0;//Disableall8PWMchannels软件示例Bit0Bit1Bit2Bit3Bit4Bit5Bit6Bit0PWM中央对齐使能寄存器CAE6CAE5CAE4CAE3CAE2CAE1CAE0RWReset:00000000PWMCAE@$_04CAE7CAEx–中心对齐模式选择,通道x1=通道x中心对齐输出0=通道x左边对齐输出Bit0Bit1Bit2Bit3Bit4Bit5Bit6Bit0通道7通道6通道0...紧急关闭紧急关闭通道7Channel6PeriodandDutyCounterChannel5PeriodandDutyCounter...Channel0PeriodandDutyCounter故障输入信号PWMLVL=1PWMLVL=0PWM7INL=1PWM7INL=0InterruptPWMIEPWMIFPWM关闭寄存器PWMIEPWMRSTRTPWMLVL0PWM7INLPWM7ENRWReset:00000000PWMSDN@$_24PWMIF0PWM7IN=UnimplementedorReserved设置关闭有效电平PWM7INL设置关闭时的输出电平PWMLVL使能紧急关闭PWM7ENAInitialisation中断使能PWMIEBit0Bit1Bit2Bit3Bit4Bit5Bit6Bit0工作模式等待模式在WAIT模式下,允许输入时钟到预分频器PSWAI=10在WAIT模式下,停止输入时钟到预分频器冻结模式在FREEZE模式下,PWM计数器继续PFRZ=10在FREEZE模式下,PWM计数器停止CON45CON010PSWAIRWPWMCTL@$_05CON67CON23PFRZ0Bit0Bit1Bit2Bit3Bit4Bit5Bit6Bit0使能/禁止时钟在WAIT模式时PFRZ—PWM计数器停止在Freeze模式时16位工作模式PWM通道周期寄存器-周期计算Bit6Bit5Bit4Bit3Bit2Bit1Bit0RWReset:11111111PWMPERx@$_14-_1BBit7PeriodClockChannelPeriodPWMxPWMPERxPWMPERxPeriodClockChannelPeriodPWMx__2_2___0)(CAExOutputAlignedLeftOutputAlignedCenterPeriodPWMxPWMPERxOutputAlignedCenterPWMPERxPeriodClockChannelPeriodPWMx__2______1)(CAExOutputAl