PIC18F的CCP模塊的捕捉模式實(shí)現(xiàn)電機(jī)測(cè)速的方法
掃描二維碼
隨時(shí)隨地手機(jī)看文章
在捕捉模式下,當(dāng)對(duì)應(yīng)的CCPx引腳上有事件發(fā)生時(shí),CCPRxH:CCPRxL寄存器對(duì)將捕捉TMR1寄存器或TMR3寄存器的16位值。事件定義為下列情況之一:
?每個(gè)下降沿
?每個(gè)上升沿
?每4個(gè)上升沿
?每16個(gè)上升沿
u通過模式選擇位CCPxM3:CCPxM0(CCPxCON<3:0>)選擇事件類型。當(dāng)一個(gè)捕捉發(fā)生時(shí),中斷請(qǐng)求標(biāo)志位CCPxIF置1,它必須用軟件清零。如果在讀取寄存器CCPRx之前發(fā)生了另一個(gè)捕捉,那么之前捕捉的值將被新捕捉的值覆蓋。
u在捕捉模式下,應(yīng)該通過將相應(yīng)的TRIS方向位置1來將CCPx引腳配置為輸入
注: 如果RB3/CCP2或RC1/CCP2引腳被配置為輸出,對(duì)該端口的寫操作會(huì)產(chǎn)生捕捉條件。
u用于捕捉功能的定時(shí)器(Timer1和/或Timer3)必須運(yùn)行在定時(shí)器模式或同步計(jì)數(shù)器模式。在異步計(jì)數(shù)器模式下,無法進(jìn)行捕捉操作。用于每個(gè)CCP模塊的定時(shí)器由T3CON寄存器選擇。
u當(dāng)捕捉模式改變時(shí),可能會(huì)產(chǎn)生錯(cuò)誤捕捉中斷。用戶應(yīng)該保持CCPxIE中斷使能位清零,以避免錯(cuò)誤中斷。還應(yīng)該在工作模式發(fā)生任何改變之后清零中斷標(biāo)志位CCPxIF。
u在捕捉模式下有4種預(yù)分頻比值設(shè)置,它們可作為工作模式的一部分由模式選擇位(CCPxM3:CCPxM0)選擇。每當(dāng)關(guān)閉CCP模塊或禁止捕捉模式時(shí),預(yù)分頻計(jì)數(shù)器就將被清零。這意味著任何復(fù)位都將清零預(yù)分頻計(jì)數(shù)器。
下面是在SP9608-PIC單片機(jī)增強(qiáng)型開發(fā)板調(diào)試的直流電機(jī)測(cè)速實(shí)驗(yàn)源程序,將RC2/CCP1引腳設(shè)置為輸入引腳,由光電傳感器將電機(jī)運(yùn)行的速度轉(zhuǎn)換成脈沖信號(hào)加到RC2/CCP1引腳上,程序中啟動(dòng)CCP模塊的捕捉功能來測(cè)量脈沖信號(hào)的周期,通過計(jì)算換算成電機(jī)運(yùn)行的速度值在數(shù)碼管上顯示出來
#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
//---中斷高優(yōu)先級(jí)---//
#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;
}
}
}
}
//---中斷低優(yōu)先級(jí)---//
#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.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;//捕捉模式,每個(gè)1時(shí)鐘的上升沿捕捉
T3CONbits.T3CCP1=0;//Timer1作為CCP1的時(shí)鐘源
T3CONbits.T3CCP2=0;
PIR1bits.CCP1IF=FALSE;//捕捉標(biāo)志清零
PIE1bits.CCP1IE=TRUE;//允許捕捉中斷
IPR1bits.CCP1IP=TRUE;//高優(yōu)先級(jí)
T1CON=0x00;//16位定時(shí),預(yù)分頻為1:1,F(xiàn)=FOSC/4
//TMR1L=0;
//TMR1H=0;
PIR1bits.TMR1IF=FALSE;//定時(shí)器TMR1溢出標(biāo)志清零
PIE1bits.TMR1IE=TRUE;//允許定時(shí)器TMR1溢出中斷
IPR1bits.TMR1IP=TRUE;//高優(yōu)先級(jí)
//T1CONbits1.TMR1ON=TRUE;//TMR1開始計(jì)時(shí)
T3CON=0x00;
TMR3H=(65536-11965)/256;
TMR3L=(65536-11965)%256;
PIR2bits.TMR3IF=FALSE;//定時(shí)器TMR3溢出標(biāo)志清零
PIE2bits.TMR3IE=TRUE;//允許定時(shí)器TMR3溢出中斷
IPR2bits.TMR3IP=FALSE;//低優(yōu)先級(jí)
T3CONbits.TMR3ON=TRUE;//TMR3開始計(jì)時(shí)
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
while(temp)
{
NumberLED.Buffer[i]=temp%10;
temp/=10;
i++;
}
MyTMR1.Flag=FALSE;
Timer1S.Enable=FALSE;
}
}
}