Your approach is far too limited. Some protocols use PulseWidthModulation (PWM), some use PulsePositionModulation (PPM) and some use something entirely different. Also the number of bits in a code can vary from less than 20 to more than 100. Most, but not all, use an extended silence between repeat copies of the code.
My interest has been more with RF than IR but the approach is similar except that most RF receivers are active high while most IR receivers are active low.
Here's some code I use with RF. You'll need to modify it for IR. And it will capture only PWM & PPM codes - there are other protocols.
A more generalized method that can capture any protocol uses a bit array and samples the input at 25-50µS intervals, setting a bit when the sample is high. This gives you an image of the code with the resolution depending on the sample rate.Code:'============================RECEIVER============================ 'PIC12F683 @ 8MHz <500 words 'Receives up to 44 bits of PDM/PWM RF with initial lead-in of 2-9mS 'outputs received codes via RS232 @ 9600bps on GPIO.4 (Pin 3) @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BOD_ON & _CP_OFF & _CPD_OFF DEFINE OSC 8 '8 MHz oscillator DEFINE ADC_BITS 8 DEFINE ADC_CLOCK 3 DEFINE ADC_SAMPLEUS 25 DEFINE DEBUG_REG GPIO DEFINE DEBUG_BIT 4 'GPIO.4 (Pin 3) DEFINE DEBUGIN_BIT 3 'GPIO.3 (Pin 4) DEFINE DEBUG_MODE 1 'Inverted logic DEFINE DEBUG_BAUD 9600 Symbol Capture=PIR1.2 'CCP1 capture flag Symbol PinChng=INTCON.0 'InterruptOnChange Symbol RS232=GPIO.3 'GPIO.3 (Pin 2) Symbol RS485=GPIO.0 'GPIO.0 (Pin 6) RF VAR byte[6] RS VAR byte[6] cfg VAR byte[5] ID VAR byte pw VAR word i VAR byte bits VAR byte bytes VAR byte sof VAR word 'start of frame minSOF VAR word 'minimum start pulse (1900-8500 ~1.9mS-8.5mS) minBits VAR byte adc VAR byte id VAR byte module VAR byte addr VAR byte rssi VAR word 'received signal strength indicator DATA $01,$FE,$6C,$07,$0E,$01 'EEPROM module,id,minSOF (1900),minBits (14),adc OSCCON = %01110001 'INT HF OSC 8MHz WHILE OSCCON.3>0:WEND 'OSC startup timeout WHILE OSCCON.2=0:WEND 'INT HF OSC stable CMCON0 =%00000111 'comparators off TRISIO =%00101110 'make GPIO.0,4 outputs GPIO =%00000000 'make GPIO.0,4 LOW ANSEL =%00000010 'make GPIO.1 analog ADCON0.7=1 'right justify ADC result for 10-bit IOC =%00001001 'IOC Pins enabled INTCON =%00000000 'disable interrupts READ 0,module DEBUG DEC module,32 READ 1,id DEBUG DEC id,32 READ 2,minSOF.LowByte READ 3,minSOF.HighByte DEBUG DEC minSOF,32 READ 4,minBits DEBUG DEC minBits,32 READ 5,adc DEBUG DEC adc,10,13 init: RF[0]=0:RF[1]=0:RF[2]=0:RF[3]=0:RF[4]=0:RF[5]=0 CCP1CON=%00000101:Capture=0 'capture rising edge While !Capture 'wait for rising edge Wend TMR1H=0:TMR1L=0:T1CON=%00010000 'prescale=2, tick=1uS ADCIN 1, rssi 'read pulse amplitude CCP1CON=0:Capture=0 'capture falling edge While !Capture 'wait for falling edge Wend sof.HighByte=CCPR1H:sof.LowByte=CCPR1L If (sof<minSOF) Then init '<min so abort If (sof>9600) Then init '>max so abort ADCIN 1, i 'read space amplitude If (i<rssi) Then rssi=rssi-i Else rssi=i-rssi EndIf CCP1CON=%00000101 'capture rising edge bits=0:Capture=0:INTCON.2=0 While !Capture 'wait for rising edge Wend Repeat TMR1H=0:TMR1L=0:T1CON=%00010000 'prescale=2, tick=1uS TMR0=100 'overflow 156*16=2496uS (100+156=256) OPTION_REG=%10000100:Capture=0 'TMR0 prescale=16 While !Capture 'wait rising edge If INTCON.2=1 Then break 'TMR0 overflow (GAP>2.5mS) Wend pw.HighByte=CCPR1H:pw.LowByte=CCPR1L If (pw<850) Then init '<0.85mS so abort If (pw>1300) Then '>1.3mS RF.0(i)=1 'set bit EndIf i=i+1 Until (i>43) break: If (bits<>20) Then bits=i+1 EndIf If (bits<minBits) Then init bytes=(bits)/8 If ((bits)//8>0) Then bytes=bytes+1 EndIf Debug bits For i = 0 to bytes Debug ihex2 RF[i] RS[i]=RF[i] Next DEBUG ihex2 (sof/40) If (adc>0) Then DEBUG ihex2 (rssi>>2) EndIf debug 13,10 GoTo init End
You might find some of the links on my web page useful.




Bookmarks