pic ccp捕捉

2019-04-15 11:45发布

void init_timer1(void)//定时器1作为ccp1捕捉的计数基本                                       timer1
{

    //分频 1:8; T1OSCEN disabled; nT1SYNC synchronize; 时钟 FOSC/4; TMR1ON off; 
T1CON = 0x30;  TMR1H = 0x00; TMR1L = 0x00;
        PIR1bits.TMR1IF = 0;
        T1CONbits.TMR1ON = 1;   // Start TMR1
}
void init_ccp1(void)//                                                                         ccp需要timer1  {

CCP1CON = 0x04;    
CCPR1L = 0x00;    
CCPR1H = 0x00;    
PIR1bits.CCP1IF = 0;
PIE1bits.CCP1IE = 1;
} if(CCP1IF==1)  //中断处理函数
{

unsigned int temp;

CCP1IF=0;

temp = CCPR1H;
temp = (temp << 8) + CCPR1L;
if(temp > RPM_bef)
RPM_data = temp - RPM_bef; //T1没有溢出
else
RPM_data = 0xffff - RPM_bef + temp; //T1溢出
RPM_No2++;
RPM_data = (RPM_data >> 1)+(time_bef >> 1);
RPM_bef  = temp;
time_bef = RPM_data;
RPM_m=  time_bef*60/DJJS;