我在网上找到一个例程(红外遥控),是用PIC写的,只是我没用过PIC,程序也没提到用到多大的晶振.请高手猜一下定时中断时间多大,Thanks.我想用它来做双MCU通信.
相应的C代码如下:(C5口是发送口)
#include
#include
#include
unsigned char dispcount[5]; //要发送的码值
unsigned char i,k,data,rfbit,zbit,rfgobit;
#pragma interrupt_level 1
interrupt adint(void)
{
if(TMR1IF==1)
{
TMR1IF=0;
TMR1H=0b11111100;
TMR1L=0b00010111;
rfbit=rfbit+1; //发送指针加一
send( );
}
}
//////////////////////////////////////////
send( )
{
if(rfgobit==0)
{
switch(rfbit)
{
case 1 :
RC5=0;
break;
case 6 :
RC5=1;
break;
case 7 :
RC5=0;
rfgobit=1; //制发送完标制
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 )
{
;
}
} |