I would allow for ±20% deviation in the start pulse. The NEC code has built-in error checking so grabbing the wrong code is not much of a problem.
Where are you doing your testing? Fluorescent lights can output a lot of noise in the 38kHz band that NEC used. Direct sunlight can also be a complication. Otherwise, IR tends to be mostly noise free so catching the start code is usually straightforward.
I would not use PulsIn. Here's some hastily modified and untested code that should work. Blame the forum software for the screwed up spacing/tabs, etc.
Code:
'=======================| GENERIC IR/RF RECEIVER |=========================
'GEN-RF-683.BAS
'PIC12F683 @4MHz uses MPASM
'==========================================================================
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _FCMEN_OFF
DEFINE OSC 4
DEFINE DEBUG_REG GPIO
DEFINE DEBUG_BIT 2
DEFINE DEBUGIN_REG GPIO
DEFINE DEBUG_IN 3
DEFINE DEBUG_MODE 1
DEFINE DEBUG_BAUD 9600
@Timer1=TMR1L
Timer1 VAR WORD EXT
TMR1ON VAR T1CON.0 'Timer1 ON/OFF control bit
IR VAR byte[4]
leadin var byte[2]
period VAR byte
i VAR byte
bits VAR byte
OSCCON =%01100001 '4MHz _INTRC_OSC_NOCLKOUT
CMCON0 =%00000111 'disable comparators
ANSEL =%00000001 'AN0=analog
ADCON0.7 =1 'right justify ADC
TRISIO =%00101011 'input=1,output=0
GPIO =%00101000 'high=1,low=0
INTCON =%00000000 'disable interrupts
T1CON =%00110000 'TMR1 prescaler=8,tick=8µS
init: RF[0]=0:RF[1]=0:RF[2]=0:RF[3]=0:
While GPIO.1=1 'wait falling edge
Wend 'falling edge
TMR0=0:OPTION_REG=%10000101 'TMR0 prescaler=64
While GPIO.1=0 'wait rising edge
Wend 'rising edge
leadin[0]=TMR0:TMR0=0
If (leadin[0]<154) Then init '8500µS (154)
If (leadin[0]>190) Then init '10500µS (190)
While GPIO.1=1 'wait falling edge
Wend 'falling edge
leadin[1]=TMR0:TMR0=0 'clear TMR0
OPTION_REG=%10000011 'TMR0 prescaler=16
i=0
Repeat
While GPIO.1=0 'wait rising edge
If TMR0>186 Then break '3000µS
Wend 'rising edge
While GPIO.1=1 'wait falling edge
If TMR0>186 Then break '3000µS
Wend 'edge
period=TMR0:TMR0=0 'reset TMR0
If (period<49) Then init
If (period>93) Then '1500µS
IR.0(i)=1
EndIf
i=i+1
Until (i>31)
break: If (i<32) Then init
Debug hex2 i
For i = 0 to 3
Debug hex2 IR[i] REV 8
Next
TMR1ON=0:Timer1=0:TMR1ON=1
Debug 13,10
GoTo init
End
Bookmarks