PIC單片機紅外遙控編碼與解碼的問題分享
#pragma interrupt_level 1
interrupt adint(void)
{
if(TMR1IF==1)
{
TMR1IF=0;
TMR1H=0b11111100;
TMR1L=0b00010111;
rfbit=rfbit+1;//發(fā)送指針加一
send( );
}
}
//////////////////////////////////////////
send( )
{
if(rfgobit==0)
{
switch(rfbit)
{
case 1 :
RC5=0;
break;
case 6 :
RC5=1;
break;
case 7 :
RC5=0;
rfgobit=1;//制發(fā)送完標制
rfbit=0;
break;
default :
break;
}
}
if(rfgobit==1)
{
zbit=dispcount[i]0b00000001;
switch(rfbit)
{
case 1:
RC5=1;
break;
case 2 :
if(zbit==0)
{
RC5=0;
rfbit=0;
rf( );
}
break;
case 3 :
if(zbit==1)
{
RC5=0;
rfbit=0;
rf( );
}
break;
default :
break;
}
}
}
//////////////////////////////////////////////
rf( )
{
k=k+1;
if(k==8)
{
i=i+1;
k=0;
if(i==6)
{
i=1;
rfgobit=0;
TMR1H=0;
TMR1L=0;
RC5=1;
dispcount[1]=0xaa;
dispcount[2]=0xbb;
dispcount[3]=0xcc;
dispcount[4]=0xdd;
dispcount[5]=0xee;
}
return;
}
dispcount[i]=dispcount[i]>>1;
}
//////////////////////////////////////
main( )
{
di( );
TRISC=0b11011111;
TRISA=0b111111;
TRISB=0b11111111;
i=1;
k=0;
TMR1H=0b11111100;
TMR1L=0b00010111;
T1CON=1;
TMR1IE=1;
RC5=1;
PEIE=1;
ei( );
dispcount[1]=0xaa;
dispcount[2]=0xbb;
dispcount[3]=0xcc;
dispcount[4]=0xdd;
dispcount[5]=0xee;
while( 1 )
{
;
}
}
這兩個程序只是一個演示例程本文引用地址:http://www.ex-cimer.com/article/172729.htm
在實際運用中可變通的把這種發(fā)送與接受用于兩機之間的通訊(好處是移植性好,抗干擾好且一發(fā)一收只要兩根線或一根線)另外如要產(chǎn)生載波(38KHZ)的話可用其輸出口去調制38KHZ發(fā)生源(比如555電路,PWM)來得到,或干脆用軟件來實現(xiàn)(不過難度較大;不如用帶38KHZ的單片機來做)
評論