[Menu]>[Circuits Gallery]>[Remote Controller]

Processing explanation of Receiver

Environment setting Like the software of the transmitter, various environment setting is done at the line number 008-011 of the list.
    008
    list
| It is defining the name of PIC to use.
    009
    include
| The file (p16f84a.inc) that a various standard label is defined is taken in.
    010
    __config
| The contents of Configuration Word are designated.
| _hs_osc |  | Type of the oscillator | : HS
_wdt_off | Watch-dog timer | : It isn't used.
_pwrte_on | Power up timer | : It is used.
_cp_off | Code protector | : It isn't used.
The value of Configuration Word by above-mentioned specification is H'3FF2'.
    011
    errorlevel
| It makes not show bank switching warning message [302]. At the RAM file register of PIC16F84A, a bank method is adopted. For example, both memory addresses of TMR0 and OPTION_REG is 01. However, TMR0 is at bank 0 and OPTION_REG is at bank 1. In the definition file which is read by INCLUDE, it defines the address of OPTION_REG as being H'0081'. Bank information is written. The bank must be designated with RP0 bit of the STATUS register by the actual processing. In case of MPLAB, to prevent mistake in the bank processing, warning message "Message[302]" is shown in the processing of the register of bank 1. "Register in operand not in bank 0. Ensure that bank bits are correct." When specifying a bank normally, this message is shown. To make not display this message, there is a way of changing a definition file designation. That is, the message becomes not displayed when changing the address of OPTION_REG into H'0001'. Or, there is a way of stopping the display of 302 messages using the ERRORLEVEL directive to be using this time.
Label definition
    ;****************  Label Definition  ********************
CBLOCK and ENDC are used for the block definition of the RAM register file. The area of the label among these commands is automatically set in the order from the address which is specified by CBLOCK. ptn1 and ptn2 are the label which specifies control code data. The value to specify here must be the same as the control code of the transmitter.
RX_STATUS | Receiving status management area 0 : Preamble code receiving status 1 : Control code receiving status 2 : Ending code receiving status
RX_SUBSTATUS | Receiving status management area In each above-mentioned status, a status every receiving bit is managed.
RX_EDGE | Electric wave rising edge detection request flag When detecting an abnormal condition when receiving a control code from the transmitter, this flag ONs. When detecting flag ON, the rising edge detection of the electric wave becomes done.
LAST_LOOK | Last Look flag For the rising edge detection of the electric wave, the detection of the electric wave is needed from the condition which doesn't have an electric wave. The condition that an electric wave isn't received when checking the input port (RA1) which shows the condition of the electric wave is memorized in this Last Look area. The rising edge of the electric wave can be known if comparing with the Last Look area when receiving an electric wave at the RA1 port.
CODE_CK | Code check flag For the operating time of the output relay, the 10-millisecond timer is making do always operation. Because it checks a receiving code using this timer, it controls whether or not to process a code in the checking by this flag. When receiving an electric wave, a flag is made ON.
CONT_DATA | Control data saving area A control data is received every bit. Every time it receives a bit, data is saved in this area.
RELAY | Relay operating time count area The operation of the output relay makes hold for about 500 milliseconds. When making do the operation of the relay, 50 is set in this area. This value is subtracted with the 10-millisecond timer and stops the operation of the relay when becoming 0.
W_SAVE | W register saving area When the timer interruption occurred, the content of the W register to be using by the processing which was interrupted is saved in this area.
S_SAVE | STATUS register saving area When the timer interruption occurred, the content of the STATUS register to be using by the processing which was interrupted is saved in this area.
Program start
    ;**************** Program Start ***********************
The program memory address 0 is the program starting address when turning on or reset occurs. Also, the program memory address 4 is a start address when interruption occurs. These addresses are decided with the hardware of PIC16F series. Initialization process
    ;****************  Initial Process  *********************
Port initialization First, the initialization of the port is done. RA0 to RA4 are set to the input mode and the RB0 to RB7 are set to the output mode. In the relation of the wiring, RA0 and RA3 are grounded. So, RA0 and RA3 must be always made an input mode. Timer setting This circuit controls the data to transmit in the 10-millisecond interval. Timer 0 is used to make 10-millisecond time. TOCS and PSA of OPTION_REG are set to "0" to use an internal clock and prescaler for timer 0. It makes prescaler value "101"(1:64). Because it is using 4 MHz for the oscillator of PIC, the clock is one microsecond. TMR0 is increased every 64 microseconds when combining with prescaler. The timer interrupt generates TMR0 when count value (TMR0) becomes 0 from 255(H'FF'). So, the value to set to TMR0 for the 10-millisecond timer is 256-(10000/64)=100. It uses the prescaler value which is the same as the transmitter to make a timer error little. Initialization of the register When turning on the power of PIC, the initialization for various register is executed with hardware. I am doing initialization to make those original values certainty.
PROTB | = 0 | It stops the operation of the output relay.
RX_STATUS RX_SUBSTATUS | = 0 = 0 | It does the initialization of the receiving status management area.
RX_EDGE | = 0 | It clears an electric wave rising edge detection request flag.
LAST_LOOK | = 1 | It does the initialization of the Last Look to electric wave receiving(ON).
CODE_CK | = 0 | It makes the condition which doesn't check a control code.
CODE_DATA | = 0 | It does the initialization of the control data save area.
RELAY | = 0 | It clears a relay operation counter.
10 milliseconds timer setting It starts a 10-millisecond timer to manage the operating time of the relay. Receiving preprocess
    ;*********** Initial Input check Process **************
The receiving state of the electric wave is checked by the condition of the RA1 port. When not receiving an electric wave, Last Look flag (LAST_LOOK) is made OFF. You have the possibility to seem, that this processing is unnecessary. To detect the rising edge timing of the electric wave, it is necessary to confirm that there is not an electric wave. Also, it is possible to prevent the wrong operation which is due to the continuously electric wave with this Last Look data. The time is the rising edge of the electric wave when an electric wave is detected in the last Look flag is OFF. The pulse from the transmitter switches over in the 10-millisecond interval. So, the signal detection timing at the receiver is shifted for 5 milliseconds from therising edge of the electric wave. These 5 milliseconds are only first. The signal detection timing after that is a 10-millisecond interval. A 10-millimeter timer is set by the initialization processing. When detecting the rising edge of the electric wave, this timer is again set to 5 milliseconds compulsorily. Therefore, to prevent timer interruption's occurring during this timer setting processing of 5 milliseconds, interruption disable processing is done.(INTCON.GIE=0) After it, it makes code check flag (CODE_CK) ON and it makes execute code check processing to timer process. Because it detected an electric wave, it makes a Last Look flag ON. An interruption is made enable after setting a 5-millisecond timer to TRM0. After this, it becomes the processing to wait for the timer interruption. In the processing of interruption's wait, the checking of the electric wave rising edge detection request flag is done. When to be illegal is detected by the detection processing of a control code, this flag is made ON. When detecting ON of this flag during interruption's wait processing, after clearing this flag, it jumps to the electric wave rising edge detection processing and the receiving operation returns to the initial state. Timer interrupt process
    ;************  Timer Interruption Process  **************
Saving registers In the timer interruption processing, the saving of W register and STATUS register is done first. When not doing this processing, the contents of the register before the interruption are changed by the interruption processing and the processing after interruption's ending becomes impossible. This time, it is to be OK even if W register isn't saved, because W register isn't used in the processing before the interruption. I saved them for the safety. When sharing the other register, they must be saved too. Memory bank specification is set for the safety too. Clearing of a timer interruption flag When the interruption occurs, the flag which shows an interruption factor is set. In case of timer 0, the INTCON.T0IF bit is set. This bit must be cleared by the interruption processing. When not clearing, the interruption factor doesn't go out and also the same interruption has occurred. Checking of an output relay counter fter making do the operation of the output relay, it is made to continue operation for about 500 milliseconds. The value of relay operation counter (RELAY) is subtracted to watch over the time. When the counter value is 0 already, counter subtraction processing isn't done. The counter is not 0 and when becoming 0 with the subtracted result, the operation of the relay is stopped. Checking of receiving status Checking processing is decided by the contents of RX_STATUS. In case of 0, a preamble code is checked. In case of 1, a control code is checked. In case of 2, an ending code is checked.
    ;*********** Preamble data check Process *************
Checking of a preamble code The preamble code is 101010110. At design first, I assumed that the ending of the preamble was judged at 11 after 10 continuously was detected. However, finally, I changed to be judged normally all preamble code strings. It is because the control code can have 10110 bit strings too. For example, it becomes the same bit string when receiving from the position of B0 in case of B0=1, B1=1. However, because the ahead of B0 is always 1, the rising edge detection of the electric wave isn't done and wrong operation like the above isn't done. However, I decided to check all bit strings because I had changed processing. Receiving status management of each bit is done by RX_SUBSTATUS. It makes RX_STATUS "1" when receiving a normally preamble code and it enters control code receiving. At this time, it clears control data saving area (CODE_DATA) and it erases the control code which was received before. It jumps to illegal process when detecting abnormally in the bit string.
    ;************ Control data check Process **************
Checking of a control code The configuration of the control code is 1x01x01x01x01x01x01x01x0. x is the bit of the control code. There are 8 bits at all. It checks this bit string in the order and the part of x is saved into control data saving area (CODE_DATA). It makes RX_STATUS "2" when receiving a normally control code and it enters ending code receiving. It jumps to illegal process when detecting abnormally in the bit string.
    ;************** End data check Process ****************
Checking of an ending code The ending code is 111. It judges a normally ending code when 1 is continuously received three times when RX_STATUS is in the condition of "2" and it enters the checking processing of a control data. It jumps to illegal process when detecting abnormally in the bit string.
    ;**************** Data check Process ******************
Checking of a control data As for the control data checking processing, it confirms whether the received data matches the data of pattern 1 or pattern 2. These patterns must be made the same as the pattern of the transmitter. When matching pattern 1, make do the operation of relay 1 and it makes relay 2 non-operation. When matching pattern 2, make do the non-operation of relay 1 and it makes relay 2 operation. Then, after making do the operation of either relay, 50 is set in relay operating time count area (RELAY) for the 500-millisecond holding time. This value is subtracted every 10 milliseconds and the operation of the relay stops 500 milliseconds later. It enters illegal processing in case of control data's matching neither patterns or driving a relay normally. It is to make set receiving timing again every time it receives a code normally that it enters illegal processing in case of normally. Illegal process
    ;*****************  Illegal Process  ********************
This illegal processing is executed when the code to receive is not a prescribed string or all data is normally received. In the illegal processing, the following processing is done. It sets the electric wave rising edge detection request flag (RX_EDGE) and the rising edge detection of the electric wave is done by this. It clears the receiving status management data (RX_STATUS,RX_SUBSTATUS) and it makes the condition to check a preamble code. It clears code check flag (CODE_CK) and it makes control code checking processing in timer processing of 10 millimeters skip. After that, it enters interruption's ending processing. Substatus increment process
    ;***********  Substatus Increment Process  **************
Substatus is used to manage the position of the detection bit in each receiving condition (the preamble, the control code, the ending code). Every time it receives by 1 bit, it increases substatus. After that, it enters interruption's ending processing. Interruption's ending process
    ;********  End of Timer Interruption Process  ***********
Timer interruption's ending is done by this processing. First, it sets the timer value of 10 milliseconds to TMR0 because of the timer interruption next time. It restores W register and STATUS register after it. It restores STATUS register first. This is to use W register to restore STATUS register. The contents of the W register have changed with the restoration value when restoring W register first. It restores W register last. However, the contents of the STATUS register have changed when using MOVF instruction for the restoration of the W register. Therefore, it restores W register using the SWAPF instruction which the contents of the STATUS register don't change. The interruption processing ends last in the RETFIE instruction. With this instruction, it jumps to the interruption return address which was saved in the stack area and moreover "1" is set to the INTCON.GIE bit and the interruption becomes an enable condition.