Chapter 1 - Processing complex sensor input
(Deciphering IR protocol)

Consumer Infrared Protocol:

I am pretty sure everyone is already living with the convenience brought along by this little guy, the IR remote controller. Gone are the days when you need to get off the couch to switch the TV channel or adjust the sound volume.
But how does it all work (Magic)? First, the IR remote controller sends a modulated IR light signal to a receiver (TV in our current discussion). This stream of modulated IR light signals is encoded with a control data stream using a known protocol (rules). The appliance then demodulates and decodes the data stream and acts accordingly.

As can be imagined, everyone will start creating their own rules to pulse or modulate this invisible light beam. Unfortunately, this lack of standardization creates a problem many consumers face daily: A huge pile of IR remote controllers or the need to purchase a universal remote.

 wiki: Consumer IR


Infrared Protocol:
There are numerous consumer IR protocols today, with RC5 & NEC being the most popular among generic electronics manufacturers. The format of both these protocols are pretty well documented. And personally, if you must ask, I do not have a preference for either protocol. We are using NEC protocol simply because we manage to get a bunch of generic IR remote controllers (Wholesale) & they happen to ride on the NEC protocol. 

NEC Protocol NEC Protocol

You can also read more about the RC5 protocol in your free time from the following link: 
RC5 Wiki RC5


General description of the NEC Protocol:

The NEC protocol uses pulse distance encoding of the bits. Each pulse is a 560μs long 38kHz carrier burst (26.3μs). Logical bits are transmitted as follows:

Logical '0' – a 562.5μs pulse burst followed by a 562.5μs space, with a total transmit time of 1.125ms
Logical '1' – a 562.5μs pulse burst followed by a 1687.5μs space, with a total transmit time of 2.25ms

When a key is pressed on the remote controller, the message transmitted consists of the following:


  • A 9ms leading pulse burst.
  • A 4.5ms space.
  • The 8-bit address for the receiving device.
  • The 8-bit logical inverse of the address.
  • The 8-bit command.
  • The 8-bit logical inverse of the command.
  • A final 562.5μs pulse burst to signify the end of message transmission.

67.5ms is needed to fully transmit the message frame.
(Not taking into account the 562.5μs pulse burst at the end)

Pins usage on the C3 CoreModule:
To remain as flexible as possible, I have left the connection to the IR sensor as an open DuPont pin header. With a suitable length female-to-female jumper cable, one can easily define the IO pin you wish to use for IR reception.

The instructor will be using IO4 in the example code. 
Connect the DuPont jumper cable from the "IR_Sensor_in" to "IO4" in the pin headers shown on the right.

Crafting your algorithm with Finite State Machine (FSM):

The main challenge when programming reactive (event-driven) systems is correctly identifying the appropriate chunk of code to execute in response to a given event.

This dependence on context often leads to deeply nested if-else or switch-case constructs. Because of that, Most reactive programs start out relatively simple and well structured, but as features are gradually grafted on, more and more flags and variables are introduced into the source code to capture the relevant event history.

"Finite State Machine function by reacting differently based on its history & current inputs."

Shown on the right is FSM diagram to describe the multi-mode LED flasher you did as an assignment in Module A.

Simplifying the NEC IR receiver engine as a FSM:

Using the protocol information & our newly acquired FSM know-how, we can simplify the structure of the IR code receiving engine to the following FSM diagram.



Below is the code, written precisely as the diagram depicted above.


Hardware interrupt: 

 In the code example below, we also utilize interrupts to let the MCU hardware monitor input pins to control the flow of the algorithm whenever an input transition occurs.

An Interrupt's job is to ensure that the processor responds quickly to critical events. When a specific signal is detected, an Interrupt (as the name suggests) interrupts whatever the processor is doing and executes some code designed to respond to whatever external stimulus is fed to the pins of the MCU.
Once that code has wrapped up, the processor goes back to whatever it was initially doing.

 Todo:

  • Figure out the rest of the remote controllers' key codes emitted by the infrared remote controller; we will be using more than one button for the next chapter's assignment.

  • (Advanced) Study the NEC IR code below (Or read up the NEC protocol through the link above), implement/append the repeat code from the NEC transmission to the solution above.

    NEC Repeat Code:
    A command is transmitted only once, even when the key on the remote control remains pressed. Every 108 ms a repeat code is transmitted for as long as the key remains down.
    • A 9ms leading pulse burst.
    • A 2.25ms space.
    • a 562.5µs pulse burst to mark the end of the space.