There are much better/faster ways to do this, but this should get you started with something that's fairly easy to understand.
Assuming you have an NEC transmitter that outputs something like the top portion of the graphic below;

This should return similar results to what's shown in the serial terminal window just above.
Code:
#CONFIG
__config _XT_OSC & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
#ENDCONFIG
DEFINE OSC 4
Leader VAR WORD ' will be up to 900 for a 9mS leader pulse
BtnVal VAR BYTE[32] ' holds 32 pulse results
DByte1 VAR BYTE ' address byte
DByte2 VAR BYTE ' inverse of address byte
DByte3 VAR BYTE ' command byte
DByte4 VAR BYTE ' inverse of command byte
X VAR BYTE ' loop count
Main:
PULSIN PORTB.0,0,Leader ' leader pulse is ~9mS low-going
IF Leader < 850 THEN Main
FOR X = 0 TO 31 ' grab 32 incoming pulses
PULSIN PORTB.0,1,BtnVal(X) ' now measuring high-going pulse widths
NEXT X
' now we'll decode 4 bytes from 32 pulses
FOR X = 0 TO 7 ' sort 1st 8 pulses
IF BtnVal[X] > 150 THEN ' > 150 x 10uS = > 1.5mS pulse period
DByte1.0[X]=1
ELSE
DByte1.0[X]=0
ENDIF
NEXT X
FOR X = 8 TO 15 ' sort 2nd 8 pulses, etc....
IF BtnVal[X] > 150 THEN
DByte2.0[X-8]=1
ELSE
DByte2.0[X-8]=0
ENDIF
NEXT X
FOR X = 16 TO 23
IF BtnVal[X] > 150 THEN
DByte3.0[X-16]=1
ELSE
DByte3.0[X-16]=0
ENDIF
NEXT X
FOR X = 24 TO 31
IF BtnVal[X] > 150 THEN
DByte4.0[X-24]=1
ELSE
DByte4.0[X-24]=0
ENDIF
NEXT X
HSEROUT [BIN8 DByte1,13,10,BIN8 DByte2," Address",13,10,13,10]
HSEROUT [BIN8 DByte3,13,10,BIN8 DByte4," Command",13,10,13,10]
PAUSE 1000
GOTO Main
Bookmarks