用STC 的 12LE5404AD做脉宽采集1致2mS的脉宽.只能采集第一路(PCA0)第二路(PCA1)不会采集,没有产生CCF1的中断信号?是不是我的程序有什么问题? 麻烦帮忙看看,谢谢. //////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// #include<STC12C5410AD.H> #include<INTRINS.H>
typedef unsigned int uint; typedef unsigned char uchar;
sbit led2 = P2^6; sbit led1 = P2^7; sbit Key1 = P2^2; sbit Key2 = P2^3; sbit PWM1 = P1^7; sbit PWM2 = P1^5;
uint temp,Count1,PWM_compare1,PWM_compare2; uchar PWM_out1,PWM_out2,CCAP2_time; uchar bdata SB; sbit tp1 = SB^0; sbit tp2 = SB^1; sbit tp3 = SB^2; sbit tp4 = SB^3;
void Delay(uchar i); void Initiate(); void Key_scan();
// IAP read and write// void IAP_enable(); void IAP_disable(); void Sector_erase(uint); uchar Byte_read(uint adr); void Byte_write(uint adr,uchar or_data);
main() { Count1 = 0; tp1 = 1; tp3 = 1; Initiate(); Delay(20); while(1) { Key_scan(); } }
void Key_scan() { if(Key2 == 0) { EA = 0; Delay(10); //防抖动. if(Key2 == 0) { Sector_erase(0x2800); Byte_write(0x2801,PWM_compare1); led2 = !led2; while(Key2 == 0); } EA = 1; } if(tp2 == 1) { PWM_compare1 = Byte_read(0x2801); led2 = !led2;Count1 = 0;EA = 1;} //如果1.3秒内收不到信号,输出保存的占空比.
if(Count1 > 65500) //1.3秒. tp2 = 1; else tp2 = 0;
if(PWM_compare1 > 50) //防止读EEPROM错误. { if(PWM_compare1 < 130) PWM_out1 = PWM_compare1; }
}
void Delay(uchar i) { uchar k1; for(k1 = 0; k1 < 9; k1++) for(; i > 0; i--); }
void Initiate() { CMOD = 0x00; CCON = 0x00; CL = 0x00; CH = 0x00; CCAPM0 = 0x21;//上升沿触发.允许中断. CCAPM1 = 0x31; //上降上伸沿触发. EPCA_LVD = 1; EA = 1; CR = 0;
ET0 = 1; TMOD = 0x02; TL0 = 0xF6; //20uS 定时器. CRYSTAL:6Mhz. TH0 = 0xF6; TR0 = 1; P1M0 = 0x00; //强上拉. P1M1 = 0x80; PWM_out1 = 0; PWM_out2 = 0;
} // CPA 中断////////////////////// void CUT() interrupt 6 { if(CCF0 == 1) //CAP0模块. { if(tp1 == 1) { CL = 0; CH = 0; CCF0 = 0; CR = 1; //开始计数. tp1 = 0; Count1 = 0; CCAPM0 = 0x11;//下降沿触发.允许中断. led1 = !led1; } if(CCF0 == 1 & tp1 == 0) { temp = CH; temp = (temp << 8) | CL; PWM_compare1 = temp /10; CR = 0; CL = 0; CH = 0; CCF0 = 0; tp1 = 1; CCAPM0 = 0x21;//上升沿触发.允许中断.
} if(CCF1 == 1) //CAP1模块.不会运行到这里,没有CCF1 = 1的中断信号,.不能采集这里的信号?????????? { led1 = !led1; if(tp3 == 1) { CCF1 = 0; CCAP2_time = 0; tp3 = 0; CCAPM1 = 0x11;//下降沿触发.允许中断. led1 = !led1; } if(CCF1 == 1 & tp3 == 0) { CCF1 = 0; PWM_compare2 = CCAP2_time; tp3 = 1; CCAPM1 = 0x21; //上升沿触发.允许中断. } }
} } //////定时器O,用于输出PWM脉冲.////////// void Time0() interrupt 1 { static uint tt; tt++; if(tt == 920) // 频率控制 920 * 20uS = 18.4mS. tt = 0; if(PWM_out1 > 50) //防止干扰. { if(PWM_out1 < 130) if(tt < PWM_out1) //占空比控制. PWM1 = 1; else PWM1 = 0; } else PWM1 = 0;
if(tt < PWM_compare2) PWM2 = 1; else PWM2 = 0; Count1++; CCAP2_time++;
}
|