السلام عليكم
هذا نفس مثال ولكن سهلت عليك الكود
ملاحظه الكود بلون أحمر هو كود ثابت لمستقبله IR _NEC
http://up.top4top.net/downloadf-top4...19a61-rar.html
التردد هو 4mhz
اما كود بلون الاخضر فهو متاح لك لتعديل عليه
كود:
sbit ir_in at RA0_bit;
sbit ir_in_dir at TRISA0_bit;
sbit led1 at RB0_bit;
sbit led2 at RB1_bit;
sbit led3 at RB2_bit;
sbit led4 at RB3_bit;
//************************** Code IR_Reciver *******************************//
const char address_advice=10;
unsigned char IR_Received_byte[4];
unsigned char IR_state=0 , IR_Timer_Count = 0 , IR_Received_Flag;
unsigned char IR_Bit_Count = 0 , IR_Byte_Count=0 ;
unsigned int retint,count=0;
//Timer0
void InitTimer0()
{
option_reg=0b10000000;
intcon=0b10100000;
tmr0=0;
}
//*********************************************//
void Interrupt()
{
if (TMR0IF_bit){
TMR0IF_bit = 0;
TMR0 = 0;
count++;
}
}
//***************************************************//
unsigned int get_space()
{
TMR0= 0; count=0;
for(;!ir_in;);
retint=2*(TMR0+(count*256));
return retint;
}
//*/********************************************************//
unsigned int get_mark(){
count=0;
TMR0= 0;
while(ir_in);
retint=2*(TMR0+(count*256));
return retint;
}
//*************************************************************//
unsigned int get_mark1(){
count=0;
TMR0= 0;
while(!ir_in);
retint=2*(TMR0+(count*256));
return retint;
}
//////////////////////////************************************///////////////
unsigned char get_remote_byte()
{
char remote;
unsigned char ir_byte;
unsigned int temp_ir;
ir_byte = 0; // initialise
for(remote=0 ; remote<8 ; remote++)
{
temp_ir = get_space();
if ((temp_ir > 430)&&(temp_ir < 600))
{
temp_ir = get_mark();
ir_byte >>= 1; // 650uS = 0, 1680uS = 1
if ((temp_ir > 700)&&(temp_ir < 1900))
{
ir_byte += 0x80;
}
}
else break;
}
return ir_byte;
}
//*****************************************************************************************//
unsigned char remote_read(const char device_address)
{
unsigned int temp_ir;
ir_in_dir =1;
IR_Received_byte[0]=0;
IR_Received_byte[1]=0;
IR_Received_byte[2]=0;
IR_Received_byte[3]=0;
while(ir_in);// wait for IR to go low
temp_ir = get_mark1();
if((temp_ir>7000)&&(temp_ir<11000))
{ // expect 4.5mS mark
temp_ir = get_mark();// wait for repeat messages to clear
if((temp_ir>3700)&&(temp_ir<5300))
{
IR_Received_byte[0] = get_remote_byte();// 7 bit command
IR_Received_byte[1] = get_remote_byte();// 5 bit address
IR_Received_byte[2] = get_remote_byte();// 7 bit command
IR_Received_byte[3] = get_remote_byte();// 5 bit address
temp_ir = get_space();
if ((temp_ir > 430)&&(temp_ir < 600))
{
if ((IR_Received_byte[0] == ~IR_Received_byte[1]) && (IR_Received_byte[2] == ~IR_Received_byte[3]))
{
if(IR_Received_byte[0]==device_address)IR_Received_Flag =1;
}
}
}
}
}
//**** main********************************************
void main()
{
cmcon=7;// OFF comparator
ir_in_dir =1;
InitTimer0();
trisb=0;
portb=0;
while(1)
{
remote_read(address_advice);
if (IR_Received_Flag == 1)
{
GIE_bit=0;
switch (IR_Received_byte[2])// data store in IR_Received_byte[2]
{
case 0xAA :{ led1=~led1;delay_ms(150); break;}
case 0xBB :{ led2=~led2;delay_ms(150) ;break;}
case 0xCC :{ led3=~led3;delay_ms(150); break;}
case 0xDD :{ led4=~led4;delay_ms(150);break;}
case 0xFF :{ portb=~portb;delay_ms(150);break;}
default : break;
}
IR_Received_Flag = 0;
GIE_bit=1;
}
}
}