Do you have a TV remote control or an air conditioner remote control at home? Do you also want to use it to control other appliances or even your computer? Well, let's make this "infrared remote control decoder" together with me.
This small production requires very few components: a TA89C2051 microcontroller, a MAX232CPE RS232 interface level and TTL level conversion chip, an infrared receiver, a crystal oscillator 11.0592MHz, 4 electrolytic capacitors 10uF, a 10uF, a 1K resistor, a 300 ohm resistor, 2 ceramic capacitors 30P, and 8 light-emitting diodes. The price is less than 20 yuan.
Circuit principle introduction:
The main control unit is the single-chip microcomputer AT89C2051, the interrupt port INT0 is connected to the infrared receiving tube U1 to receive the pulse of the infrared signal, and the 8 light-emitting diodes are used as display decoding output (it can also be used to expand other control circuits), U3 is the level conversion chip when connected to the computer serial port RS232, and the 9th and 10th pins are connected to the 1st and 2nd pins of the single-chip microcomputer respectively (1st pin is serial receiving, 2nd pin is serial sending), and the 7th and 8th pins of the MAX232CPE are connected to the 2nd (receiving) pin and 3rd (sending pin) of the computer serial port respectively. The crystal oscillator uses 11.0592MHz, so that the baud rate of the communication can reach 9600b/s. The general default value of the computer is 9600b/s, 8 data bits, 1 stop bit, and no parity bit. The circuit is so simple, now let's analyze the specific programming process.
As shown in the figure, the waveform of the panasonic remote control is like this (the result of repeated tests).
The start bit is a 3.6ms low level followed by a 3.6ms high level. The data representation is a 0.9ms low level, 0.9ms high level, and a period of 1.8ms for "0", and a 0.9ms low level, 2.4ms high level, and a period of 3.3ms for "1". When writing a program, a high level greater than 3.4ms and less than 3.8ms is used as the start bit, a high level greater than 2.2ms and less than 2.7ms is used to represent "1", and a high level greater than 0.84ms and less than 1.11ms is used to represent "0". Therefore, we mainly use the microcontroller to measure the length of the high level to determine whether it is "1" or "0". The working mode of timer 0 is set to mode 1: mov tmod, #09h. Setting timer 0 in this way means setting GATE to 1, a 16-bit counter, and a maximum count value of 2 to the 16th power of machine cycles. This mode is controlled by the external interrupt INT0, that is, the counter is allowed to count only when INT0 is high. For example:
jnb p3.2, $
jb p3.2, $
clr tr0
these three instructions can measure a high level, and then read the count value TH0, TL0 to distinguish whether it is the start bit or "1" or "0". Before determining the code table, you can use the 8 light-emitting diodes of the P0 port to display the code. 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 two consecutive codes displayed by P0, record the code of each key 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 remote control code, it will be compared with the code table to find the matching code item, and the sequence number corresponding to the code item will be output to the P0 port, and the sequence number will also be output to the computer through the serial port. After the computer receives the data, the serial port software will decide how to process it. [page]
The program is not long, here is the complete program and comments: (see the flow chart first)
keydata equ 30h ; //This address and address 31H are used to store the remote control key code.
org 00h
main:
mov keydata,#0 ;// clear
mov tmod ,#09h ;// set timer 0 mode 1, GATE=1
mov r7,#0 ;// counter, used to count whether it is full of 8 bits
mov r6,#0 ;// counter, used to count whether it is full of 2 bytes (decode 16 bits)
jb p3.2,$ ;// is it low level
again: ;// if it is low, continue to execute below
mov tl0,#0 ;// clear TL0
mov th0,#0 ;// clear TH0
setb tr0 ;// start timer 0
jnb p3.2,$ ;// wait for high level to arrive
jb p3.2,$ ;// high level arrives, start counting at this time
clr tr0 ;// high level ends, stop counting
mov a,th0 ;// read th0 value, TL0 is ignored
clr c ;//
subb a,#12 ;//
jc again ;//If th0<12, then turn, which means it is less than 3.4ms, you can calculate this time
mov a,#14 ;//
clr c ;//
subb a,th0 ;//Compare with 14, if TH0>14, it is greater than 3.8ms
jc again ;//Greater than 3.8ms, re-detect
nextbit: ;//The start bit is found, then the next bit
mov tl0,#0 ;//
mov th0,#0 ;//
setb tr0 ;//Start timer
jnb p3.2,$ ;//Wait for high level
jb p3.2,$ ;//High level arrives, then start counting
clr tr0 ;//High level ends, stop counting
mov a,th0 ;//Read the count value, TL0 is ignored
clr c ;//
subb a,#8 ;//Compare th0 with 8
jc next ;;;;//If <2.2ms, then jump and determine whether it is greater than 0.84ms
mov a,#10 ;//Compare with 10 again
clr c ;//
subb a,th0 ;//
jc again ;;;;;;;//If >2.7ms, give up and re-detect
mov a,keydata ;// If it is greater than 2.2ms and less than 2.7ms, it 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 it full of 8 bits
inc r6 ;//Count plus 1
cjne r6,#2,last8 ;//Is it full of two bytes
sjmp seach ;//If it is less than two bytes, collect last8 again
: ;//Full of 1 byte, then the next byte
mov keydata+1,a ;//Save the first byte of encoded data to 31h
mov r7,#0 ;//Clear counter R7
sjmp nextbit ;//Continue collecting data
next: ;//Go here when less than 2.2ms
mov a,th0 ;//Read count value TH0
swap a ;//Swap the upper 4 bits with the lower 4 bits
mov r1,a ;//Save to R1
anl tl0,#0f0h ;//Take the upper 4 bits of TL0 and ignore the lower 4 bits
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 ; //Judge whether it is <0.84ms, if yes, give up and continue sampling
mov a,r1 ;//No
clr c ;//
cjne a,#64,continue ;//Compare with 64
continue: ;//
jnc nextbit ; //a>64 means the sampling value is >1.11ms give up
mov a,keydata ;//Otherwise, the matching bit is "0"
clr c ;//C = 0
rrc a ;//shift zero right into A
mov keydata,a ;//save
inc r7 ;//counter plus 1
cjne r7,#8,nextbit ;//is it 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 ;//save the first byte to 31h
mov r7,#0 ;//clear R7
sjmp nextbit ;//
seach: ;//match key code
mov r0,#-2 ;//key code byte number counter
mov r1,#-1 ;//key sequence counter
seach1: ;//
inc r0 ;//
seach2: ;//
inc r0 ;//
inc r1 ;//
cjne r1,#29,compare ;//Is 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 ;//get code
cjne a,keydata,seach1 ;//compare
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 ;//compare
cjne a,keydata+1,seach2 ;//match or not, if not match, continue to compare with the next byte
mov p1,r1 ;//if match, 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;//The above settings set the serial port baud rate coefficient to: 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,$ ;//Wait to see if the transmission is completed
clr ti ;//Sending is completed, clear TI
exit0: ;//
ljmp main ;//Loop
keycode: ;//Each two bytes represent the encoding of a key
db 11111000b,00000000b, 11111100b,00000000b, 11111001b,11000000b
db 11111100b,11000000b, 11111010b,0000000b, 11111010b,00100000b
db 11111010b,01000000b, 11111010b,01100000b, 11111010b,10000000b
db 11111010b,10100000b, 11111010b,11000000b, 11111010b,11100000b
db 11111011b,00000000b, 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
---------------------------------------------------------------------------------
Various remote controllers have different encodings. If you use other remote controllers, you can just modify a few parameters (of course the button encoding table must be different), that is, the value of the counter is different. However, some remote controllers have machine codes (the machine codes are the same for each button). In this case, you can skip the collection of the machine code. Finally, I want to mention that it is troublesome to not know the remote control code at the beginning. I downloaded a sound card oscilloscope from the "Shuanglong Electronics" website. With it, I can see the waveform of the remote control and measure its pulse width at a glance. It is indeed much more convenient. The software can be downloaded and used for free at the Shuanglong company website http://www.sl.com.cn/. If you want to control the computer with a remote control, you can write a serial port application software yourself, or you can directly download and use the Girder software, which is specially made for serial port remote control and is very easy to use. The download address is: http://www.girder.nl/. You can use this software to define the remote control to remotely control your computer to move the mouse, keyboard operation, browse the Internet, open the player, shut down, etc.
Previous article:Infrared remote control decoding program written in C language
Next article:Dual-machine communication (C language, master and slave shared program)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Line regulation and load regulation
- Some cases of DSP program crash (flying away) - hardware reasons
- [Social Recruitment] [Campus Recruitment] China Electronics Technology Group Corporation Recruitment for Embedded Software Development
- EEWORLD University Hall----Live Replay: ON Semiconductor and Avnet IoT Innovation Design Competition Award Ceremony
- 【NXP Rapid IoT Review】+ Bluetooth (2 Use the computer's Bluetooth connection)
- Based on STM32F0+L6474 stepper driver open source sharing: source code + schematic + BOM table, etc. are all open source
- The product uses two dry batteries with a voltage of 3.2V. In order to prevent the batteries from being connected in reverse, MOS is used. From the simulation results, only...
- C language standard and usage, and standard library introduction
- CircuitPython now supports STM32
- Use two ultrasonic modules to compare the signal distance from another ultrasonic module