السلام عليكم
كود ريموت كونترول بأستخدام بروتوكول NEC
http://up.top4top.net/downloadf-top4...a25b1-rar.html
المرسل
كود:
sbit led at RA0_bit;
sbit led_dir at trisA0_bit;
sbit sw1 at RB0_bit;
sbit sw2 at RB1_bit;
sbit sw3 at RB2_bit;
sbit sw4 at RB4_bit;
sbit sw5 at RB5_bit;
char x=0;
const char address=10;
void IR_Send(char IR_Address, char IR_COmmand);
// flashing***********************//
void flashing()
{
for(x=0;x<10;x++)
{
led=1;
delay_ms(50);
led=0;
delay_ms(50);
}
}
// main *******************************//
void main()
{
cmcon=7;
led_dir=0;// output
trisb=0xF7;
PWM1_Init(38000);// frequncy 38KHZ
PWM1_Set_Duty(178); // set duty 70%
flashing();
while(1)
{
if(sw1==1){IR_Send(address,0xAA); }
if(sw2==1){IR_Send(address,0xBB); }
if(sw3==1){IR_Send(address,0xCC); }
if(sw4==1){IR_Send(address,0xDD); }
if(sw5==1){IR_Send(address,0xFF); }
}
}
/*******************************************************************************
* Function IR_Send(char IR_Address, char IR_COmmand)
* ------------------------------------------------------------------------------
* Overview: Functions sends address and command via IR NEC protocol
* Input: Address - address of the device
* Command - command for device
* Output: Nothing
*******************************************************************************/
void IR_Send(char IR_Address, char IR_COmmand)
{
char ir_byte[4];
char bitnum;
char bytenum;
ir_byte[3] = ~IR_COmmand;
ir_byte[2] = IR_COmmand;
ir_byte[1] = ~IR_Address;
ir_byte[0] = IR_Address;
// Delay_ms(100); // precaution to be certain of some dead time before message start
PWM1_Start(); // mark
led=1;
Delay_ms(9); // 9mS mark start bit
PWM1_Stop(); // space
led=0;
Delay_us(4500); // 4.5mS space ends start bit
for(bytenum = 0; bytenum < 4; bytenum++) // 4 bytes to send
{
for(bitnum = 0; bitnum < 8; bitnum++) // 8 bits LSB first
{
PWM1_Start(); // mark
led=1;
Delay_us(560); // 560uS mark for '0' or '1'
PWM1_Stop(); // space
led=0;
if(ir_byte[bytenum] & (1 << bitnum)) // test bit <bitnum>
{
Delay_us(1680); // 1.68mS space for a '1'
}
else{
Delay_us(560); // 560uS space for a '0'
}
}
}
PWM1_Start(); // mark
led=1;
Delay_us(560); // stop bit of '1'
PWM1_Stop(); // space
led=0;
Delay_us(1680); // stop bit of 1
Delay_ms(200); // precaution to be certain of some dead time after message ends
}
============================================
المستقبل
كود:
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;
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))
{
IR_Received_Flag =1;
}
}
}
}
//**** main********************************************
void main()
{
cmcon=7;// OFF comparator
ir_in_dir =1;
trisb=0;
portb=0;
InitTimer0();
while(1)
{
remote_read(address_advice);
if (IR_Received_Flag == 1)
{
GIE_bit=0;
if ((IR_Received_byte[0] == ~IR_Received_byte[1]) && (IR_Received_byte[2] == ~IR_Received_byte[3]))
{
if (IR_Received_byte[0] == address_advice) // correct address
{
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;
}
}
}