在捕捉形式下,当对应的CCPx引脚上有事情产生时,CCPRxH:CCPRxL寄存器对将捕捉TMR1寄存器或TMR3寄存器的16位值。事情界说为下列状况之一:
每个下降沿
每个上升沿
每4个上升沿
每16个上升沿
u经过形式挑选位CCPxM3:CCPxM0(CCPxCON《3:0》)挑选事情类型。当一个捕捉产生时,中止请求标志位CCPxIF置1,它有必要用软件清零。假如在读取寄存器CCPRx之前产生了另一个捕捉,那么之前捕捉的值将被新捕捉的值掩盖。
u在捕捉形式下,应该经过将相应的TRIS方向方位1来将CCPx引脚装备为输入
注: 假如RB3/CCP2或RC1/CCP2引脚被装备为输出,对该端口的写操作会产生捕捉条件。
u用于捕捉功用的守时器(TImer1和/或TImer3)有必要运转在守时器形式或同步计数器形式。在异步计数器形式下,无法进行捕捉操作。用于每个CCP模块的守时器由T3CON寄存器挑选。
u当捕捉形式改动时,可能会产生过错捕捉中止。用户应该坚持CCPxIE中止使能位清零,以防止过错中止。还应该在作业形式产生任何改动之后清零中止标志位CCPxIF。
u在捕捉形式下有4种预分频比值设置,它们可作为作业形式的一部分由形式挑选位(CCPxM3:CCPxM0)挑选。每逢封闭CCP模块或制止捕捉形式时,预分频计数器就将被清零。这意味着任何复位都将清零预分频计数器。
下面是在SP9608-PIC单片机增强型开发板调试的直流电机测速试验源程序,将RC2/CCP1引脚设置为输入引脚,由光电传感器将电机运转的速度转换成脉冲信号加到RC2/CCP1引脚上,程序中发动CCP模块的捕捉功用来丈量脉冲信号的周期,经过核算换算成电机运转的速度值在数码管上显示出来。
#include
#define TRUE 1
#define FALSE 0
#define HIGH 1
#define LOW 0
rom unsigned char LEDDATA[]=
{
0x3F,0x06,0x5B,0x4F,0x66,
0x6D,0x7D,0x07,0x7F,0x6F,
0x40,0x00,
};
struct TIMER_STRUCT
{
unsigned int Interval;
unsigned char Enable;
};
struct TIMER_STRUCT Timer1S;
struct CAPTURE_STRUCT
{
unsigned char Flag;
unsigned char Pointer;
unsigned int LowData;
unsigned char HighData;
};
struct CAPTURE_STRUCT MyTMR1;
unsigned char High_TMR1;
struct LED_STRUCT
{
unsigned char DotPointer;
unsigned char ScanPointer;
unsigned char Buffer[8];
};
struct LED_STRUCT NumberLED;
void PIC18F_High_isr (void);
void PIC18F_Low_isr (void);
#pragma code high_vector_section=0x8
void high_vector (void)
{
_asm goto PIC18F_High_isr _endasm
}
#pragma code low_vector_section=0x18
void low_vector (void)
{
_asm goto PIC18F_Low_isr _endasm
}
#pragma code
//—中止高优先级—//
#pragma interrupt PIC18F_High_isr
void PIC18F_High_isr (void)
{
if(TRUE==PIR1bits.TMR1IF)
{
PIR1bits.TMR1IF=FALSE;
High_TMR1++;
}
if(TRUE==PIR1bits.CCP1IF)
{
PIR1bits.CCP1IF=FALSE;
if(FALSE==MyTMR1.Flag)
{
if(0==MyTMR1.Pointer)
{
MyTMR1.Pointer++;
TMR1L=0;
TMR1H=0;
High_TMR1=0;
T1CONbits.TMR1ON=TRUE;
}
else
{
MyTMR1.Pointer=0;
T1CONbits.TMR1ON=FALSE;
MyTMR1.LowData=CCPR1H;
MyTMR1.LowData《《=8;
MyTMR1.LowData|=CCPR1L;
MyTMR1.HighData=High_TMR1;
MyTMR1.Flag=TRUE;
}
}
}
}
//—中止低优先级—//
#pragma interruptlow PIC18F_Low_isr
void PIC18F_Low_isr (void)
{
if(TRUE==PIR2bits.TMR3IF)
{
PIR2bits.TMR3IF=FALSE;
TMR3H=(65536-11962)/256;
TMR3L=(65536-11962)%256;
if(FALSE==Timer1S.Enable)
{
Timer1S.Interval++;
if(250==Timer1S.Interval)
{
Timer1S.Interval=0;
Timer1S.Enable=TRUE;
}
}
if(LOW==LATAbits.LATA0)LATAbits.LATA0=HIGH;
else LATAbits.LATA0=LOW;
LATD=LEDDATA[NumberLED.Buffer[NumberLED.ScanPointer]];
if(NumberLED.ScanPointer==NumberLED.DotPointer)LATD|=0x80;
LATE=NumberLED.ScanPointer;
NumberLED.ScanPointer++;
if(NumberLED.ScanPointer==sizeof(NumberLED.Buffer))
NumberLED.ScanPointer=0;
}
}
void main(void)
{
unsigned char i;
unsigned long temp;
float calctemp;
for(i=0;i NumberLED.ScanPointer=0;
NumberLED.DotPointer=0;
MyTMR1.Pointer=0;
MyTMR1.Flag=FALSE;
MyTMR1.HighData=0;
MyTMR1.LowData=0;
High_TMR1=0;
Timer1S.Interval=0;
Timer1S.Enable=FALSE;
TRISAbits.TRISA0=0;
LATAbits.LATA0=0;
TRISD=0;
LATD=0;
TRISE=0;
LATE=0;
TRISCbits.TRISC0=0;
LATCbits.LATC0=1;
TRISCbits.TRISC1=1;//装备为输入引脚
TRISCbits.TRISC2=1;//装备为输入引脚
CCP1CON=0x04;//捕捉形式,每个1时钟的上升沿捕捉
T3CONbits.T3CCP1=0;//Timer1作为CCP1的时钟源
T3CONbits.T3CCP2=0;
PIR1bits.CCP1IF=FALSE;//捕捉标志清零
PIE1bits.CCP1IE=TRUE;//答应捕捉中止
IPR1bits.CCP1IP=TRUE;//高优先级
T1CON=0x00;//16位守时,预分频为1:1,F=FOSC/4
//TMR1L=0;
//TMR1H=0;
PIR1bits.TMR1IF=FALSE;//守时器TMR1溢出标志清零
PIE1bits.TMR1IE=TRUE;//答应守时器TMR1溢出中止
IPR1bits.TMR1IP=TRUE;//高优先级
//T1CONbits1.TMR1ON=TRUE;//TMR1开端计时
T3CON=0x00;
TMR3H=(65536-11965)/256;
TMR3L=(65536-11965)%256;
PIR2bits.TMR3IF=FALSE;//守时器TMR3溢出标志清零
PIE2bits.TMR3IE=TRUE;//答应守时器TMR3溢出中止
IPR2bits.TMR3IP=FALSE;//低优先级
T3CONbits.TMR3ON=TRUE;//TMR3开端计时
INTCONbits.GIEH=TRUE;
INTCONbits.GIEL=TRUE;
RCONbits.IPEN=TRUE;
while(1)
{
if(TRUE==MyTMR1.Flag)
{
while(FALSE==Timer1S.Enable);
temp=MyTMR1.HighData;
temp《《=16;
temp|=MyTMR1.LowData;
calctemp=12000000;
calctemp/=temp;
calctemp*=100;
temp=calctemp;
NumberLED.DotPointer=2;
for(i=0;i i=0;
while(temp)
{
NumberLED.Buffer[i]=temp%10;
temp/=10;
i++;
}
MyTMR1.Flag=FALSE;
Timer1S.Enable=FALSE;
}
}
}
来历;21ic