Bruce, thank you :-)

Your code worked perfectly at 4 mhz first go :-)

So, I've hacked away at it all day and through trial & error and determination I have got it to work at 20mhz, It was a total pain I could not work out the math for the life of me, so I searched the forum and net, but everyone seems to have a different idea on how to work it out, in the end I resorted to trial & error but I got it to work at 20mhz.

The repeatsub does not work correctly all the time, again I assum a timing error

I show the results below for all to see, I've also tried to add the repeat (button held down) function with limited success, if you or anyone would care to comment on or improve the end result that would be awesom..

'#CONFIG
'__config _XT_OSC & _WDT_ON & _MCLRE_OFF & _LVP_OFF & _CP_OFF
'#ENDCONFIG
'Defines for EasyPic3
DEFINE LCD_DREG PORTB ' Define LCD registers and bits
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3
INCLUDE "AllDigital.pbp" 'Set ALL ports to Digital i/o
include "LCDDEFS.bas" 'easy lcd

DEFINE OSC 20 'set osc to 20mhz

Leader VAR WORD ' will be up to 900 for a 9mS leader pulse
BtnVal VAR word[32] ' holds 32 pulse results 'NEEDS TO BE WORD VAR FOR 20Mhz
DByte1 VAR BYTE ' address byte
DByte2 VAR BYTE ' inverse of address byte
DByte3 VAR BYTE ' command byte
DByte4 VAR BYTE ' inverse of command byte
i VAR BYTE ' loop count

Main:

PULSIN PORTC.0,0,Leader ' leader pulse is ~9mS low-going

IF Leader < 4400 THEN goto Main 'use 850 for 4mhz, use 4400 for 20mhz

FOR i = 0 TO 31 ' grab 32 incoming pulses
PULSIN PORTC.0,1,BtnVal(i) ' now measuring high-going pulse widths

if btnval(i) > 950 then goto repeatsub 'capture repeat pulse ?

NEXT i



' now we'll decode 4 bytes from 32 pulses (address)
FOR i = 0 TO 7
IF BtnVal[i] > 350 THEN ' > use 150 for 4mhz, use 350 for 20mhz
DByte1.0[i]=1
ELSE
DByte1.0[i]=0
ENDIF
NEXT i


'now we'll decode 4 bytes from 32 pulses (command)
FOR i = 16 TO 23
IF BtnVal[i] > 350 THEN ' > use 150 for 4mhz, use 350 for 20mhz
DByte3.0[i-16]=1
ELSE
DByte3.0[i-16]=0
ENDIF
NEXT i

'show results on LCD, address on top line & command on bottom line
Lcdout cmd,clss,cmd,movtoA2, "Address " , hex DByte1 'debug to LCD display
lcdout cmd,movtoB2, "Command " , hex DByte3 'debug to LCD display

'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 200 'dont know why this is needed for it to work ?
GOTO Main




'we get to heare if button on remote is held down
repeatsub:
toggle porta.0 ' led to tell us that a button is held down (repeat)

PAUSE 400 'only god knows why this is needed for it to work ?
goto main
return


Thanks..
Bruce
Ioannas
dhouston
bogdan
cncmachineguy
jumper