- 
	
	
		2 Attachment(s) Pulse train capture 
		I am trying to capture in an array the pulse width of a pulse train coming out of an HCS-301 chip.
 
 The pulses are not always the same in a raw but the width should be fairly consistent as the Logic analyzer confirms, about 800μsec and 400μsec.
 
 The PIC is the F886 at 8MHz so the resolution of PulsIn is 5μsec. Based on this the attached Excel shows some readings and though some pulses are in the correct range others are even 0 despite the use of PulsIn with a positive pulse trigger.
 
 Cannot understand why this happens.
 
 The complete pulse train consist of a guard width of 16ms, then 12 pulses of 50% 400/400μsec, a second guard width of about 4ms and then 66 pulses of 800 or 400μsec.
 
 the code is this:
 
 
	Code: 
 #config
 Line1 = _DEBUG_OFF & _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_ON
 Line2 = _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
 __CONFIG _CONFIG1, Line1 & Line2
 __CONFIG _CONFIG2, _WRT_HALF & _BOR40V
 #endconfig
 
 OSCCON = %01110001 '8MHz Clock
 
 'OSCCON = %01100001  '4MHz Clock
 
 DEFINE OSC 8
 
 ;----- Configuration bits ------------------------------------------------
 
 
 
 PORTA=0:PORTB=4:PORTC=0:PORTE=0
 
 TRISA=%00000000                                         'RA0: RED LED
 'RA1: Relay 1
 'RA2: Relay 2
 'RA3: Relay 3
 'RA4: Relay 4
 'RA5:
 'RA6:
 'RA7:
 
 TRISB=%00111111                                                'RB0: RF in
 'RB1: Channel 4 Latch button
 'RB2: Channel 3 Latch button
 'RB3: Channel 2 Latch button
 'RB4: Channel 1 Latch button
 'RB5: Autolearn Button
 'RB6: NC
 'RB7: NC
 
 TRISC=%00000100                                                'RC0:
 'RC1:
 'RC2:
 'RC3:
 'RC4:
 'RC5:
 'RC6: TX
 'RC7: RX
 
 TRISE=%00000001                                                'RE0: assigned to /MCLR
 
 OPTION_REG.0=1                                                'PSA0 PRESCALER SELECT 1:1 TO 1:256
 OPTION_REG.1=0                                                'PSA1
 OPTION_REG.2=1                                                'PSA2
 OPTION_REG.3=0                                                'PRESCALER TO: 1->WDT, 0->TMR0
 OPTION_REG.4=0                                                'T0SE SOURCE EDGE 1->H TO L, 0->L TO H
 OPTION_REG.5=0                                                'T0CS 1->FROM RA4, 0->FROM INT. CLOCK
 OPTION_REG.6=0                                                'INT EDGE SELECT 0->H TO L, 1->L TO H
 OPTION_REG.7=0                                                'PULL UP 1->DISABLE, 0->ENABLE
 
 WPUB = %00111111
 
 DEFINE HSER_RCSTA 90h
 DEFINE HSER_TXSTA 20h
 DEFINE HSER_BAUD 9600
 DEFINE HSER_SPBRG 12  ' 9600 Baud @ 8MHz, 0,16%
 DEFINE HSER_CLROERR 1 ' Clear overflow automatically
 
 '************************* ADC Setup
 ADCON0 = 0
 ADCON1 = 0        'Set PORTA/E digital
 ANSEL  = 0  'all as digital inputs
 ANSELH = 0  'all as digital inputs
 
 BAUDCTL = %00010000         'Transmit inverted data (No MAX232 needed)
 
 code        VAR byte[9] BANK0   ' Code
 code1       var byte[66]
 temp        var word
 index       var byte
 
 clear
 
 hserout ["Waiting pulse",13,10]
 
 while 1
 pulsin portb.0,0,temp
 if temp>3000 then
 if temp<4000 then     'find lomg header of 16ms
 for index=0 to 10
 pulsin portb.0,1,temp  'then skip 12 pulses of 50% Duty, 400ìsec
 next index
 for index=0 to 65   'now collect 66 pulses of aroun 800 or 400 ìsec
 pulsin portb.0,1,temp
 code[index]=temp
 next index
 for index=0 to 65
 hserout [#code[index],13,10]  'display pulse train
 next index
 hserout ["End",13,10]
 endif
 endif
 wend
 
 Ioannis
 
 
- 
	
	
	
		Re: Pulse train capture 
		False alarm... Sorry.
 
 code[index] should be code1[index] in the for-next loops.
 
 Ioannis
 
 
- 
	
	
	
		Re: Pulse train capture 
		The HCS301 is a KEELOQ encoder device and what you are possibly asking is for the decoder code.
 
 Have you seen AN744?  It gives a decoder in C.  Should not be too far away from implementation in PBP
 https://ww1.microchip.com/downloads/...tes/00744a.pdf
 
 
- 
	
	
	
		Re: Pulse train capture 
		Thank you Jerson.
 
 I had this error in the above routine that collects the pulse train from the receiver module.
 
 Now works remarkably ok. No problem with the decoding. Also works fine.
 
 Ioannis
 
 P.S. Yes I do have this AN and others too. Have also the Keeloq Dev.Kit from Microchip but the C routines are not easy for me. Anyway did help a lot.