PDA

View Full Version : Pulse capture on 12F1822



astouffer
- 18th May 2015, 16:55
Hello all, I'm having a bit of a problem using the CCP hardware to measure a pulse length on a 12F1822. I know where the program stalls but I can't figure out why. I debugged the program by lighting an LED at the various points. It never moves past the second while/wend loop when the trailing edge of the pulse is supposed to trigger an interrupt. Any ideas on why the interrupt is never happening? Thanks.


#CONFIG
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _PLLEN_ON
#ENDCONFIG

OSCCON = %01110000 '8Mhz clock with 4x PLL (32MHz)
APFCON = %00000001 'CCP1 on RA5 (pin 2) T1G is RA4 (pin 3)
PORTA = 0 'RA5 all low
TRISA = %00100000 'RA5 input
CM1CON0 = 0 'turn off comparitor 1
CM1CON1 = 0 'configuration of comparator
MDCON = 0 'turn off data modulator
DACCON0 = 0 'turn off DAC
DACCON1 = 0 'turn off DAC
ANSELA = 0 'All pins digital
OPTION_REG = %10000000 'Weak pull ups off
FVRCON = 0 'Bunch of stuff disabled
CPSCON0 = 0 'Cap touch sensors off
CCP1AS = 0
PIE1.2 = 1
PIE2 = 0
T1GCON = 0


DEFINE OSC 32
Include "modedefs.bas"

Symbol Capture = PIR1.2
T1 VAR word
PW VAR word

reload:
low porta.2
CCP1CON = %00000101

T1CON = 0
TMR1H = 0
TMR1L = 0
T1CON.0 = 1

Capture = 0
while !Capture
wend

T1.HighByte = CCPR1H
T1.LowByte = CCPR1L

CCP1CON = %00000100
Capture = 0


while !Capture
wend


PW.HighByte = CCPR1H
PW.LowByte = CCPR1L

PW = PW-T1

serout porta.4,N9600,[#PW,"uS High",13,10]
goto reload

end

LinkMTech
- 18th May 2015, 17:10
It looks like you're monitoring for a positive going pulse that is caught by the first While/Wend but will stay there because the second While/Wend is waiting for it to go Positive again.

Change the second one to:


while Capture
wend


So now it is waiting for the pulse to go Low.

Or am I missing something to?... yup, thought the "Capture" was a pin, it's the interrupt. NM

LinkMTech
- 18th May 2015, 17:50
Okay looked a little closer and "think" you need to enable the interrupts with:


INTCON = %11000000 'Enables global and all active peripheral interrupts

astouffer
- 18th May 2015, 20:25
Louie, thanks for looking it over but I have to admit the mistake of operator error on my part :eek: I was using the sync output on the signal generator instead of the real output. Here is the working code.


#CONFIG
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__CONFIG _CONFIG2, _PLLEN_ON
#ENDCONFIG

OSCCON = %01110000 '8Mhz clock with 4x PLL (32MHz)
APFCON = %00000001 'CCP1 on RA5 (pin 2) T1G is RA4 (pin 3)
PORTA = 0 'RA5 all low
TRISA = %00100000 'RA5 input
CM1CON0 = 0 'turn off comparitor 1
CM1CON1 = 0 'configuration of comparator
MDCON = 0 'turn off data modulator
DACCON0 = 0 'turn off DAC
DACCON1 = 0 'turn off DAC
ANSELA = 0 'All pins digital
OPTION_REG = %10000000 'Weak pull ups off
FVRCON = 0 'Bunch of stuff disabled
CPSCON0 = 0 'Cap touch sensors off
CCP1AS = 0
PIE2 = 0
T1GCON = 0
INTCON = 0


DEFINE OSC 32
Include "modedefs.bas"

Symbol Capture = PIR1.2
T1 VAR word
PW VAR word

reload:
CCP1CON = %00000101

T1CON = 0
TMR1H = 0
TMR1L = 0
T1CON.0 = 1

Capture = 0

while !Capture
wend

T1.HighByte = CCPR1H
T1.LowByte = CCPR1L

CCP1CON = %00000100
Capture = 0


while !Capture
wend


PW.HighByte = CCPR1H
PW.LowByte = CCPR1L

PW = PW-T1

serout porta.4,N9600,[#PW,10,13]
goto reload

end