本帖最后由 hpfily 于 2013-3-8 15:34 编辑
这是个计算脉冲高电平宽度的程序
#include "pic.h"
int time1,time2;
int i;
void ccp(void)
{
CCP1IF=0;
CCP1IE=1;
T1CKPS1=0;
T1CKPS0=0;
T1OSCEN=0;
//上升沿
CCP1M3=0;
CCP1M2=1;
CCP1M1=0;
CCP1M0=1;
TMR1CS=0;
TMR1ON=1;
PEIE=1;
GIE=1;
}
void interrupt CCP1INT(void)
{
if(CCP1IF)
{
CCP1IF=0;
if(CCP1M0==1) //如果是上升沿
{
unsigned int temp=0;
temp=CCPR1H;
time1=(temp<<8)+CCPR1L;
CCP1M0=0; //改成下降沿
return;
}
else //下降沿
{
unsigned int temp=0;
temp=CCPR1H;
time2=(temp<<8)+CCPR1L;
CCP1M0=1; //继续改成上升沿
i=time2-time1;
CCP1IE=0;
CCPR1H=CCPR1L=0;
}
}
else
{
TMR1IF=0;
}
}
void main()
{
TRISC=0xff;
time1=0;
time2=0;
TRISD=0x00;
PORTD=0x00;
ccp();
while(1)
{CCP1IE=1;}
} |