here is part of code i wrote to capture the IR codes for NEC protocol from a remote controlled LED RGB lamp project
code uses a 8mhz, PIC12F683, code use IOC with darrel 's int routines
hope it helps
Code:
DEFINE OSC 8 ' Timing referance for pause , pauseus commands
DEFINE PULSIN_MAX 21600 ' Maximum counts( clock ticks) allowed before pulsin times out( 21600 ^ 5ms = 108 ms)
' Norm IR pulse length is 108ms
' ========================= Get IR Code Routine =================
' * *
' * ******* IOC - Interupt Handler routine ******* *
' * *
'====================================================================
GetIRcode:
z = 0 ' Z points to start range of byte
S = 7 ' S points to end range of byte
X = 0 ' start X at 0 to line up with BtnVal array
y = 0 ' Y is used for each byte
rctime IR,0,Leader ' get leader low pulse time value in on GPIO.0
IF Leader < 1700 or Leader > 1875 tHEN ' look for 9000us Low pulse of header for 1st or 2nd key area pulse
@ INT_RETURN ; reject if < 8500us or > 9375us (1700^ 5us= 8500us)
endif
RCtime IR,1,Leader ' check for 1st key seq high pulse for 4.5ms after the 9ms low pulse so no repeat headers or 2nd key
if Leader < 850 or Leader > 950 then ' if high for < 4250us then its the 2nkey /data at wrong or > 4750us ( invalid pulse) ( 850 * 5us = 4250us)
@ INT_RETURN
endif
FOR X = 0 TO 31 ' grab 32 incoming pulses
PULSIN IR,1,leader ' now measuring high-going pulse widths ( pulsin_max set to 21600 = 108ms )
BtnVal(x) = leader/2 ' leader divide by 2 so array Btnval(x) is byte (<255) not word value
NEXT X
for Y = 1 to 4 ' Get in Array and decode 4 bytes from 32 bit pulses
T = 7 ' Set T to 7 so that it puts BtnVal(0) in to D7 location
FOR X = Z TO S ' sort 8 pulses of byte
IF BtnVal[X] > 120 THEN ' > 120 x (5uS *2) = > 1.2mS pulse period = 1 (150)
DByteTmp = DByte[Y] ' get into temp var
DByteTmp.0[T]=1 ' Set value to 0 or 1 , T reverses bit order of BtnVal(x) so byte has correct bin value to write byte
ELSE
DByteTmp = DByte[Y] ' get into temp var
DByteTmp.0[T]=0 ' Set value to 0 or 1 , T reverses bit order of BtnVal(x) so byte has correct bin value to write byte
ENDIF
DByte[Y] = DByteTmp ' get it back into DByte(y) c
T = T - 1 ' T points to next MSB on loop
NEXT X
Z = x ' Z = X (0,8,16,24) points to start of next byte
S = 7 + X ' S (7,15,23,31) points to End of next DByte BtnVal offset to X
next Y ' loop for 4 bytes
Bookmarks