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.... > 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 did something similar some time ago. Sender was 18F1320 with protocol made up by me. Receiver was a 12F675 using a TSOP IR recever. First I set up the IR-input pin of the 12F675 for "interrupt-on- change" and using passive pullup. The "interrupt edge" was initialy set to "idle-to-carrier-present" (0->1) edge. Then in the ISR for the interrupt-on-change I did : - Was this a 0>1 slope ? We have a start-of-carrier (pulse) edge ! - clear timer 1 - toggle the "interrupt edge" bit. - exit ISR - Was this a 1>0 slope ? We have an end-of-carrier (pulse) edge ! - Check timer 1 for start/zero/one bit. - Take care of the bit accordingly. - toggle the "interrupt edge" bit. - Exit ISR. The timing of the start/zero/one "pulses" was selected in such a way that it was enough to check a single bit (bcf/bsf) in TMR1 to detect the type of pulse. Didn't have to check against some "ranges" at all. Fast and simple. The ISR updated a few status variables to keep track of where it was in the pulse train between different runs of the ISR. The "command" sent begun with a "start" bit, and then about 25 bits in 4-5 different "fields" like "device number", "command", "value" and so on. Since timer 1 is reset at the start of each bit/pulse, there are no cumultative errors. Any timing errors betwen the sending and the receiving PIC's are zeroed out at the beginning of each new pulse. There are never any code "waiting" for anything. The ISR end as fast as possible. The 12F675 was at the same time handling a 100 Hz interrupt on another pin (from a zero cossing detector) and also handling phase control on an output (using another timer interrupt). By building it all without any software timing/delaying code at all, at was much easier to get it all together. Everyting was interrupt driven, the "main" part of the code, just run in a loop checking a number of flags that was set by the different ISR's. Best Regards Jan-Erik. -- 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