PIC16F887紅外解碼程序
//----------------------------------------------------------------//PIC16F887 紅外解碼程序//指令周期1us,#include htc.h> // header file with compiler macros, for// example __CONFIG// the processor header file, with register // and bit definitions (pic16f887.h) is// automatically included#define uchar unsigned char#define uint unsigned int// __CONFIG sets the PIC16F887 Configuration Words. See bottom of the header// file pic16f887.h for fuse option constants.__CONFIG(INTCLK WDTDIS PWRTDIS
MCLRDIS UNPROTECT DUNPROTECT BORDIS IESODIS FCMDIS LVPDIS);__CONFIG(BORV40); // 2nd config word////// /////////////////////////////////////////////////uint shuju[3]={0}; //存放數(shù)據(jù)uint temp="0"; //臨時(shí)變量uchar i,j; //數(shù)組及移位計(jì)數(shù)用uchar biaozhi="0"; //起始碼標(biāo)志位uchar flag="1"; //有鍵按下,打開定時(shí)器void init_io(void) //初始化函數(shù){TRISC = 0xFF; //置CCP1口為輸入態(tài)TRISD = 0x00; //置D口為輸出態(tài)CCP1CON=0x04; //CCP1模塊工作于捕捉方式CCP1IE=1; //開中斷CCP1IF=0; //清捕捉標(biāo)志位PEIE=1; //開外部中斷T1CON=0; //配置定時(shí)器T1不分頻,使用內(nèi)部時(shí)鐘,且先禁止T1ei(); // enable global interruptsTMR1H=0; //計(jì)數(shù)值清0TMR1L=0;PORTD=0; //輸出清0}main(){init_io(); //初始化函數(shù)while(1); //等待CCP1模塊中斷產(chǎn)生}void interrupt interrupt_service(void) //中斷{if (CCP1IE CCP1IF) //判斷是否為CCP1模塊中斷,當(dāng)在一個(gè)系統(tǒng)中時(shí)會(huì)用{if(flag==1) {TMR1H=0;TMR1L=0;TMR1ON=1; //開定時(shí)器flag=0;}else{TMR1ON=0; //先停定時(shí)器temp=TMR1L+TMR1H*256; //取數(shù)據(jù)并判斷TMR1H=0; //清0計(jì)數(shù)值TMR1L=0;TMR1ON=1; //開定時(shí)器等待下一個(gè)脈沖到來(lái)if(temp>12600temp14600biaozhi==0) //起始碼{biaozhi=1; //當(dāng)檢測(cè)到起始碼時(shí),下次直接進(jìn)行入數(shù)據(jù)判斷i=0;j=0;}else //數(shù)據(jù)判斷區(qū){if((temp 3200)(temp > 1200)) //時(shí)間在2.2ms左右的為數(shù)碼0{shuju[i]=shuju[i]1;j++;}elseif((temp 5000)(temp > 3000)) //時(shí)間在4ms左右的為為數(shù)碼1{shuju[i]=(shuju[i]1)+1;j++;}elsebiaozhi=0; //否則為終止碼,清0起始碼標(biāo)志位,準(zhǔn)備下一次按鍵的接收}if(j==16i2) //數(shù)組遞加,當(dāng)?shù)谝粋€(gè)數(shù)組接收滿后,開始對(duì)下一個(gè)數(shù)組進(jìn)行接收{(diào)i++;j=0;}else{if(j==16i==2) //當(dāng)接收滿后(共48位),對(duì)數(shù)據(jù)輸出{if(shuju[0]==0xeb14) //為對(duì)應(yīng)的某一按鍵值PORTD=0x10; //相應(yīng)端口輸出if(shuju[0]==0xee11) //另一鍵值PORTD=0xa0; //輸出biaozhi=0; //接收完成,復(fù)位相應(yīng)標(biāo)志位flag=1;i=0;j=0;}}} TMR1IF = 0; // clear Timer0 interrupt flagCCP1IF=0; //每次必須對(duì)CCP1IF位軟件清0}}
注:本程序使用的是TOYO空調(diào)的遙控器,起始碼為13.6ms,0碼為2.2ms,1碼為4ms,停止碼為9ms.共48位數(shù)據(jù),單片機(jī)的指令周期為1us,這個(gè)程序還沒(méi)有加校驗(yàn).
評(píng)論