Single-chip microcomputer to do infrared remote control decoder

Does your home have a TV remote control or an air conditioner remote? Do you also want it to remotely control other appliances or even let it remotely control your computer? Well, let me do this "infrared remote control decoder".

The small production needs few components: one single chip TA89C2051, one RS232 interface level and TTL level conversion core chip MAX232CPE, one infrared receiving tube, crystal oscillator 11.0592MHz, electrolytic capacitor 10uF4 only, 10uF one, resistor 1K1, 1 around 300 ohms, 30P2 ceramic capacitors. 8 LEDs. The price is less than 20 yuan.

Introduction to circuit principle:

The main control unit is a single-chip AT89C2051, the interrupt port INT0 is connected with the infrared receiving tube U1, receives the pulse of the infrared signal, and 8 LEDs are used as the display decoding output (also can be used to extend the connection to other control circuits), U3 is the serial port with the computer. The level conversion card is connected when RS232 is connected. The pins 9 and 10 are connected to the pins 1 and 2 of the MCU respectively. (1 pin is serial reception, 2 pins are serial transmission), and the 7 and 8 pins of MAX232CPE are respectively connected to the computer string. 2 (receive) pin and 3 (send pin) of the line port. The crystal oscillator adopts 11.0592MHz, so that the baud rate of communication can reach 9600b/s. The default value of the computer is 9600b/s, 8 data bits, 1 stop bit, and no parity. The circuit is so simple, now analyze the specific programming process.

As shown, the waveform of the panasonic remote is like this (through repeated testing).

The start bit is 3.6ms low and then 3.6ms high, then the data representation is 0.9ms low 0.9ms high period 1.8ms means "0", 0.9ms low 2.4ms high The flat period is 3.3ms for “1”. When writing the program, the high level is greater than 3.4ms and less than 3.8ms, and the high level is greater than 2.2ms and less than 2.7ms. “1” is greater than 0.84ms and less than 1.11ms. A high level indicates "0". Therefore, we mainly use the single chip to measure the length of the high level to determine whether it is "1" or "0". The operation mode of timer 0 is set to mode 1: mov tmod, #09h, so setting timer 0 is to set GATE to 1, 16-bit counter, the maximum count value is 2 to 16 power cycles, this mode is outside The INT0 control is interrupted, that is, the counter is allowed to count when INT0 is high. such as:

Jnb p3.2, $

Jb p3.2,$

Clr tr0

These three instructions can measure a high level, and then read the count value TH0, TL0 can distinguish whether the start bit is "1" or "0". Before determining the code table, you can use the 8 LEDs of port P0 to display the code, and the 16-bit code is displayed twice:

Mov p0, keydata

Acall delay_1s ;//1ms delay subroutine

Mov p0, keydata+1

Ljmp main

According to the code of the two successive displays of P0, the code of each button is recorded to form a code table, that is, the decoding of the remote control code is completed. After the code table is determined, after receiving the code of the remote controller, it compares with the code table, finds the matching code item, and outputs the sequence number corresponding to the code item to the P0 port, and also outputs the sequence number to the serial port. To the computer, the computer determines what to do after receiving the data.

The program is not long, the following is the complete program and comments: (see the flow chart first)

Keydata equ 30h ;//This address and 31H address are used to store the remote control button code.

Org 00h

Main:

Mov keydata,#0 ;// clear

Mov tmod , #09h ;//Set timing 0 mode 1, GATE=1

Mov r7, #0 ;/ / counter, used to count whether it is full 8 bits

Mov r6, #0 ;/ / counter, used to count whether it is full 2 ​​bytes (solve 16-bit encoding)

Jb p3.2, $ ;/ / whether it is low

Again: ;//If it is low, continue to execute below

Mov tl0, #0 ;//clear TL0

Mov th0, #0 ;//clear TH0

Setb tr0 ;/ / turn on timer 0

Jnb p3.2, $ ;//waiting for a high level

Jb p3.2, $ ; / / high arrival, start counting at this time

Clr tr0 ;/ / high end, stop counting

Mov a, th0 ;/ / read the value of th0, TL0 ignores clr c ;/ /

Subb a,#12 ;//

Jc again ;//th0 "12 turns, that is less than 3.4ms, you can count this time

Mov a, #14 ;//

Clr c ;//

Subb a, th0 ;/ / and 14 comparison, if TH0 "14 is greater than 3.8ms

Jc again ;//greater than 3.8ms, retesting from new

Nextbit: ;// The start bit is found, then the next one

Mov tl0, #0 ;//

Mov th0,#0 ;//

Setb tr0 ;// start timer

Jnb p3.2, $ ;//waiting high level

Jb p3.2, $ ; / / high arrival, start counting at this time

Clr tr0 ;/ / high end, stop counting

Mov a,th0 ;//read the count value, TL0 ignores

Clr c ;//

Subb a, #8 ;//th0 and 8 comparison

Jc next ;;;;//If "2.2ms then turn, then judge whether it is greater than 0.84ms

Mov a, #10 ;// compare with 10

Clr c ;//

Subb a,th0 ;//

Jc again ;;;;;;;//If 2.7ms, give up, retest

Mov a,keydata ;// matches more than 2.2ms and is less than 2.7ms, which is "1"

Setb c ;//C = 1

Rrc a ;//shift 1 into A

Mov keydata,a ;//save

Inc r7 ;//counter plus 1

Cjne r7, #8, nextbit ;/ / is full 8 bits

Inc r6 ;//count plus 1

Cjne r6, #2,last8 ;//full two bytes

Sjmp seach ;//less than two bytes, new acquisition

Last8: ;//full 1 byte, then the second byte

Mov keydata+1,a ;//Save the first byte of encoded data to 31h

Mov r7, #0 ;//Counter R7 clear

Sjmp nextbit ;// continue to collect data

Next: ;//When it is less than 2.2ms, go here

Mov a,th0 ;//read count value TH0

Swap a ;// high 4 bit and low 4 bit swap

Mov r1,a ;//Save to R1

Anl tl0, #0f0h ;/ / take TL0 high 4 digits, low 4 digits ignore

Mov a,tl0 ;//

Clr c ;//

Rrc a ;//

Rrc a ;//

Rrc a ;//

Rrc a ;//

Add a,r1 ;//

Mov r1,a ;//

Subb a, #30 ;//The above lines combine the lower 4 bits of TH0 and the upper 4 bits of TL0 into 1 byte as the count value.

Jc nextbit ; / / determine whether "0.84ms, yes, give up, continue to collect

Mov a,r1 ;//No

Clr c ;//

Cjne a, #64,conTInue ;//Compare with 64

conTInue: ;//

Jnc nextbit ; //a"64 means sample value" 1.11ms give up

Mov a,keydata ;//other, match bit "0"

Clr c ;//C = 0

Rrc a ;//move zero right into A

Mov keydata,a ;//save

Inc r7 ;//counter plus 1

Cjne r7, #8, nextbit ;/ / is full 8 bits

Inc r6 ;//counter plus 1

Cjne r6, #2,last_8 ;//The first byte is full

Sjmp seach ;//

Last_8: ;//if it is the second byte

Mov keydata+1, a ;/ / then save the first byte to 31h

Mov r7, #0 ;//clear R7

Sjmp nextbit ;//

Seach: ;//match key code

Mov r0, #-2 ;// button code byte count counter

Mov r1, #-1 ;// button sequence counter

Seach1: ;//

Inc r0 ;//

Seach2: ;//

Inc r0 ;//

Inc r1 ;//

Cjne r1, #29,compare ;//whether R1=29

Sjmp exit0 ;//

Compare: ;// start matching

Mov a,r0 ;//

Mov dptr, #keycode ;/ / address pointer points to the first address of the code table

Movc a, @a+dptr ;// take code

Cjne a,keydata,seach1 ;//comparison

Inc r0 ;//R0+1, then compare the next byte (each key is encoded as 2 bytes)

Mov a,r0 ;//

;mov dptr,#keycode ;//

Movc a,@a+dptr ;//comparison

Cjne a, keydata+1,seach2 ;//match, no match, continue to compare with the next byte

Mov p1,r1 ;//If it matches, output the key sequence number to p1

Send: ;//

Mov tmod, #20h ; //Set TImer 1, mode 2

Mov tl1, #0fdh ;//set the initial value of the timer

Mov th1, #0fdh ;//

Mov scon, #01010000b; / / above settings, that is, set the serial port baud rate coefficient: 9600, 8, 1, 0

Setb tr1 ;// start timer 1

Loop_s: ;//

Mov sbuf,r1 ;//output R1 (key sequence number) to the serial port

Jnb TI, $ ;//waiting whether the transmission is completed

Clr ti ;/ / send completed, clear TI

Exit0: ;//

Ljmp main ;//loop

Keycode: ;// every two bytes represents the encoding of a button

Db 11111000b, 000000000b, 11111100b, 000000000b, 11111001b, 11000000b

Db 11111100b,11000000b, 11111010b,00000000b, 11111010b,00100000b

Db 11111010b,01000000b, 11111010b,01100000b, 11111010b,10000000b

Db 11111010b,10100000b, 11111010b,11000000b, 11111010b,11100000b

Db 11111011b, 000000000b, 11111011b, 00100000b, 11111011b, 01000000b

Db 11111011b,01100000b, 11111111b,01100000b, 11111111b,10100000b

Db 10001100b, 10001110b, 10001101b, 11101110b, 10001100b, 10101110b

Db 10001101b, 11001110b, 11111000b, 11100000b, 11111100b, 10000000b

Db 11111100b, 01000000b, 11111001b, 10100000b, 11111100b, 10100000b

Db 11111100b,01100000b

End

-------------------------------------------------- -------------------------------

Different remote control codes are different. If you are using other remote controllers, you can modify several parameters (of course, the key code table is definitely different), that is, the counter value is different, but some remote control machine code (machine code) Each button is the same), you can skip the acquisition of machine code.

Beverage cooler

Beverage Refrigerator,Drink Refrigerator,Cold Drink Refrigerator,Commercial Beverage Refrigerator

ZHENGZHOU KAIXUE COLD CHAIN CO., LTD. , https://www.supersnowfreezer.com