下面这个程序是应用在stc12c2052ad上的pwm程序,测验成功
#includesfr CCON =0xD8; //PCA control registersfr CMOD =0xD9;sfr CL =0xe9;sfr CH =0xF9;sfr CCAP0L=0xea;sfr CCAP1L=0xeb;sfr CCAP0H=0xfa;sfr CCAP1H=0xfb;sfr CCAPM0=0xda;sfr CCAPM1=0xdb;sbit CR=0xde; //位寻址void main(void){CMOD=0x02; //运用体系时钟,时钟输入源频率fosc/2。PWM频率=fosc/2/256/************************************7 6 5 4 3 2 1 0 CIDL - - - cps2 CPS1 CPS0 ECFCIDL: =0时,闲暇形式下PCA计数器持续作业;=1时,闲暇形式下PCA计数器停止作业。CPS2,CPS1,CPS0:PCA计数脉冲挑选。010,定时器0的溢出,能够完成可调频率的PWM输出。ECF: =1时,使能寄存器CCON CF位的中止;=0时,制止该功用。***************************************/CL=0x00;CH=0x00;CCAP0L=0xc0; //当CL小于CCAP0L时,输出为低;大于时,输出为高。CCAP0H=0x7f; //当CL由ff变为00溢出时,CCAP0H装载到CCAP0L中,完成无搅扰的更新PWM。CCAP1L=0xc0; //当CL小于CCAP1L时,输出为低;大于时,输出为高。CCAP1H=0xc0; //当CL由ff变为00溢出时,CCAP1H装载到CCAP1L中,完成无搅扰的更新PWM。CCAPM0=0X42; //使能PWM形式,PWM0=1,ECOM0=1CCAPM1=0X42; //使能PWM形式,PWM1=1,ECOM1=1CR=1; //发动PCA计数器while(1) {CCAP0H=RF4432_TxRxBuf[4]; //此处改动占空比CCAP1H=RF4432_TxRxBuf[5];CCAP2H=RF4432_TxRxBuf[6]/2.5;};}