Editing Experimenting with IR Remotes using a PIC running BoostC Project

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
 
= Experimenting with IR Remotes using a PIC running BoostC Project =
 
= Experimenting with IR Remotes using a PIC running BoostC Project =
 +
 +
 +
'''EARLY DRAFT - NOT READY'''
 +
  
 
*Name: Experimenting with IR Remotes using a PIC running BoostC Project
 
*Name: Experimenting with IR Remotes using a PIC running BoostC Project
*Status: done, until enhanced
+
*Status: early draft -- do not read
 
*Technology: PIC microcontroller, slavaged IR reciever, running with code in BoostC  
 
*Technology: PIC microcontroller, slavaged IR reciever, running with code in BoostC  
 
*Author: [[russ_hensel]] ( where you can find an email address to reach me )
 
*Author: [[russ_hensel]] ( where you can find an email address to reach me )
 
*Summary: A PIC16F877A project that reads IR remote controls. Runs with a PC running a terminal program.
 
*Summary: A PIC16F877A project that reads IR remote controls. Runs with a PC running a terminal program.
*Last revision: see page history.
+
*Last revision Dec 21 2008 – Early Draft.  
*Download: available, see section below.
+
*Download: see section below.
  
 
This project describes just enough software and hardware to let you experiment with infra red remote controls to use as a jumping off point for a more complete project.
 
This project describes just enough software and hardware to let you experiment with infra red remote controls to use as a jumping off point for a more complete project.
Line 13: Line 17:
 
Platform: PIC16F877A using BoostC connected via rs232 to a PC running a terminal program.  The PIC chip is supplemented with a slavaged IR reciever.
 
Platform: PIC16F877A using BoostC connected via rs232 to a PC running a terminal program.  The PIC chip is supplemented with a slavaged IR reciever.
  
This code may be used as part of a user interface into a larger project.  One pin supports all the buttons on a remote, and a universal remote is only about $10. and works remotely.
 
  
 
== Hardware ==
 
== Hardware ==
 +
  
 
Taking apart old VCR's and similar devices you can often find small IR remote receivers.  These are outwardly simple devices, an IR sensor looks something like a LED but perhaps hidden behind a grill or filter and 3 wires.  The wires are for power ( two wires, +5 volts and ground ) and one for the signal which goes to a PIC input port.  Shine the light from an IR remote control ( from a broken, or not TV, the dump or buy a universal remote ) and the output pin jumps up and down for a while.  Learn what the jumping means and figure out what button was pressed on the remote.  Add a whole new interface to your PIC projects using only one pin.
 
Taking apart old VCR's and similar devices you can often find small IR remote receivers.  These are outwardly simple devices, an IR sensor looks something like a LED but perhaps hidden behind a grill or filter and 3 wires.  The wires are for power ( two wires, +5 volts and ground ) and one for the signal which goes to a PIC input port.  Shine the light from an IR remote control ( from a broken, or not TV, the dump or buy a universal remote ) and the output pin jumps up and down for a while.  Learn what the jumping means and figure out what button was pressed on the remote.  Add a whole new interface to your PIC projects using only one pin.
Line 22: Line 26:
  
 
For the PIC part of the project I use my favorite 16F877A set up as in [[PIC based Stepper Motor Dancing Analog Clock]].  Use input RB.0 as it has an interrupt available.  Use the UART and a PC for other control of and communications with the PIC as with the Clock.
 
For the PIC part of the project I use my favorite 16F877A set up as in [[PIC based Stepper Motor Dancing Analog Clock]].  Use input RB.0 as it has an interrupt available.  Use the UART and a PC for other control of and communications with the PIC as with the Clock.
 +
 +
== Software ==
 +
 +
If you look at the subroutine readIR( void ) you will see the heart of the IR receive.  The routine loops each time checking to see if the output of the detector has changed.  If it has not it adds one to the period, if it has it records the period and resets the period to 0.  When the input goes quiet for a while it is taken as the end of the signal and we exit. Pretty simple.
 +
 +
The software logs the period of each transition at the input pin in units of the time around the receiving loop.  The number of log entries is limited to about 100 due to memory limitations.  Because the timing is in terms of times around the receive loop the code has been written to make this time ( roughly ) constant despite branches within the loop.
 +
 +
Control of readIR() is normally via an endless loop ( which the stop command breaks you out of ) which:
 +
 +
Listens for quiet on the IR signal, this stops us from starting to receive in the middle of a signal.
 +
Setting an interrupt to call readIR() on the next IR signal.
 +
Running readIR()
 +
Outputting the log or the received signal.
 +
 +
You can take the data from the log and put it in a spreadsheet to try to analyze what is going on.  There is much information on IR protocols on the web.
 +
 +
One of my remotes seems to use the NEC protocol and I have added a version of readIR() to decode this signal, it is called readIRNec().  In this protocol there are basically two different low periods of the input, a short one is read as a 0 the long one as a 1.  The code seems to work.  I got the information on the protocol from [http://www.sbprojects.com/knowledge/ir/nec.htm].
 +
 +
 +
Control of the software is via the serial communications library that I have described in other articles including [[Serial Communications Library -- BoostC and 16F877A]].  It yields a command driven interface with the following commands.
 +
 +
 +
 +
v<cr>
 +
report the version of the software
 +
 +
r<cr>
 +
 +
reset the counters and the log of the encoder
 +
 +
l<cr>
 +
 +
report the log of data
 +
 +
 +
{| class="wikitable"
 +
|-valign="top"
 +
|Command
 +
|Action
 +
<!-------------------------------->
 +
|-valign="top"
 +
|!
 +
|Stop whatever is running, like read ir and and read NEC.  Then you can select a new command.
 +
<!-------------------------------->
 +
|-valign="top"
 +
|V<cr>
 +
|Version of the software, responds something like:  IR.c version = Dec 29 2008 b
 +
<!-------------------------------->
 +
|-valign="top"
 +
|I<cr>
 +
|Read Ir Data in a loop without decoding. Generally turn log on or you will not see much in the way of results. 
 +
<!-------------------------------->
 +
|-valign="top"
 +
|Ln<cr>
 +
|Turn log report on with n=1, off with n=0.
 +
<!--------------------------------
 +
|-valign="top"
 +
|<cr>
 +
|
 +
<!--------------------------------
 +
|-valign="top"
 +
|<cr>
 +
|
 +
<!--------------------------------
 +
|-valign="top"
 +
|<cr>
 +
|
 +
<!--------------------------------
 +
|-valign="top"
 +
|<cr>
 +
|
 +
<!-------------------------------->
 +
|-valign="top"
 +
|<cr>
 +
|
 +
<!--------------------------------
 +
|-valign="top"
 +
|xx
 +
|
 +
<!-------------------------------->
 +
 +
|}
 +
  
 
=== Schematic ===
 
=== Schematic ===
  
I think you can figure this out, if not take a look at another project like [[PIC based Stepper Motor Dancing Analog Clock]]
+
[[Image:pending.png | Schematic]]
 +
 
  
Parts
+
 
 +
Parts -- this is not up to date, working on it
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 43: Line 132:
 
<!--------------------------------
 
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
|IR three terminal receiver.
+
|Stepper Motor
 
|I used a salvage one I had lying around.
 
|I used a salvage one I had lying around.
 
<!--------------------------------
 
<!--------------------------------
Line 49: Line 138:
 
|
 
|
 
|
 
|
<!--------------------------------
+
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
 
|Power supply splitting resistors, form a pseodo ground at about 2.5 volts.
 
|Power supply splitting resistors, form a pseodo ground at about 2.5 volts.
 
|100 more or less
 
|100 more or less
<!--------------------------------
+
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
 
|RIN
 
|RIN
 
|about 5 to 10 K   
 
|about 5 to 10 K   
<!--------------------------------
+
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
 
|RFB
 
|RFB
Line 65: Line 154:
 
|xx
 
|xx
 
|
 
|
<!-------------------------------->
+
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
 
|Q = crystal
 
|Q = crystal
Line 81: Line 170:
 
|RRA2, RRA3  = Pull up resistors
 
|RRA2, RRA3  = Pull up resistors
 
|10K more or less
 
|10K more or less
<!-------------------------------->
+
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
 
|C_BP  = By Pass Cap.
 
|C_BP  = By Pass Cap.
|Good idea to add one. A .01 to .1 mfd mica or other by pass cap, good at high frequency seems good.
+
|Not on schematic, good idea to add one. A .01 to .1 mfd mica or other by pass cap, good at high frequency seems good.
 
<!--------------------------------
 
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
Line 97: Line 186:
 
|PUSH_BUTTON_SWITCH  = Reset
 
|PUSH_BUTTON_SWITCH  = Reset
 
|Push to reset the processor.  Mine was from salvage.
 
|Push to reset the processor.  Mine was from salvage.
 +
<!--------------------------------
 
<!--------------------------------
 
<!--------------------------------
 
|-valign="top"
 
|-valign="top"
Line 136: Line 226:
  
 
All commands ( except stop should be terminated with a carriage return ) Note that the command interface is not very smart, giving parameters that are out of range my blow the whole program up. If so reboot the PIC. Do not send a new command ( except stop ) until earlier commands have been completed ( actually you can get ahead some if you are careful )
 
All commands ( except stop should be terminated with a carriage return ) Note that the command interface is not very smart, giving parameters that are out of range my blow the whole program up. If so reboot the PIC. Do not send a new command ( except stop ) until earlier commands have been completed ( actually you can get ahead some if you are careful )
 +
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 191: Line 282:
 
== Microcontroller Program  ==
 
== Microcontroller Program  ==
  
=== Design -- Polling Routine ===
+
=== Design ====
 
 
The code in the download files is the project \IR.
 
 
 
If you look at the subroutine readIR( void ) you will see the heart of the IR receive.  The routine loops each time checking to see if the output of the detector has changed.  If it has not it adds one to the period, if it has it records the period and resets the period to 0.  When the input goes quiet for a while it is taken as the end of the signal and we exit. Pretty simple.
 
 
 
The software logs the period of each transition at the input pin in units of the time around the receiving loop.  The number of log entries is limited to about 100 due to memory limitations.  Because the timing is in terms of times around the receive loop the code has been written to make this time ( roughly ) constant despite branches within the loop.
 
 
 
Control of readIR() is normally via an endless loop ( which the stop command breaks you out of ) which:
 
 
 
Listens for quiet on the IR signal, this stops us from starting to receive in the middle of a signal.
 
Setting an interrupt to call readIR() on the next IR signal.
 
Running readIR()
 
Outputting the log or the received signal.
 
 
 
You can take the data from the log and put it in a spreadsheet to try to analyze what is going on.  There is much information on IR protocols on the web.
 
 
 
One of my remotes seems to use the NEC protocol and I have added a version of readIR() to decode this signal, it is called readIRNec().  In this protocol there are basically two different low periods of the input, a short one is read as a 0 the long one as a 1.  The code seems to work.  I got the information on the protocol from [http://www.sbprojects.com/knowledge/ir/nec.htm].
 
 
 
I have tried to do a good job commenting the program, try reading them for more info.  Comments on the comments, email me [[russ_hensel]].
 
 
 
Note that the design blocks other activities in the software once the interrupt is triggered.  This lasts about .1 seconds.  I think the next itteration of the software should interrupt on each bit ( not just the starting bit ) and a timer should be used to time the bits.  Then other tasks using either the main loop or other interrupts would not suffer much inpact.  I will work on this at some future time ( update, I have an almost complete implementation Jan 9 09, should post fairly soon ).  Do you have some code? Add your material here.
 
 
 
Some credit should be given to John Main [http://www.best-microcontroller-projects.com/infrared-receiver.html] whose code I studied to aid in writing this.
 
 
 
Control of the software is via the serial communications library that I have described in other articles including [[Serial Communications Library -- BoostC and 16F877A]].  It yields a command driven interface with the following commands.
 
 
 
=== Commands ===
 
 
 
{| class="wikitable"
 
|-valign="top"
 
|Command
 
|Action
 
<!-------------------------------->
 
|-valign="top"
 
|!
 
|Stop whatever is running, like read ir and and read NEC.  Then you can select a new command.
 
<!-------------------------------->
 
|-valign="top"
 
|V<cr>
 
|Version of the software, responds something like:  IR.c version = Dec 29 2008 b
 
<!-------------------------------->
 
|-valign="top"
 
|I<cr>
 
|Read Ir Data in a loop without decoding. Generally turn log on or you will not see much in the way of results. 
 
<!-------------------------------->
 
|-valign="top"
 
|N<cr>
 
|Read Ir Data in a loop with NEC decoding. Generally turn log off unless you are debugging the decoding.
 
Response looks something like:
 
 
 
  exit waitForNoIR()
 
  IR Data = 10001011,01110100,00000011,11111100,
 
  IR Data hex = 8B,74,03,FC,
 
  XOR = 11111111,11111111
 
 
 
<!-------------------------------->
 
|-valign="top"
 
|Ln<cr>
 
|Turn log report on with n=1, off with n=0.  The log report gives the count of periods between all tranitions.
 
When reported the log will look something like:
 
 
 
  Lot Len =66,72,12,24,12,5,12,24,12,5,12,5,12,4,13,23,12,5,12,5,13,23,12,5,12,24,12,24,12,24,12,5,11,25,11,   
 
  6,11,6,11,6,11,6,11,5,12,5,12,5,12,24,12,24,12,24,12,24,12,24,11,25,11,25,11,24,12,5,12,
 
  
The first number is the number of items logged, the rest are the peiriods logged ( up to a max of about 100 )
+
==== Compiling ====
<!--------------------------------
 
|-valign="top"
 
|<cr>
 
|
 
<!--------------------------------
 
|-valign="top"
 
|<cr>
 
|
 
<!--------------------------------
 
|-valign="top"
 
|<cr>
 
|
 
<!--------------------------------
 
|-valign="top"
 
|<cr>
 
|
 
<!-------------------------------->
 
|-valign="top"
 
|<cr>
 
|
 
<!--------------------------------
 
|-valign="top"
 
|xx
 
|
 
<!-------------------------------->
 
 
 
|}
 
 
 
 
 
=== Design -- Interrupt Driven Routine ===
 
 
 
This is similar to the design above but the IR sections have be rewritten to use interrupts.  For an explanation see: [[A Tutorial on PIC interrupts using BoostC including Example Programs]]  The code in the download files is the project \IIR.  This version is probably much better for use in a project than the non interrupt code, but was harder to code, and may be harder to understand.  It only decodes the NEC ( inc Toshiba ) codes, others are logged for timing.
 
 
 
=== Notes on the NEC Decoding ===
 
 
 
Using the [] as I guide I guessed at the periods infolved in decoding 1 and 0's.  The specification say that the odd numbered bytes should be the bitwize not of the next prior byte.  I xor these together, if the decoding is right this should yeild a 1111111 which it seems to do.  The button <1> decodes to 03 ( the third byte ).  Each sucessively higher button goes up by 2.  To see more codes, build the project.  My transmitter was not actually a NEC but was a Toshiba which is supposed to be the same, perhaps it is.  Test stuff yoursel before trusting the code too much.
 
 
 
=== Compiling ===
 
 
The zip file contains the entire source bootst project.  Unzip into a directory and open in source boost.  Set the target to 16F877A.  See the comments in the program header for other setup before compiling.    After compiling my compiler reports something like:
 
The zip file contains the entire source bootst project.  Unzip into a directory and open in source boost.  Set the target to 16F877A.  See the comments in the program header for other setup before compiling.    After compiling my compiler reports something like:
  
Line 305: Line 295:
 
The program seems to be safely under the 2K free compiler limit.
 
The program seems to be safely under the 2K free compiler limit.
  
== Other Things to Try/Additions/Changes/Modification ==
+
== Other Things to Try ==
 +
 
 +
== Additions/Changes/Modification ==
  
 
* The 877A is way overkill for this project unless you make it do a bit more.  Note that only 1 input pin ( with interrupt capabability ) plus the serial lines are really used.
 
* The 877A is way overkill for this project unless you make it do a bit more.  Note that only 1 input pin ( with interrupt capabability ) plus the serial lines are really used.
  
* Make more interrupt driven.
 
 
* Decode other brands of remotes.
 
  
 
== Possibly useful links ==
 
== Possibly useful links ==
Line 318: Line 307:
  
 
More information on serial communications with microcontrollers: [[Microcontroller Serial Communications Articles]]
 
More information on serial communications with microcontrollers: [[Microcontroller Serial Communications Articles]]
 +
  
 
A free terminal program, I like this much better than hyperterminal:
 
A free terminal program, I like this much better than hyperterminal:
Line 326: Line 316:
 
SourceBoost Technologies
 
SourceBoost Technologies
 
http://www.sourceboost.com/
 
http://www.sourceboost.com/
 +
 +
 +
 +
  
 
== Download ==
 
== Download ==
  
[http://home.comcast.net/~russ_hensel/OC/IRCode.zip   Both sets of code in IRCode.zip]
+
pending, not at: [http://home.comcast.net/~russ_hensel/OC/StepperTest_v1.zip Version 1 zip file: StepperTest_v1.zip]
  
 
== Comment, Questions, Contributions? ==
 
== Comment, Questions, Contributions? ==
  
 
Email me [[russ_hensel]], or use the talk page for this topic.  All feedback is welcome.
 
Email me [[russ_hensel]], or use the talk page for this topic.  All feedback is welcome.
 
[[category:projects]][[category:Serial Communications]][[category:Microcontroller]][[category:PIC]][[category:BoostC]]
 

Please note that all contributions to OpenCircuits may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see OpenCircuits:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)