What type of accuracy are you after? I've had a lot of experience with both IR and RF and have found ±10% is adequate as most of the protocols were developed for battery operation where there can be a lot of variation. What protocol are you trying to capture?
Here's code that I use with X-10 RF signals (which are essentially the same as NEC IR signals) as well as with several other RF protocols. It can handle almost all PDM/PWM signals. The resolution is only limited by the timer prescaler. If you want better resolution, use a faster oscillator. I was replacing a 12C508A in an existing module so had to use the internal 4Mhz oscillator. You can modify this to capture both pulses and spaces. I only needed the time between rising edges here.Code:'MR26X509.BAS 'PIC12F509 @ 4MHz 663 words uses MPASM 'Pin 1 - Vdd 'Pin 2 - GPIO.5 'Pin 3 - GPIO.4 'Pin 4 - GPIO.3 'Pin 5 - GPIO.2 RS232 output @ 9600bps 'Pin 6 - GPIO.1 Receives up to 48 bits of PDM/PWM RF with lead-in of 2-9mS 'Pin 7 - GPIO.0 'Pin 8 - Vss 'TMR0 1:64 29=1.9mS,145=9.3mS - 1:16 49=0.8mS,93=1.5ms,155=2.5ms '=============================================================== @ __config _IntRC_OSC & _WDT_OFF & _MCLRE_OFF & _CP_OFF DEFINE OSC 4 DEFINE DEBUG_REG GPIO DEFINE DEBUG_BIT 2 DEFINE DEBUG_MODE 1 DEFINE DEBUG_BAUD 9600 RF VAR byte[6] i VAR byte period VAR byte bytes VAR byte sof VAR byte x VAR byte OPTION_REG=%11000101 'TMR0 1:64 prescaler, disable TOCKI TRISIO=%000010 'make GPIO.1 an input, rest outputs GPIO=%000000 'all outputs low Debug "MR26X",13,10 Debug "Copyright ",169," 2009 dlh",13,10 Goto init hexout: If x>>4<10 Then Debug x>>4+48 Else Debug x>>4+55 EndIf if x//16<10 Then Debug x//16+48 Else Debug x//16+55 EndIf Return init: RF[0]=0:RF[1]=0:RF[2]=0:RF[3]=0:RF[4]=0:RF[5]=0 While GPIO.1=0:Wend 'wait rising edge TMR0=0 'clear TMR0 OPTION_REG=%11000101 'TMR0 prescaler=64 While GPIO.1=1:Wend 'wait falling edge sof=TMR0 'read TMR0 If (sof<29) Then init '1920µS OPTION_REG=%11000011 'TMR0 prescaler=16 While GPIO.1=0:Wend 'wait rising edge TMR0=0 'reset TMR0 OPTION_REG=%11000011 'TMR0 prescaler=16 i=0 Repeat While GPIO.1=1 'wait falling edge If TMR0>175 Then break '2800µS Wend 'falling edge While GPIO.1=0 'wait rising edge If TMR0>175 Then break '2800µS Wend 'rising edge Period=TMR0:TMR0=0 'clear TMR0 OPTION_REG=%11000011 'TMR0 prescaler=16 If (period<49) Then init '800µS If (period>93) Then '1500µS RF.0(i)=1 EndIf i=i+1 Until (i>47) break: If (i<12) Then init bytes=i/8 If i//8 Then bytes=bytes+1 EndIf x=i:GoSub hexout For i = 0 to bytes-1 x=RF[i]:GoSub hexout Next Debug 13,10 GoTo init End




Bookmarks