Infrared receiving method and device
Technical Field
The invention relates to the technical field of infrared remote control, in particular to an infrared receiving method and device.
Background
The invention patent No. 201210436867.2 discloses a method for decoding infrared remote control by an input capture interrupt mode, which comprises the following steps:
s1: configuring an input capture timer in a main program of the single chip microcomputer, starting the input capture timer, and switching on interruption;
s2: and interrupting to trigger acquisition of a capture time value.
S3: and comparing with the last capture time value, and calculating two intervals, namely the pulse width.
S4: the state machine is in idle state, at this time, whether the comparison pulse width is in accordance with the front end of the guide code is judged, if yes, S5 is carried out, otherwise, S8 is carried out, and the operation is exited.
S5: confirm whether it is a dock, enter the decoding state if yes, otherwise go to S4.
S6: the state machine is in a decoding state, and whether the comparison pulse width is consistent with: if the coding rule of the data 0 or the data 1 or the data F is positive, the coding value is shifted into the coding variable to be stored, the counting value of the coding bit number is added with 1 until the bit number accords with the preset coding bit number, the decoding is completed, the state machine jumps to an idle state, namely S4, otherwise, the state machine is considered to be a clutter and is discarded, and the state machine shifts to S7.
S7: the captured value is stored in a variable for use in the next comparison.
S8: clearing the flag bit, and turning over the edge trigger mode to prepare for the next capture.
The method for decoding the infrared remote control by the input capturing interrupt mode uses a timer which has an input capturing function, automatically records the time when the edge jump occurs when the signal changes and jumps, triggers the interrupt, processes the time value of two continuous times only in the interrupt processing program, calculates the pulse width of the signal and then decodes the signal.
However, in the specific process of infrared reception, because of the priority conflict of multiple interrupts inside the MCU, the board-level MCU (or the single chip microcomputer) adds one more or one less overflow interrupt to the timer, thereby causing code loss of one frame of data and further causing decoding errors.
Disclosure of Invention
The technical problem to be solved by the present invention is to provide an infrared receiving method and apparatus capable of avoiding decoding errors.
In order to solve the technical problems, the invention adopts the following technical scheme:
an infrared receiving method, comprising the steps of: the single chip is provided with a timer, and the timer overflow interruption and the capture interruption are started; acquiring an overflow number value and a capture time value when capturing interruption triggers; calculating a capturing time difference value according to the current capturing time value, the last capturing time value and the overflow number value; if the capture time difference satisfies the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bAnd correcting and outputting a capture time difference value according to a preset rule, wherein △ t represents the capture time difference value, a represents a preset constant, K represents an allowable error value, K is more than 0, b represents the bit width of the timer, and decoding the infrared pulse sequence according to the capture time difference value.
Preferably, the correcting and outputting the capturing time difference value according to the preset rule further comprises the steps of: if the capture time difference satisfies the condition a-k-2b<△t<a+k-2bAccording to the formula△t'=△t+2bCorrecting and outputting the capturing time difference value, wherein △ t' represents the corrected capturing time difference value, and if the capturing time difference value meets the condition a-k +2b<△t<a+k+2bAccording to the formula △ t' △ t-2bAnd correcting and outputting the capture time difference.
Preferably, decoding the ir pulse sequence according to the capture time difference value further comprises the steps of: when the value of a is 13500, and the capture time difference value meets the condition of a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, decoding the infrared pulse sequence into a guide code according to the capture time difference; when a is 2250, the capture time difference satisfies the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, decoding the infrared pulse sequence into logic '1' according to the capture time difference; when the value of a is 1120, the capture time difference value meets the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, the infrared pulse sequence is decoded to logic "0" according to the capture time difference.
Preferably, before decoding the infrared pulse sequence according to the capturing time difference value, the method further comprises the step of judging whether the capturing time difference value meets the condition that a-k is less than △ t is less than a + k, and if yes, directly outputting the capturing time difference value.
Preferably, before decoding the ir pulse sequence according to the capture time difference value, the method further comprises the steps of: if the capture time difference does not satisfy a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bOr a-k < △ t < a + k, judging that the infrared pulse sequence is invalid, and discarding the infrared pulse sequence.
An infrared receiving apparatus, characterized in that, the infrared receiving apparatus includes: the configuration module is used for configuring a timer for the single chip microcomputer and starting timer overflow interruption and capture interruption; the capture module is used for capturing the interruption and acquiring the overflow number value and the capture time value; a calculation module for calculating the current capture time value, the last capture time value and the overflow number valueCalculating a capture time difference; a correction module for correcting the acquisition time difference when the acquisition time difference satisfies a condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bAnd correcting and outputting a capture time difference value according to a preset rule, wherein △ t represents the capture time difference value, a represents a preset constant, K represents an allowable error value, K is more than 0, b represents the bit width of the timer, and a decoding module is used for decoding the infrared pulse sequence according to the capture time difference value.
Preferably, the modification module comprises: a first correction unit for correcting the acquisition time difference value when the acquisition time difference value satisfies a condition a-k-2b<△t<a+k-2bAccording to the formula △ t- △ t +2bCorrecting and outputting a capturing time difference value △ t' representing the corrected capturing time difference value, and a second correcting unit for correcting the capturing time difference value when the capturing time difference value satisfies a condition a-k +2b<△t<a+k+2bAccording to the formula △ t- △ t-2bAnd correcting and outputting the capture time difference.
Preferably, the decoding module is configured to: when the value of a is 13500, and the capture time difference value meets the condition of a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, decoding the infrared pulse sequence into a guide code according to the capture time difference; when a is 2250, the capture time difference satisfies the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, decoding the infrared pulse sequence into logic '1' according to the capture time difference; when the value of a is 1120, the capture time difference value meets the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, the infrared pulse sequence is decoded to logic "0" according to the capture time difference.
Preferably, the infrared receiving device further comprises a judging module for judging whether the capturing time difference satisfies a condition a-k < △ t < a + k and judging whether the capturing time difference satisfies a condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2b。
Preferably, theThe infrared receiving device also comprises: a discarding module for discarding the difference between the acquisition times when the difference does not satisfy a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bOr a-k < △ t < a + k, determining that the infrared pulse sequence is invalid, and discarding the infrared pulse sequence.
The invention has the beneficial technical effects that: according to the infrared receiving method, the capturing time difference value meets the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bAnd then, the infrared pulse sequence is decoded according to the capturing time difference, so that the situation of decoding errors caused by the fact that the capturing time difference is calculated incorrectly is avoided.
Drawings
Fig. 1 is a flow chart of an infrared receiving method of the present invention;
FIG. 2 is a code diagram of a 32-bit binary code set in NEC format;
FIG. 3 is a code diagram of logic "1" and logic "0" in NEC format;
fig. 4 is a block diagram of an infrared receiving device according to the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more clearly understood by those skilled in the art, the present invention is further described with reference to the accompanying drawings and examples.
As shown in fig. 1, in an embodiment of the present invention, the infrared receiving method includes the steps of:
and S10, configuring a timer by the singlechip, and starting timer overflow interruption and capture interruption.
The invention uses a 1us 12 bit wide (4096 ticks) timer to roll count, and the timer triggers an interrupt every time the timer overflows, and the overflow number n is added.
And S20, acquiring an overflow number value and a capture time value when capturing interruption triggers.
The invention adopts external interrupt and timer to receive infrared signal, infrared pulse signal enters into single chip of infrared receiving end through external interrupt port, responds to rising edge of receiving end to trigger capture interrupt, timer rolls to time according to 1us reference time, because the counting range of timer is only 4096(0-4095), so it needs to time by overflow accumulation, and once interrupt is triggered. When capturing interruption triggers, an overflow number value n and a capturing time value T are obtained from a timer.
And S30, calculating the capture time difference value according to the current capture time value, the last capture time value and the overflow number value. The capture time difference is calculated as: Δ T ═ T2-T1+ n 2bWhere Δ T denotes a capture time difference value, T2 denotes a current capture time value, T1 denotes a last capture time value, n denotes an overflow number value, and b denotes a bit width of the timer. The bit width of the timer in the embodiment of the present invention is 12, that is, b is 12; in other embodiments of the present invention, b takes different values according to the bit width of the timer.
S40, judging whether the capture time difference value meets the condition a-K < △ t < a + K, if yes, jumping to the step S80, and if not, executing the step S50, wherein a represents a preset constant, K represents an allowable error value, and K is greater than 0.
S50, judging whether the capture time difference value meets the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bIf yes, go to step S60; if not, go to step S70.
And S60, correcting the capture time difference.
When the capture time difference satisfies the condition a-k-2b<△t<a+k-2bWhen the time is over, the timer adds one overflow time, namely the capture time difference is reduced by 2bTherefore, it is necessary to add 2bThe correct capture time difference can be obtained according to the formula △ t' △ t +2bCorrecting and outputting the capturing time difference value, wherein △ t' represents the corrected capturing time difference value, and when the capturing time difference value meets the condition a-k +2b<△t<a+k+2bWhen the time is over, the timer adds one more overflow time, namely the capture time difference is 2 morebTherefore, it is necessary to subtract 2bCan get correct captureThe difference value is obtained according to the formula △ t' △ t-2bAnd correcting and outputting the capture time difference.
And S70, judging that the infrared pulse sequence is invalid, and discarding the infrared pulse sequence.
And S80, decoding the infrared pulse sequence according to the capture time difference value.
In the infrared transceiving process, the data format of infrared transmission is fixed, as shown in fig. 2 and fig. 3, the pulse signal in NEC format adopts a pulse width modulated serial code, and the period of the pilot code is 13.5 ms; the period of logic "1" is 2.25 ms; the period of logic "0" is 1.12 ms. The decoding process of the infrared pulse sequence is to judge whether the capture time difference is matched with the pulse width of the guide code, logic '1' or logic '0'. a is a preset constant, and the value of a is related to the pulse width of the bootstrap code, logic '1' or logic '0'.
When the value of a is 13500, and the capture time difference value meets the condition of a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, decoding the infrared pulse sequence into a guide code according to the capture time difference;
when a is 2250, the capture time difference satisfies the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, decoding the infrared pulse sequence into logic '1' according to the capture time difference;
when the value of a is 1120, the capture time difference value meets the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bThen, the infrared pulse sequence is decoded to logic "0" according to the capture time difference.
As shown in fig. 4, an embodiment of the present invention further provides an infrared receiving apparatus, including:
and the configuration module 10 is used for configuring a timer for the singlechip and starting timer overflow interruption and capture interruption.
And the capturing module 20 is used for capturing the interrupt, and acquiring the overflow number value and the capturing time value.
And the calculating module 30 is configured to calculate a capture time difference according to the current capture time value, the last capture time value, and the overflow number value.
A judging module 40 for judging whether the capturing time difference satisfies the condition a-k < △ t < a + k and whether the capturing time difference satisfies the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2b。
A correction module 50 for determining whether the capture time difference satisfies a condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bAnd correcting and outputting the capture time difference according to a preset rule, wherein △ t represents the capture time difference, a represents a preset constant, K represents an allowable error value, K is greater than 0, and b represents the bit width of the timer.
The correction module comprises a first correction unit and a second correction unit. The correction unit is used for satisfying the condition a-k-2 when the capture time difference valueb<△t<a+k-2bAccording to the formula △ t- △ t +2bA correction unit for correcting and outputting a capturing time difference value △ t' representing the corrected capturing time difference value, and a second correction unit for correcting the capturing time difference value when the capturing time difference value satisfies a condition a-k +2b<△t<a+k+2bAccording to the formula △ t- △ t-2bAnd correcting and outputting the capture time difference.
And the decoding module 60 is configured to decode the infrared pulse sequence according to the capture time difference value.
When the value of a is 13500, and the capture time difference value meets the condition of a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bAnd the decoding module is used for decoding the infrared pulse sequence into the guide code according to the capture time difference value.
When a is 2250, the capture time difference satisfies the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bAnd the decoding module is used for decoding the infrared pulse sequence into logic '1' according to the capture time difference.
When the value of a is 1120, the capture time difference value meets the condition a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bAnd the decoding module is used for decoding the infrared pulse sequence into logic '0' according to the capture time difference value.
A discarding module for discarding the difference between the acquisition times when the difference does not satisfy a-k-2b<△t<a+k-2bOr a-k +2b<△t<a+k+2bOr a-k < △ t < a + k, determining that the infrared pulse sequence is invalid, and discarding the infrared pulse sequence.
The foregoing is considered as illustrative of the preferred embodiments of the invention and is not to be construed as limiting the invention in any way. Various equivalent changes and modifications can be made by those skilled in the art based on the above embodiments, and all equivalent changes and modifications within the scope of the claims should fall within the protection scope of the present invention.