三个通道测低电平时间,方法是开PCA边沿中断,上下降沿时间互减,但测试中一个方波进来,上升沿中断22次,下降沿中断居然有600多次
到底什么问题,搞了几天都不行!!!!
还有设置PCA中断优先级会导致中断时死机,真是无语了!
只开了PCA中断,其他中断全关
PCA初始化程序如下:
//PCA中断 相关功能中文手册316页
CCON = 0; //Initial PCA control register
CR = 0;
CF = 0; //PCA timer stop running
CCF0 = 0; //Clear CF flag
CCF1 = 0; //Clear all module interrupt flag
CCF2 = 0;
CL = 0; //Reset PCA base timer
CH = 0;
CMOD = 0x01; //Set PCA timer clock source as Fosc/12
// CMOD = 0x00; //Enable PCA timer overflow interrupt
CCAPM0 = 0x31; //PCA module-0 capture by a negative tigger on CEX0(P3.7) and enable PCA interrupt
CCAPM1 = 0x31; //PCA1
CCAPM2 = 0x31; //PCA2 跳变边沿中断
EPCAI = 1; //PCA MODEL INT EN
// IP |= 0x00; //中断优先级设置会引起死机!!!!!
// IPH |= 0x40;
// IP = 0x40;
// IPH = 0x40; //PCA中断最高优先级
// Delay( 10 );
CR = 1; //PCA timer start run
EA = 1; //总中断
PCA中断程序:
void PCA_isr() interrupt 6 using 1
{
unsigned int buf;
if( CF )
{
CF = 0;
// CL = 0;
// CH = 0;
if( Timing[0] )
PCACount[0]++;
if( Timing[1] )
PCACount[1]++;
if( Timing[2] )
PCACount[2]++;
}
//测量方式,下降沿记录一次定时器值,溢出中断检查计数中的的Count加1
if( CCF0 ) //PCA0中断
{
CCF0 = 0;
buf = CCAP0H;
buf <<= 8;
buf += CCAP0L;
// CCAPM0 = 0x31;
if( PCA0 ) //上升沿
{
// TimeCount[0] = (65536*PCACount[0] + buf-BufTimer[0])/100;
TimeCount[0]++;
Timing[0] = 0;
PCACount[0] = 0;
CCAPM0 = 0x11;
}
else if( !PCA0 ) //下降沿
{
TimeCount[1]++; Timing[0] = 1;
BufTimer[0] = buf;
CCAPM0 = 0x21; //开启上升沿中断
}
}
if( CCF1 )
{
CCF1 = 0;
buf = CCAP1H;
buf <<= 8;
buf += CCAP1L;
// CCAPM1 = 0x31;
if( PCA1 ) //上升沿
{
// TimeCount[1] = (65536*PCACount[1] + buf-BufTimer[1])/100;
Timing[1] = 0;
PCACount[1] = 0;
CCAPM1 = 0x11;
}
else if( !PCA1 ) //下降沿
{
TimeCount[1]++;
Timing[1] = 1;
BufTimer[1] = buf;
CCAPM1 = 0x21;
}
}
if( CCF2 )
{
CCF2 = 0; //Clear interrupt flag
buf = CCAP2H;
buf <<= 8;
buf += CCAP2L;
// CCAPM2 = 0x31;
if( PCA2 ) //上升沿
{
TimeCount[2] = (65536*PCACount[2] + buf-BufTimer[2])/100;
Timing[2] = 0;
PCACount[2] = 0;
CCAPM2 = 0x11;
}
else if( !PCA2 ) //下降沿
{
Timing[2] = 1;
BufTimer[2] = buf;
CCAPM2 = 0x21;
}
}
}
测试中TimeCount[1]次数是TimeCount[0]的30多倍,按照正常方波进来不可能的。10Hz方波进来TimeCount[0]的值是对的!
|