PIC單片機(jī)紅外遙控編碼與解碼的問題分享
一般常用的紅外遙控器編碼規(guī)則都差不多,基本上都同6221原理一樣
本文引用地址:http://www.ex-cimer.com/article/172729.htm如果用54,57這類片子做的話有一定的難度(假如要做成實(shí)時(shí)控制的;比如說你還要驅(qū)動顯示,驅(qū)動步進(jìn)電機(jī),在加上幾個(gè)按鍵)原因就是這類片子沒有中斷
例程如下(用來解6221;分頻比為256)
RF:
BTFSC PORTB,2;;B2口用做接收口
GOTO RF1
BTFSS DOWNBIT;;檢測下降沿標(biāo)制
CLRF RTCCOUNT
BSF DOWNBIT;制下降沿標(biāo)制
BTFSS UPBIT;;檢測上升沿標(biāo)制
RETLW 0
BTFSC IDBIT;;檢測碼頭標(biāo)制
GOTO RF3
MOVLW 2AH
SUBWF RTCCOUNT,0
BTFSS STATUS,0
GOTO RF2
MOVLW 36H
SUBWF RTCCOUNT,0
BTFSC STATUS,0
GOTO RF2
BTFSC IDBIT
GOTO RF3
MOVLW .8
MOVWF LOOP
MOVLW .3
MOVWF LOOPCOUNT
CLRF DATACOUNT
BSF IDBIT
BSF DOWNBIT
BCF UPBIT
CLRF RTCCOUNT
RETLW 0
RF1:
BTFSS DOWNBIT
RETLW 0
BSF UPBIT
RETLW 0
RF2:
BCF DOWNBIT
BCF UPBIT
BCF IDBIT
CLRF RTCCOUNT
RETLW 0 ;遙控接收
RF3:
MOVLW 02H
SUBWF RTCCOUNT,0
BTFSS STATUS,0
GOTO RF4
MOVLW 0CH
SUBWF RTCCOUNT,0
BTFSS STATUS,0
GOTO RF4
GOTO RF2
RF4:
MOVLW 08H
SUBWF RTCCOUNT,0
BTFSC STATUS,0
BSF 3H,0
MOVLW 07H
SUBWF RTCCOUNT,0
BTFSS STATUS,0
BCF 3H,0
RLF DATACOUNT,1
BSF DOWNBIT
BCF UPBIT
CLRF RTCCOUNT
DECFSZ LOOP,1
RETLW 0
MOVLW .8
MOVWF LOOP
DECFSZ LOOPCOUNT
RETLW 0
BSF RFBIT;;制接收完標(biāo)制
BCF DOWNBIT
BCF UPBIT
BCF IDBIT
CLRF RTCCOUNT
RETLW 0
//////////////////////////////////////////////////////////
TIME:
BTFSC TIMEPD1
GOTO TIME1
MOVF RTCC,0;;(
MOVWF TIMEONE
BSF TIMEPD1
RETLW 0 ;定時(shí)查尋
TIME1:
MOVF RTCC,0
SUBWF TIMEONE,0
BTFSC STATUS,2
RETLW 0
BCF TIMEPD1
INCF RTCCOUNT,1
RETLW 0
////////////////////////////////////////////////
在這里我是用查詢的方式來定時(shí)的(RTCCOUNT)只是在解碼時(shí)不需要去追求時(shí)間精度;我是去查RTCC有沒有發(fā)生跳變?nèi)缬袆t表示時(shí)間過了 256US---RTCCOUNT加一;這樣做有一個(gè)好處---你不必去管RTCC具體的值是多少,(RTCC去做精確的時(shí)鐘定時(shí);在這個(gè)查詢的子程序中你可以去判斷鍵掃,顯示刷新,驅(qū)動步進(jìn)電機(jī)等等)
相應(yīng)的C代碼如下:
unsigned char rfcount,
loop,
rftime,//查詢定時(shí)器
k;
bit rfbit, //接收完標(biāo)制
lowbit1,
lowbit2,
downbit,
rfgobit;
unsigned char dispcount[5];//結(jié)果
#define rfin RC6
////////////////////////////////////////////////////////////////////////////////
rf( )//遙控接收
{
if(rfbit==0)
{
if((lowbit1==0)(rfin==0))
{
downbit=1;
rftime=0;
lowbit1=1;
return;
}
if((lowbit1==1)(rfin==1))
{
lowbit2=1;
return;
}
if((lowbit1==1)(lowbit2==1)(RC6==0))
{
lowbit1=0;
lowbit2=0;
if((rftime>=40)(downbit==1))//遙控接收;
{
rfgobit=1;
loop=0;
rfcount=0;
k=1;
rftime=0;
return;
}
rfcount=rfcount+1;
loop=loop+1;
if(rfcount>=31)
{
rfgobit=0;
downbit=0;
rfcount=0;
rfbit=1;
loop=0;
return;
}
if((rftime>=7)(rfgobit==1))
{
dispcount[k]=dispcount[k]|0x80;
rftime=0;
if(loop==8)
{
k=k+1;
loop=0;
return;
}
dispcount[k]=dispcount[k]>>1;
return;
}
if((rftime5)(rfgobit==1))
{
dispcount[k]=dispcount[k]0x7f;
rftime=0;
if(loop==8)
{
k=k+1;
loop=0;
return;
}
dispcount[k]=dispcount[k]>>1;
return;
}
}
}
}
(查詢子程序同匯編)
評論