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}; //存放數據uint temp="0"; //臨時變量uchar i,j; //數組及移位計數用uchar biaozhi="0"; //起始碼標志位uchar flag="1"; //有鍵按下,打開定時器void init_io(void) //初始化函數{TRISC = 0xFF; //置CCP1口為輸入態(tài)TRISD = 0x00; //置D口為輸出態(tài)CCP1CON=0x04; //CCP1模塊工作于捕捉方式CCP1IE=1; //開中斷CCP1IF=0; //清捕捉標志位PEIE=1; //開外部中斷T1CON=0; //配置定時器T1不分頻,使用內部時鐘,且先禁止T1ei(); // enable global interruptsTMR1H=0; //計數值清0TMR1L=0;PORTD=0; //輸出清0}main(){init_io(); //初始化函數while(1); //等待CCP1模塊中斷產生}void interrupt interrupt_service(void) //中斷{if (CCP1IE CCP1IF) //判斷是否為CCP1模塊中斷,當在一個系統中時會用{if(flag==1) {TMR1H=0;TMR1L=0;TMR1ON=1; //開定時器flag=0;}else{TMR1ON=0; //先停定時器temp=TMR1L+TMR1H*256; //取數據并判斷TMR1H=0; //清0計數值TMR1L=0;TMR1ON=1; //開定時器等待下一個脈沖到來if(temp>12600temp14600biaozhi==0) //起始碼{biaozhi=1; //當檢測到起始碼時,下次直接進行入數據判斷i=0;j=0;}else //數據判斷區(qū){if((temp 3200)(temp > 1200)) //時間在2.2ms左右的為數碼0{shuju[i]=shuju[i]1;j++;}elseif((temp 5000)(temp > 3000)) //時間在4ms左右的為為數碼1{shuju[i]=(shuju[i]1)+1;j++;}elsebiaozhi=0; //否則為終止碼,清0起始碼標志位,準備下一次按鍵的接收}if(j==16i2) //數組遞加,當第一個數組接收滿后,開始對下一個數組進行接收{i++;j=0;}else{if(j==16i==2) //當接收滿后(共48位),對數據輸出{if(shuju[0]==0xeb14) //為對應的某一按鍵值PORTD=0x10; //相應端口輸出if(shuju[0]==0xee11) //另一鍵值PORTD=0xa0; //輸出biaozhi=0; //接收完成,復位相應標志位flag=1;i=0;j=0;}}} TMR1IF = 0; // clear Timer0 interrupt flagCCP1IF=0; //每次必須對CCP1IF位軟件清0}}
注:本程序使用的是TOYO空調的遙控器,起始碼為13.6ms,0碼為2.2ms,1碼為4ms,停止碼為9ms.共48位數據,單片機的指令周期為1us,這個程序還沒有加校驗.
評論