Recent

Author Topic: Example code for IR component.  (Read 2075 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Example code for IR component.
« on: February 07, 2023, 06:44:10 pm »
Hey, does somebody have freepascal example code for me to read the TSOP31136 IR component ? Greets, PascalByThree


ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: Example code for IR component.
« Reply #1 on: February 08, 2023, 06:19:40 am »
I only have some C code to capture a bit stream from an IR remote (not worth sharing, there are many similar examples on the internet). There are several strategies for reading the pulse stream:
  • use the input capture functionality (ICR) of a timer
  • use a pin interrupt and read the timer when the interrupt fires
  • read the pin state in a loop and log the duration of the high/low levels
This bit stream with timings can be replayed if you simply want to clone the behaviour of the captured signal.  Further protocol identification and decoding is also possible, but involve a bit more work.

If you elaborate on your requirements perhaps more specific help can be given.

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Example code for IR component.
« Reply #2 on: February 13, 2023, 02:29:32 pm »
The arduino IDE seems to have plenty of options.


Does anybody got them in FPC ?

Otherwise it is not going to work out i am affraid.

Greets, Wouter

MarkMLl

  • Hero Member
  • *****
  • Posts: 6682
Re: Example code for IR component.
« Reply #3 on: February 13, 2023, 02:56:12 pm »
Use an Arduino, or something comparable, to convert the device's bitstream into a bytestream that you can read via USB.

There's quite simply too many layers of detail to make it feasible to do this directly with a PC, particularly now that microcontrollers and their support components are so cheap.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: Example code for IR component.
« Reply #4 on: February 18, 2023, 06:59:23 am »
The RCapture demo illustrates how to capture the demodulated signal from an infrared receiver.  The example only captures timing information of the marks and spaces, decoding this requires information of different protocols.  This is possible (there are existing Arduino examples), but the primary intent of this example is to illustrate how to capture the raw signal.

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Example code for IR component.
« Reply #5 on: February 18, 2023, 06:00:10 pm »
Does compile and program, and responds to old television remote control within half a hour work or so..

Except that it seems to only receive weird data, see attachment.

I am using the TSOP31138 component.

Is it also going to work on 8 mhz ? Or is a 16mhz Cristal permitted ?

I did change the baud rate to 19200 baud.

Can anybody help further ?

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: Example code for IR component.
« Reply #6 on: February 18, 2023, 07:30:33 pm »
Does compile and program, and responds to old television remote control within half a hour work or so..

Except that it seems to only receive weird data, see attachment.
Please share your compiler version and the full compiler command line (in Lazarus it is under Project Options, click on Show Options).  There was a problem with optimization levels above -O2, I discovered it because of corrupted serial output when testing this code.

Quote
I am using the TSOP31138 component.
That should work  Ensure that Vs and ground are connected to the same power supply as the controller and that the OUT pin is connected to PB0.

Quote
Is it also going to work on 8 mhz ? Or is a 16mhz Cristal permitted ?
You can run the controller at 8 MHz, the timing will just be slow by a factor of 2.  Firstly update the F_CPU define to 8000000 (Project Options, Custom Options). Then multiply the measured tick counts by 8 (or shift left by 3):
Code: Pascal  [Select][+][-]
  1.     uart_transmit_asstring(dword(data[2*i]) shl 3);
  2. ...
  3.     uart_transmit_asstring(dword(data[2*i+1]) shl 3);
  4.  

Quote
I did change the baud rate to 19200 baud.
The serial monitor correctly detects the text, so no issues with the baud rate.  This also means that F_CPU is correct.
« Last Edit: February 18, 2023, 07:32:13 pm by ccrause »

pascalbythree

  • Sr. Member
  • ****
  • Posts: 256
Re: Example code for IR component.
« Reply #7 on: February 19, 2023, 04:35:36 pm »
Yay! tank you, tank you, tank you, tank you,  it seems to work for a step further.

Are these the right values?

Incase yes, How do i make a IF statement what button is pressed?

please respond Mr. ccrause / others, i am about there! Greets, Wouter
« Last Edit: February 19, 2023, 05:38:01 pm by pascalbythree »

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: Example code for IR component.
« Reply #8 on: February 19, 2023, 08:59:05 pm »
Are these the right values?
Difficult to say whether it is correct or not without having an idea what the protocol may be.  What remote control are you testing (make and model number)?  The timing results doesn't match the NEC, RC5 or Sony protocols.
 
Quote
Incase yes, How do i make a IF statement what button is pressed?
To identify a button code reliably requires a few extra checks such as ensuring that a long enough quiet time preceded the start of the first bit, checking if the first pulse is a header pulse (if the protocol has one).  This is to ensure your timing sequence starts at the beginning of a transmission.

ccrause

  • Hero Member
  • *****
  • Posts: 856
Re: Example code for IR component.
« Reply #9 on: March 25, 2023, 06:12:48 pm »
Note that one can also visualize the IR remote signal using LazScope.  Below see an example trace of the power button on an LG DVD player remote.

 

TinyPortal © 2005-2018