On Sat, Apr 17, 2004 at 02:54:11PM +0100, Matt Marsh wrote: > Hi guys, > > For my second PIC project, I'm going to need to detect a button > being pressed on an IR remote control. I've done a small amount of > research on this and from what I've read I'm going to need to hook > up an IR receiver to one of the pins and then time how long pulses > last (and/or the gaps between them depending on the remote). Right on the mark. > > So, in order to determine how long a pulse lasts I'm thinking that > I'm going to need to do something like: > > - loop waiting for pin to go high > - reset TMR0 > - loop waiting for pin to go low > - value in TMR0 now contains length of time of pulse > > I just wanted to get some feedback whether I'm thinking of this in > the right way, or whether there is some other technique that is > generally used for timing pulses etc. "Young grasshoppa, there is no right, there is no wrong, there is only options." This is especially true here. Let me make a couple of observations then throw out my suggests: 1) Typically for remote protocols, only the pulse width of one polarity is important. And again typically this is the polarity of when the IR led is on and a pulse is actively being sent. See this web page for examples: http://users.pandora.be/davshomepage/sony.htm You are on point on that with your code above. 2) The difference pulse widths are often multiples of some base width. So in the Sony example the base width T is defined as 600 uS. So a 0 it 1T, a one is 2T, and a start bit is 4T. So it's possible to measure at a much longer interval then simply counting. So now onto how to get it done. If you have a CCP (Capture/Compare/PWM) module it's your bestest friend for such a project. It'll measure the pulse widths for you, give you an interrupt when it's done capturing, and has programmable polarity and counting speed. It's literally set and forget. Otherwise the purely software approach you outlined above is possible. However it may be better to integrate it into an interrupt routine where you capture or count samples at an interval of T/3 or similar. Then you'll get max 3T samples/counts for every interval. So say T=600 uS so you interrupt then capture/count every 200 uS. Since zero=T one=2T and start=4T, then the counts you'd get for zero is 2-3, one is 5-6 and start is 11-12. Now the beauty of this approach is that now the capture happens completely in the background so you can get other stuff done while you waiting for transistions. If you have a Universal remote the Sony protocol is nice because it's simple, and very well documented. Hope this helps, BAJ -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics