stc12 15单片机的pca模块产生pwm的完整例程,测试成功。完整代码下载:http://www.51hei.com/f/stcpcaPWM.zip
#include <reg51.h>
#include "PCA.h"
void PWM(unsigned int num,int cex);
void main()
{
CMOD=0x02;
CL=0x00;
CH=0x00;
PWM(10,0);
PWM(20,1);
PWM(50,2);
PWM(70,3);
PWM(90,4);
CR=1;
while(1);
}
void PWM(unsigned int num,int cex)
{
double i=256*(100-num)/100;
switch(cex)
{
case 0: CCAP0L=i; CCAP0H=i; CCAPM0=0x42; break;
case 1: CCAP1L=i; CCAP1H=i; CCAPM1=0x42; break;
case 2: CCAP2L=i; CCAP2H=i; CCAPM2=0x42; break;
case 3: CCAP3L=i; CCAP3H=i; CCAPM3=0x42; break;
case 4: CCAP4L=i; CCAP4H=i; CCAPM4=0x42; break;
}
}
头文件:
#ifndef _SST_PCA_H_
#define _SST_PCA_H_
sfr CH =0xF9;
sfr CL =0xE9;
sfr CMOD =0xD9;
sfr CCON =0xD8;
sfr CCAPM0 =0xDA;
sfr CCAPM1 =0xDB;
sfr CCAPM2 =0xDC;
sfr CCAPM3 =0xDD;
sfr CCAPM4 =0xDE;
sfr CCAP0H =0xFA;
sfr CCAP1H =0xFB;
sfr CCAP2H =0xFC;
sfr CCAP3H =0xFD;
sfr CCAP4H =0xFE;
sfr CCAP0L =0xEA;
sfr CCAP1L =0xEB;
sfr CCAP2L =0xEC;
sfr CCAP3L =0xED;
sfr CCAP4L =0xEE;
/*CCON*/
sbit CF =CCON^7;
sbit CR =CCON^6;
sbit CCF4 =CCON^4;
sbit CCF3 =CCON^3;
sbit CCF2 =CCON^2;
sbit CCF1 =CCON^1;
sbit CCF0 =CCON^0;
#endif