Infrared starts sending a 13.5ms boot code. The boot code is composed of a high level of 9ms and a low level of 4.5ms. Followed by the boot code is the system code, the system code, the key code, and the key code, if the key is not pressed Put, then the remote controller sends a repetition code, the repetition code is from 9ms high, 2.25ms low, followed by a short pulse.
#include "at89x52.h"
#defineNULL0x00// invalid data
#defineRESET0X01//Program reset
#defineREQUEST0X02//Request Signal
#defineACK0x03//Answer signal, sending ACK signal after receiving data indicates that the data is received correctly.
Also respond to the request signal
#defineNACK0x04//Answer signal, indicating that the received data is incorrect
#defineBUSY0x05//Busy signal indicating busy
#defineFREE0x06//Idle signal, indicating idle
#defineREAD_IR0x0b//Read IR
#defineSTORE_IR0x0c//Save data
#defineREAD_KEY0x0d//Read key values
#defineRECEIVE0Xf400//Receive buffer start address
#defineSEND0xfa00//Send buffer start address
#defineIR0x50//Infrared Receive Buffer Start Address
#defineHEAD0xaa//Data Frame Header
#defineTAIL0x55//data frame end
#defineSDAP1_7
#defineSCLP1_6
Unsigned char xdata *buf1;// accept data buffering
Unsigned intbuf1_length;//The actual length of the received data
Unsigned char xdata *buf2;//send data buffer
Unsigned intbuf2_length;//The actual length of the data to be sent
Bit buf1_flag;//reception flag, 1 indicates that a data frame was received, 0 indicates no data frame or data was received
Frame is empty
Bit buf2_flag;//send flag, 1 indicates that transmission is required or not completed, 0 indicates no data to be transmitted or
Sent
Unsigned char state1, state2;// used to mark the state of the receiving character, state1 used to represent
Receive state, state2 used to indicate the sending status
Unsigned char data *ir;
Union{
Unsigned char a[2];
Unsigned int b;
Unsigned char data *p1[2];
Unsigned int data *p2[2];
Unsigned char xdata *p3;//IR buffer pointer
Unsigned int xdata *p4;
}p;
//union{//
//unsigned char a[2];//
//unsigned int b;
//unsigned char data *p1[2];
//unsigned int data *p2[2];
//unsigned char xdata *p3;
//unsigned int xdata *p4;//address pointer
//}q;//
Union{
Unsigned char a[2];
Unsigned int b;
}count;
Union{
Unsigned char a[2];
Unsigned int b;
}temp;
Union{
Unsigned char a[4];
Unsigned int b[2];
Unsigned long c;
}ir_code;
Union{
Unsigned char a[4];
Unsigned int b[2];
Unsigned long c;
Unsigned char data *p1[4];
Unsigned int data *p2[4];
Unsigned char xdata *p3[2];
Unsigned int xdata *p4[2];
}I;
Unsigned char ir_key;
Bit ir_flag;//Infrared reception flag, 0 is buffer empty, 1 is successful, 2 is buffer overflow
Void sub(void);
Void delay(void);
Void ie_0(void);
Void tf_0(void);
Void ie_1(void);
Void tf_1(void);
Void tf_2(void);
Void read_ir(void);
Void ir_jiema(void);
Void ir_init(void);
Void ir_exit(void);
Void store_ir(void);
Void read_key(void);
Void reset_iIC(void);
Unsigned char read_byte_ack_iic(void);
Unsigned char read_byte_nack_iic(void);
Bit write_byte_iic(unsigned char a);
Void send_ack_iic(void);
Void send_nack_iic(void);
Bit receive_ack_iic(void);
Void start_iic(void);
Void stop_iic(void);
Void write_key_data(unsigned char a);
Unsigned int read_key_data(unsigned char a);
Void ie0(void)interrupt 0{ie_0();}
Void tf0(void)interrupt 1{tf_0();}
Void ie1(void)interrupt 2{ie_1();}
Void tf1(void)interrupt 3{tf_1();tf_2();}
Void tf2(void)interrupt 5{// Using a combination of interrupt mode and query mode decoding
EA=0;//disabling interrupts
If (TF2) {//determine whether it is an overflow or an interrupt generated by a level change
TF2 = 0; / / If the interrupt generated by the overflow is to clear the overflow bit, re-open the interrupt to exit
EA=1;
Goto end;
}
EXF2 = 0; / / clear interrupt level generated interrupt
*ir=RCAP2H;//Save the captured number
Ir++;
*ir=RCAP2L;
*ir++;
F0=1;
TR0=1; //Open Counter 0
Loop:
TL0=0; //Reset counter 0 to zero
TH0=0;
While(!EXF2){//Query waits for EXF2 to become 1
If(TF0)goto exit;//Check if there is a timeout, exit if it times out
};
EXF2 = 0; / / clear EXF2
If(!TH0)//Decide whether it is a long low pulse.
{// Not a long low pulse but a short low
If(F0)count.b++;//short pulse number plus one
Temp.a[0]=RCAP2H;//Temporarily store the capture number
Temp.a[1]=RCAP2L;
Goto loop;//Return to query
}
Else{// is a low-level pulse, then processing
F0=0;
*ir=temp.a[0];// Record the total time of consecutive short pulses
Ir++;
*ir=temp.a[1];
Ir++;
*ir=RCAP2H;// Record the long pulse time
Ir++;
*ir=RCAP2L;
Ir++;
If(ir)=0xda) {
Goto exit;//to determine whether the buffer overflow, if the overflow fails to exit
}
Goto loop;//Return to query
}
Exit:
Ir_flag=1;// Setting ir_flag to 1 indicates successful reception
End:
;
}
Void rs232(void)interrupt4{
staTIc unsigned char sbuf1, sbuf2, rsbuf1, rsbuf2; // sbuf1, sbuf2 is used to receive
Send temporary use, rsbuf1, rsbuf2 are used to store the received and transmitted nibbles, respectively
EA=0;//disabling interrupts
If(RI){
RI=0; //Clear receive interrupt flag
Sbuf1=SBUF;//Copy the received buffered characters to sbuf1
If(sbuf1==HEAD){//Judge whether the frame starts
State1=10;// is to assign state to 10
Buf1=RECEIVE; // Initialize the receive address
}
Else{
Switch(state1){
Case 10:sbuf2=sbuf1â€â€4; //Kick right nibble to high nibble
Sbuf2=~sbuf2;//Reverse the lower nibble
If((sbuf2&0x0f)!=(sbuf1&0x0f))//Decide if the reception is correct
{// Receive error, it is possible to receive the end of the data frame, it may be a receive error
If(sbuf1==TAIL)//determine if data frame end is received
{// is the end of the data frame received
Buf1=RECEIVE; // Initialize the received address
If(*buf1==RESET)//Decide whether to reset command
{
ES=0;
Sbuf2=SP+1;
For(p.p1[0]=SP-0x10;p.p1[0]"=sbuf2;p.p1
[0]++)*p.p1[0]=0;
}
State1=0;// Set the receive status flag to zero and receive the next data frame
Buf1_flag=1;//Set the receive flag to 1, indicating that a data frame has been received
REN=0;//No reception
}
Else
{// Not receiving end of data frame, indicating receipt error
State1=0;// Set the receive status flag to zero and re-receive
Buf1=RECEIVE; // Initialize the address sent
*buf1=NACK;//Store NACK signal in receive buffer
Buf1_flag=1;// Set the flag bit to 1, so that the main program can handle the receiving error
REN=0;//No reception
}
}
Else
{// Received correctly
Rsbuf1=~sbuf1;//Bitwise inversion, making the high half-byte change the original code
Rsbuf1&=0xf0; //Reserve only the upper nibble and the lower nibble
State1=20;//Set the status flag to 20, ready to receive the lower nibble
}
Break;
Case 20: sbuf2=sbuf1â€â€4; //Kick the right nibble to the high nibble
Sbuf2=~sbuf2;//Reverse the lower nibble
If((sbuf2&0x0f)!=(sbuf1&0x0f))//Decide if the reception is correct
{//Accept error
State1=0;// Set the receive status flag to zero and re-receive
Buf1=RECEIVE; // Initialize the received address
*buf1=NACK;//Store NACK signal in send buffer
Buf1_flag=1;// Set the flag bit to 1, so that the main program can handle the receiving error
REN=0;//No reception
}
Else
{
Sbuf1&=0x0f; //Reserve only the lower nibble and remove the high nibble
Rsbuf1 "=sbuf1;//high and low nibble merge
* Buf1 + + = rsbuf1; / / will receive data stored in the receive buffer, and the data pointer plus one
Buf1_length++;// Receive data length plus one
State1=10; //Set state1 to 10, ready to receive the upper nibble of the next byte
}
Break;
}
}
}
Else{
TI=0; //Clear transmit interrupt flag
If(buf2_length) // judge whether the sending length is zero
{// send length is not zero
If(state2==0)//Decide whether to send high nibbles
{// Send high nibbles
Sbuf2=*buf2;//Send the bytes to be sent to sbuf2
Rsbuf2=~sbuf2;//invert, to make the upper nibble into inverted
Sbuf2""=4;//Move right high nibble to low nibble
Rsbuf2 & = 0xf0; / / retain the high nibble, remove the lower nibble
Sbuf2&=0x0f;//Reserve the low nibble and remove the high nibble
Rsbuf2|=sbuf2;//Combining high and low nibbles
SBUF=rsbuf2;//Send it
State2=10; // Set state2 to 10 to send the lower nibble
}
Else
{// Send low nibble
Sbuf2=*buf2;//Send the bytes to be sent to sbuf2
Buf2++;//pointer plus one
Buf2_length--;// send data length minus one
Rsbuf2=~sbuf2;//Reverse to make the lower nibble into an inverted
Rsbuf2 "" = 4; / / to lower the lower half of the anti-code to the high nibble
Rsbuf2 & = 0xf0; / / retain the high nibble, remove the lower nibble
Sbuf2&=0x0f;//Reserve the low nibble and remove the high nibble
Rsbuf2|=sbuf2;//Combining high and low nibbles
SBUF=rsbuf2;//Send out
State2=0;
}
}
Else
{// If the send data length is zero then send data frame end
If(buf2_flag){//determine if the end of the data frame has been sent
SBUF=TAIL;//Send the end of the data frame
While(TI==0);
TI=0;
Buf2_flag=0;//Set the send flag to zero, indicating that the send is complete
}
}
}
EA=1;//Open interrupt
}
Photography Accessories,Camera Flash Diffuser,Lantern Softbox Diffuser,Photography Light Diffuser
SHAOXING SHANGYU FEIXIANG PHOTOGRAPHIC CO.,LTD , https://www.flying-photography.com