Lesson 5. Working with IR remote control

Hello everyone.

Our new lesson is dedicated to processing IR remote control signals. To do this, we will use a very interesting timer function – capture.

With this timer function, it is possible to measure the signal duration at the input of the microcontroller. It is often used to measure the PWM signal, but we will use it to decode data from the IR remote control.

First, let’s figure out what this data is.

There are a huge number of IR remote controls and a huge number of these remote controls are used by NEC Protocol. 

In our example, we will use these remote controls. For now, we’ll mark them as “remote control 1” 

and “remote control 2” 

We’ll need it. Then you will understand why.

And we’ll need an Arduino IR receiver like this.

We think you might already have both.

The peculiarity of the NEC Protocol is that it carries 32 bits of data. 

https://www.snrelectronicsblog.com/wp-content/uploads/2016/01/NEC-Protocol.png

First is the start bit indicating the beginning of the transfer. Then the first 8 bits are the address of the receiver (TV/DVD/CD, etc.), the next 8 bits are the inverted value of the address field, then the following 8 bits are the command and the last 8 bits are the inverse of the command value. The value of each bit is read from the beginning of the pulse to the end of the pause between the pulses. Inverted values allow us to check whether the received data is correct.

Keep in mind that in reality, the signal from the IR receiver will look inversely. Here’s what it looks like on an oscilloscope. 

Let’s take a closer look at the signal. 

Here you can clearly see the shape of the starting bit and the shape of bits “0” and “1”. Note that the duration of the start bit without pause is just over 9 milliseconds. This is the fault of the internal circuit of the IR receiver. In the documentation, you can find references to this. 

The NEC Protocol provides for small jitter in the pulse duration. So we need to take this into account when detecting it.

Let’s put together a diagram like this. 

On the eight lower LEDs, we will display a scale that will be controlled by the “Volume+” and “Volume -” buttons.

On the top four LEDs, we will indicate the number of the “channel“. We will control these LEDS with the buttons ” Channel+” and “Channel -” and digital buttons from “0”to ” 9″.

This is the stand we got.

Let’s move on to the program.

First, we will determine the type of our remote control. In this example, “remote control 1” is selected.

Then we will declare the duration of the pulses and pauses, which we will use to detect the data.

Finally, the codes for the IR remote control buttons. The codes are 16-bit. The upper part of the code contains the address of the IR receiver, and the lower part contains the button code. We will not fully comply with the bit order in full compliance with the Protocol standard because this will not affect the operation.

We need to set the timer to work in capture mode. Let’s do it like this.

Let’s explain it in order. 

Capture mode on both edges of the pulse, CCIxA capture input, the synchronous capture mode, the capture mode, allow interruption by capture event, 500 clock timeout of the reference generator (approximately 12 milliseconds) to determine the error during detection, allow interruption when the timeout is reached, select the reference generator 32768 Hz, continuous timer count, allow interrupts from the timer  and disable the divider for the reference generator.

The timer is set, but how do we use it?

The answer is simple – the interruption!

Let’s create the handler interrupt for the timer and use it to decode data.

Here’s what it will look like.

We suggest that you study the interrupt handler algorithm yourself. Do not forget that it will be executed with each transition from ” 0 ” to ” 1 “and from” 1 “to”0”.

As a result, we will have 3 values in the IRready variable:

IRNONE – data is not accepted

IRDONE – data is accepted

IRREPEAT – repeat data when the button is pressed for a long time

Everything is fine, but we have not yet explained to the MSP430 core which pin is connected to the photodetector and the LEDs.

To do this, we will configure the Input Output ports in the main body of the program and apply the unique mechanism of the MSP430 core – the port mapping.

From this point on, port P2.0 is the capture input for the timer.

There is not much left – start the timer and process the detection results and turn on the desired LEDs.

Let’s do it like this.

When The “Volume+” and “Volume -” buttons are processed with the long press, it forms the repeat of the command. The rest of the buttons ignore the long press.

Let’s build and launch our project.

Congratulations! You have learned how to use an important timer function – the capture, how to measure the duration of the pulses, and detect and apply signals from the IR remote control.