Single-chip microcomputer short-distance wireless communication anti-interference simple decoding method
Technical field
The present invention relates to a kind of short-distance wireless communication anti-interference simple encoding method for single-chip, be particularly useful for the less demanding occasion of communications device functions.
Background technology
The wireless serial communication of single-chip microcomputer has a wide range of applications in productive life and industrial control field with its flexibility and low cost.Usually adopt the serial i/O mouth of single-chip microcomputer to finish data communication, the conventional method that serial ports detects data is from data mid portion sampling three times.Have only when occurring twice identical value in three sampled values at least, data just are identified.Yet the antijamming capability of this Data Detection method is very poor.Because the existence of various electromagnetic interference signals in the space, distortion had very likely taken place at sampled point in data that receiver receives, thereby increased the possibility of single-chip microcomputer erroneous judgement data.
Summary of the invention
Because decaying and distort, data cause the erroneous judgement of single-chip microcomputer in order to eliminate to data, the invention provides a kind of novel communication decoding method, can effectively overcome the shortcoming of microcomputer series interface communication poor anti jamming capability, guarantee the validity and the reliability of single-chip microcomputer RFDC.
According to technical scheme provided by the invention, a kind of single-chip microcomputer short-distance wireless communication anti-interference simple decoding method comprises data-encoding scheme and data decoding method:
Described data-encoding scheme, the square wave that equates with the high-low level duration is represented the start bit of Frame; Frame identical by position and last of data bit to be sent; Short with high level lasting time, the long square wave of low duration is represented data bit ' 1 '; Long with high level lasting time, the short square wave of low duration is represented data bit ' 0 '; The transmitting terminal single-chip microcomputer is represented Frame to be sent by software program with square wave;
Described data decoding method, the receiving terminal single-chip microcomputer is set a fixed sampling time at interval by software program, write down the high and low level number of times that samples in each data bit period, according to the high and low level occurrence number that samples judge the data bit that receives be ' 0 ', ' 1 ' or the start bit signal.
Described data bit period is for sending the required time of each data bit, and described data bit period is divided by sampling time interval, and promptly the sampling number of each data bit is between 100 to 400.For example, data bit period 906us=9.06 * 10
-3S, sampling period 3.6us=3.6 * 10
-5S, then sampling number is 251 times or 252 times.
By adjusting described data bit period, adjust message transmission rate.
The present invention has overcome when utilizing serial ports to carry out data communication, each data bit is only sampled three times and is prone to the shortcoming of misjudgement.Because it be not by simple sampling three times with regard to judge the data bit that receives be ' 0 ', ' 1 ' or the start bit, but to sample hundreds of times, make data declare wrong possibility with regard to having avoided greatly because of scurrying into interference signal in the data transmission procedure like this.
The invention has the beneficial effects as follows, utilize simple data-encoding scheme to realize the anti-interference of single-chip microcomputer short-distance wireless communication.With respect to other coding method, memory space shared after the design programmes is minimum, and for single-chip microcomputer has been saved a large amount of memory spaces, this was very important with regard to little single-chip microcomputer for memory capacity originally.
Description of drawings
Fig. 1 is a data bit method for expressing of the present invention.
Fig. 2 is the program flow diagram of transmitting terminal of the present invention.
Fig. 3 is the program flow diagram of receiving terminal of the present invention.
Embodiment
Among Fig. 1: 1. the method for expressing of start bit, the 2. method for expressing of data bit ' 1 ', the 3. method for expressing of data bit ' 0 '.
The present invention is further described below in conjunction with accompanying drawing and specific implementation process.
Adopt the AT89c2051 single-chip microcomputer with transmitting terminal, it is that example illustrates design philosophy of the present invention that receiving terminal adopts PIC16F73.If adopt other model single-chip microcomputer, method is similar.
For example data frame content is (form of Frame can be other type, but the representation of each data bit is similar):
Start bit (1)+data bit (8)+by position (1)
As shown in Figure 1, the present invention represents start bit (1) with equal square wave of high-low level duration; Short with high level lasting time, the long square wave of low duration is represented data bit ' 1 ' (2); Long with high level lasting time, the short square wave of low duration is represented data bit ' 0 ' (3); Determining according to last position of the 8 bit data positions that send by the position, that is to say, if last position of data bit is ' 1 ', also is 1 by the position then, and last position of data bit is ' 0 ', is ' 0 ' just by the position then.
At receiving terminal, software program makes the single-chip microcomputer of receiving terminal sample by turn to the data that receive with regular time interval (as 3.6us), and write down detected high-low level number of times in each data bit period (906us), determine that by the occurrence number of differentiating high-low level the data bit of reception is ' 0 ' at last, ' 1 ' or the start bit.Time in sampling interval and data bit period can change, thereby reach the purpose of adjusting message transmission rate.By sampling hundreds of times, avoided greatly making data declare wrong possibility because of scurrying into interference signal in the data transmission procedure.
As shown in Figure 2: transmitting terminal at first sends the start bit, represents the beginning of a frame information.Simultaneously to some registers that need use, flag bit initialize.The transmission of data is to adopt the ring shift right of bringing the position into to realize, as long as read the value of carry flag bit like this, just can read 8 bit data successively from the low level to a high position, has made things convenient for reading of data bit, is convenient to the operation of back.The data bit figure place that sends deposits register R6 in advance in, operates the transmit operation that realizes 8 bit data positions by carrying out the ring shift right of bringing the position for 8 times into.Deciding by the position by last position of differentiating the transmission data at last is ' 0 ' or ' 1 ', and it is sent, and indicates the end of frame data.
As shown in Figure 3: by the predefined sampling time, and duration of high-low level in data bit and the start bit, data bit ' 0 ' under the good normal condition of calculated in advance, ' 1 ' and the start bit high and low level number of times that should occur and give its certain nargin.Prevent from data to be judged by accident because of interference makes the receiving terminal single-chip microcomputer.The same ring shift right of bringing the position into that utilizes of receiving terminal is realized the reception of data is operated.To the data that receive, detect by turn at interval with regular time, and write down detected high-low level number of times in each data bit period, it is stored in respectively among variable i and the j.By judging that the value among i and the j is the determined zone of high and low level occurrence number that falls into the start bit, still fall into the determined zone of high and low level occurrence number of data bit ' 1 ', or the determined zone of high and low level occurrence number of data bit ' 0 ' and determine the data bit that receives be ' 0 ', ' 1 ' or the start bit signal.If commencing signal is then to counting unit num (data bits of reception) initialize 8, and begin to carry out the ring shift right operation of bringing the position for 8 times into, realize storage operation data bit.Before carrying out ring shift right, need utilize the same method that data bit is detected at every turn and differentiate, differentiate the result by flag bit STATUS is operated realization.Flag bit STSTUS is the 0th STATUS<0 of STATUS register in the PIC single-chip microcomputer 〉: carry and borrow sign.The value that it is controlling the carry flag bit of ring shift right operation is ' 0 ' or ' 1 '.