#include <p18f2480.h>
unsigned char cap_high; unsigned char cap_low;
void VectorHigh (void); void HandlerHigh (void);
#pragma code VectorHigh = 0x08 void VectorHigh (void) { _asm goto HandlerHigh //jump to interrupt routine _endasm } #pragma code
#pragma interrupt HandlerHigh void HandlerHigh () { //CCP1中断---------------------------------- TMR1H=0;//定时器1一定要在中断里面清0 TMR1L=0;//进入中断先清0,保证计时精度
if(PIR1bits.CCP1IF) { PIR1bits.CCP1IF = 0; //一定要在中断里面赋值 cap_low=CCPR1L; cap_high=CCPR1H; }
} #pragma code
void main() { //初始化定时器1,Capture用----------------------------------------
TRISCbits.TRISC2= 1; //设定RC2为输入
CCP1CON = 0x04;//CCP1下降沿捕捉模式
T3CON = 0x00;//定时器1是CCP1的时钟源
CCPR1H=0; CCPR1L=0;
PIE1bits.CCP1IE=1;//CCP1中断打开 PIR1bits.CCP1IF=0;//CCP1溢出标志 IPR1bits.CCP1IP = 1;//CCP1溢出中断优先级为高中断
T1CON = 0x01;//打开定时器1
PIE1bits.TMR1IE=1;//定时器1中断打开 PIR1bits.TMR1IF=0;//定时器1溢出标志 IPR1bits.TMR1IP = 1;//定时器1溢出中断优先级为高中断
INTCONbits.GIE = 1;//全局中断打开 INTCONbits.PEIE=1;//外围设备中断打开
for(;;);
} |