50 shades of stub * Hardware reception of PWM-coded signals by Microchip microcontrollers


    * POI - Peripheral Independent of the Core in Microchip microcontrollers, also known as CIP - Core Independent Peripheral.

    Part 4


    Previous articles [ 1 ], [ 2 ] and [ 3 ] dealt with the Microchip Microcontroller Independent Kernels (PND) Peripherals: configurable logic cells, input / output ports with current limiting function and ADC with a calculator; some possibilities of such peripherals were shown. Let me remind you that independence is not implied on the type of PIC core of microcontrollers (BaseLine, Mid-Range, Enhanced Mid-Range, PIC18, 16-, 32-bit), but on the operation of the core, i.e. independent execution of tasks assigned to the periphery from the state of the CPU. Such peripherals, and in particular the possibility of configuring it for collaboration and the synthesis of hardware functions, are designed to relieve the software part and reduce power consumption.

    In this small article I want to show examples of the implementation of the reception of “custom”, non-standard communication interfaces using Peripherals Independent of the Core.

    PWM coding of information is quite common, when discrete signals, log.1 and log.0, are encoded with a pulse width. Consider the option of receiving and decoding such signals using Peripherals Independent of the PIC Core Controllers.

    Decoding the PWM signal of the AM2302 sensor


    In DIY projects, a DHT22 (AM2302) temperature and humidity sensor is often used. The sensor has 3 outputs, information is transmitted over one wire. In response to a request (low level with a duration of approximately 1ms), the sensor responds with a start bit, and then with a sequence of 40 bits, where the information is encoded in the pulse duration: log. “0” - impulse 30µs, log. "1" - 70µs (typical values). The response from the sensor contains 5 bytes: 2 bytes of humidity data, 2 bytes of temperature and 1 control byte.


    Fig.1. Explanation of the principle of signal generation sensor DHT22.

    There are many examples of working with such sensors on the Arduino. Some library implementations use constructs like:

    loopCnt = TIMEOUT;
    while(PIN) {
    	if(--loopCount == 0) return ErrorTimeout;
    } 
    if (loopCnt <  cntOne) {
    // bit =1
    …
    } else {
    // bit =0
    …
    }
    

    In such implementations, I see the following problems:

    - the program for the entire measurement time (> 5ms) “hangs” in the measurement code;
    - the occurrence of a sufficiently long interruption will ruin the reading of data from the sensor;
    - potential problems with working at a low clock frequency of the microcontroller;

    The algorithm of the program of such solutions has approximately the form (see Fig. 2)


    . 2. The algorithm of the software receiving and decoding sensor signals.

    Below is a variant of the hardware protocol receiving / decoding with minimal software overhead.

    The idea is to separate the clock pulses from the bitstream, followed by the direction of the original signal and the clock pulses to the hardware SPI module. In this case, the program of the microcontroller can only take in succession 5 bytes of data from the SPI.

    Part of the CIP are timers with the ability to run on events (change the state of the input or other peripherals). Those. changing the state of the input can start a timer, see the signal TMR6 in Fig.3. When the timer reaches the specified value, its counting stops and the timer is in the state TMR6 = PR6 (PR is the period register). The timer state can be an input for a Configurable Logic Cell (CLC, see Part 1).

    Thus, with the help of a timer and Logic Cells, we can form a signal suitable for applying to the clock input SCK of the SPI module. To extract information, the duration of such a clock should have an average value between the duration of zero and one. Then SPI can fix the bit by the decay of the clock (see Figure 3 SCK signal).

    The signal from the sensor will have false pulses generated from the request and start pulses. In order for these impulses not to interfere with work, you need to enable SPI only for the time of information impulses, or cut off unnecessary impulses. This problem can also be solved with the help of CIP.

    Another timer acts as a pulse counter with switching by decay: write the number 2 to the period register, the timer counts the first 2 pulses (see signal TMR4 in Fig. 3) and stops the count (see EN signal in Fig. 3), which the block of logic cells allows the output of the remaining pulses to the SPI input.


    Fig.3. Diagrams explaining the reception of a DHT22 sensor signal.

    The logical function of the SCK signal generation is implemented on a single logical cell (CLC), the complete circuit in the AND-OR configuration is shown in Fig.4.

    The logic cell output is connected to the SCK input, and the DHT22 sensor signal is connected to the MOSI of the SPI module. All connections are made inside the microcontroller (Fig.5). To control and debug signals can be displayed on the ports of the microcontroller.


    Fig.4. CLC logic cell configuration in a PIC microcontroller.


    Fig. 5. Complete configuration structure of the periphery.

    If it seems that the 2 timers on the task of decoding the protocol of the sensor is a waste of resources, then a counter up to two can be “collected” on free logical CLC cells.

    Total, the decoding task is reduced to a very simple algorithm: the microcontroller and its periphery are initialized, if necessary, the SPI module is turned on and a measurement request is generated (log. “0” for ~ 1 ms).

    It remains to read the data from the buffer when an SPI interrupt occurs.


    Fig.6. Algorithm of work with the sensor DHT22 when using the stub.


    Fig.7. The signals from the ports of the microcontroller. SSP1IF signal - byte interrupt by the SPI module.

    Figure 7 shows signal diagrams, where:
    DHT (dat) - signal on the sensor signal line - we feed the SPI module MOSI input;
    TMR6! = RP6 - dedicated clock signal - send to the SPK SCK;
    SSP1IF - interrupt signal (data availability in the SPI buffer) - in fact, this signal shows the load of the microcontroller's core - reading the result data.

    Decoding of other PWM protocols


    Such "single-wire" PWM protocols are used in IR control panels for household appliances. Often, IR transmission uses coding by the position of the pulse, when the duration is constant, and the pause is variable. This option is also called "Variable pause coding". In essence, this is the same PWM coding, but with an inverted signal. If there are logic cells, it is not a problem to make an inversion, besides IR receivers invert the received signal. In fig. 8 shows the signals after the receiver received from two different consoles.



    Fig.8. Coding options IR control panels.

    Both consoles have different protocols, but these protocols are easily decoded using the method described above, the only thing is that it is necessary to ensure that the start of the parcel is detected in order to synchronize the activation of SPI, since the IR receiver can catch interference.



    Fig. 9. SPI decoded signals.

    Not all IR remotes have PWM coding. Some of the protocols, for example RC5, have phase coding (the Manchester code, “0” is transmitted as 10, and “1” as 01) [4]. The decoding of the Manchester code with the help of the periphery of the independent of the core was already considered earlier in the first part [1].

    Results


    Instead of almost 100% CPU utilization of the microcontroller for decoding the PWM protocol in the Arduino variant (yes, I know, the problem can be solved more efficiently using capture modules or other peripherals), we transferred the reception of the information package to the hardware. The front of the input signal starts the timer, the timer state determines the output of the block of logic cells, the output of the logic cell is fed to the SPI.

    The use of peripherals independent of the core allows you to optimize the use of resources, transfer some tasks to hardware, simplify the code, reduce consumption.

    Literature


    1. Configurable Logic Cells in PIC Controllers.
    2. 50 shades of stub. I / O ports
    3. 50 shades of stub. A / D and A / D converters with a Microchip
    4 microcontroller computer . Sbprojects.com/knowledge/ir/rc5.php

    Also popular now: