|                                          
									    #include <stc12c5a60s2.h>#define FOSC 12000000L
 sbit led0=P0^6;sbit led1=P0^7;
 sbit PWM0=P1^3;
 sbit PWM1=P1^4;
 sbit int0=P3^2;
 unsigned char HZ;unsigned long PCHZ;
 unsigned char xdata rsbuf[64],ssbuf[64];unsigned char yb,syb;
 unsigned int m;
 void INT0_Init(){
 IT0=1;EX0=1;
 }
 void ENPCA_Init(unsigned char tt)
 {
 CMOD |=0x04;
 CL=0;CH=0;
 CR=1;
 AUXR |=0x80;
 TMOD |=0x02;
 TH0= tt;
 TL0= tt;
 TR0= 1;
 ET0= 1;
 }
 void PWM_OUT(unsigned char channel,unsigned char start_value){
 if(channel == 0)
 {
 CCAP0L = start_value; //Set the initial value same as CCAP0H
 CCAP0H = start_value; //25% Duty Cycle
 CCAPM0 = 0x42;   //0x42 Setup PCA module 0 in PWM mode
 }
      if(channel == 1){
 CCAP1L = start_value; //Set the initial value same as CCAP1H
 CCAP1H = start_value; //25% Duty Cycle
 CCAPM1 = 0x42;   //0x42 Setup PCA module 1 in PWM mode
 }
 }
 void Serical_Init()
 {
 PCON |= 0x80;  //使能波特率倍速位SMOD
 SCON = 0x50;  //8位数据,可变波特率
 BRT = 0xC4;  //设定独立波特率发生器重装值
 AUXR |= 0x04;  //独立波特率发生器时钟为Fosc,即1T
 AUXR |= 0x01;  //串口1选择独立波特率发生器为波特率发生器
 AUXR |= 0x10;  //启动独立波特率发生器
 ES=1;
 }
 void main(){
 Serical_Init();
 INT0_Init();
 EA=1;
 while(1)
 {
 led0=PWM0;
 if(m>10000)
 {
 TR0=0;ET0=0;CR=0;EX0=0;
 m=0;
 }
 }
 }
 void Serical_Interrupt() interrupt 4
 {
 unsigned char temp,temp_next;
 if(RI)
 {
 RI=0;
 temp=SBUF;
 rsbuf[yb]=temp;
 yb++;
 if((rsbuf[0]==':')&&(temp_next=0x0D)&&(temp==0x0A))
 {
 yb=0;
  
  CHZ=(rsbuf[1]-0x30)*10000+(rsbuf[2]-0x30)*1000+(rsbuf[3]-0x30)*100+(rsbuf[4]-0x30)*10+(rsbuf[5]-0x30)*1; HZ=256-FOSC/256/PCHZ;
  
  WM_OUT(0,0X80); ENPCA_Init(HZ);
 }
 }
 if(TI)
 {TI=0;}
 }
 void INT0_Interrupt() interrupt 0
 {
 m++;
 }
 通过外部中断来计算PWM个数,PWM最大频率为10K(个人自定义的),这样的话外部中断完全可以采集到的,但是呢在运行的过程中PWM脉冲个数总会比预想的要多这是个什么情况??
 |