以下程序供参考:
- void PWM_Out(unsigned int pwm_0,pwm_1,pwm_2)
- {
- unsigned char temp_l,temp_h,temp;
-
- temp_l=pwm_0&0x00ff; //读pwm_0低8位赋值给temp_l
- temp_h=(pwm_0&0x0300)>>4; //读pwm_0高2位,移动到XCCAP0H(PCA_PWM0[5:4])位置,赋值给temp_h
- temp=PCA_PWM0&0xcd; //读PCA_PWM0,XCCAP2H(PCA_PWM0[5:4])、EPC0H(PCA_PWM0[1])清零后赋值给temp
- temp_h|=temp; //写其他位到temph
- if(pwm_0==0x03ff) //如果pwm_0最大
- temp_h|=0x02; //设置EPC0H(PCA_PWM0[1])为1
- PCA_PWM0=temp_h; //写PCA_PWM0(先写高2位)
- CCAP0H=temp_l; //写CCAP0H(再写低8位)
-
- temp_l=pwm_1&0x00ff; //读pwm_1低8位赋值给temp_l
- temp_h=(pwm_1&0x0300)>>4; //读pwm_1高2位,移动到XCCAP1H(PCA_PWM1[5:4])位置,赋值给temp_h
- temp=PCA_PWM1&0xcd; //读PCA_PWM1,XCCAP1H(PCA_PWM2[5:4])、EPC1H(PCA_PWM1[1])清零后赋值给temp
- temp_h|=temp; //写其他位到temph
- if(pwm_1==0x03ff) //如果pwm_1最大
- temp_h|=0x02; //设置EPC1H(PCA_PWM1[1])为1
- PCA_PWM1=temp_h; //写PCA_PWM1(先写高2位)
- CCAP1H=temp_l; //写CCAP1H(再写低8位)
-
- temp_l=pwm_2&0x00ff; //读pwm_2低8位赋值给temp_l
- temp_h=(pwm_2&0x0300)>>4; //读pwm_2高2位,移动到XCCAP2H(PCA_PWM2[5:4])位置,赋值给temp_h
- temp=PCA_PWM2&0xcd; //读PCA_PWM2,XCCAP2H(PCA_PWM2[5:4])、EPC2H(PCA_PWM2[1])清零后赋值给temp
- temp_h|=temp; //写其他位到temph
- if(pwm_2==0x03ff) //如果pwm_2最大
- temp_h|=0x02; //设置EPC2H(PCA_PWM2[1])为1
- PCA_PWM2=temp_h; //写PCA_PWM2(先写高2位)
- CCAP2H=temp_l; //写CCAP2H(再写低8位)
- }
复制代码
|