Re: Using hardware capture
Quote:
Originally Posted by
Bruce
That'll work too ... if you have a PIC with the TMR1 gate option.
Code:
' PIC12F609 using Timer1 gate input to record pulse widths
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _IOSCFS_4MHZ & _CP_OFF
CMCON0 = 0 ' comparator disabled
CMCON1 = %00000010 ' TMR1 gate source is T1G pin
GPIO = 0 ' outputs all low at POR
TRISIO = %00010000 ' GPIO.4 = TMR1 gate input
PVAL VAR WORD ' holds pulse time
TMR1H = 0
TMR1L = 0
T1CON = %11000001 ' TMR1 gate active-high, Fosc/4 clock, TMR1 on
Main: ' apply a pulse < 65536uS to TMR1 gate input GPIO.4 to use
WHILE !GPIO.4 ' wait for high signal to trigger TMR1 gate pin
WEND
WHILE GPIO.4 ' wait for signal to return low (stops TMR1)
WEND
PVAL.LowByte = TMR1L ' get TMR1 counts into PVAL
PVAL.HighByte = TMR1H ' from TMR1
TMR1H = 0 ' reset TMR1
TMR1L = 0
GOTO Main
END
For all the NOOBS (like me) don't forget the to make your T1G pin a digital input. I spent a couple hours scratching my head trying to figure out why I couldn't get this code to run on my 12f683......that was all I needed.
Thank you Bruce for the code snippets...can't wait for the book. I actually got started with micros about 10 years ago with the help of your website. I'm only coming back to working with them in the last few months, and your code is helping me again!
Re: Using hardware capture
Hi Picone,
Glad I could help, and welcome back.
FYI: Darrel has a nifty include file you can use to disable all analog features by just including it in your code. See this thread http://www.picbasic.co.uk/forum/showthread.php?t=11100
It's pretty handy when you forget to disable A/D it takes care of it for you.
Re: Using hardware capture
Hi Bruce
i am trying to use your code on 18f452 but it seems to stop at start i must be missing something
DEFINE LOADER_USED 1 ' uses a bootloader
Include "Modedefs.Bas"
Define OSC 20
clear
' Setup Hardware for uart
DEFINE HSER_BAUD 9600
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_CLROERR 1
Symbol Capture = PIR1.2 ' CCP1 capture flag
T1 VAR WORD ' 1st capture value
PW VAR WORD ' 2nd capture value & ultimately final pulse width
TRISC.2 = 1 ' CCP1 input pin (Capture input)
INTCON.7 = 0 ' Interrupts off
hserout [" Start Measure ",10,13 ]
ReLoad:
CCP1CON = 000101 ' Capture mode, capture on rising edge
T1CON = 0 ' TMR1 prescale=1, clock=Fosc/4, TMR1=off (200nS per count @20MHz)
TMR1H = 0 ' Clear high byte of TMR1 counter
TMR1L = 0 ' Clear low byte
T1CON.0 = 1 ' Turn TMR1 on here
Capture = 0 ' Clear capture int flag bit
While !Capture ' Wait here until capture on rising edge
Wend
' Rising edge detected / stuff Timer1 value in T1
T1.HighByte = CCPR1H
T1.LowByte = CCPR1L
CCP1CON.0 = 0 ' Configure capture for falling edge now
Capture = 0 ' Clear capture interrupt flag bit
While !Capture ' While here until capture on falling edge
Wend
' Falling edge detected / stuff Timer1 value in PW
PW.HighByte = CCPR1H
PW.LowByte = CCPR1L
PW = PW-T1 ' High pulse width = PW-T1
' Convert to uS for 20MHz osc with 200nS Timer1 ticks
PW = (PW * 2)/10
hserout ["uS High = ",dec PW,10,13 ]
'DEBUG dec PW,"uS High",13,10 ' Output to RS232 display
GOTO ReLoad
END
Re: Using hardware capture
Not sure what you mean by it seems to stop at start but timer1 is running a lot faster at 20MHz, and you don't mention how far it gets or what it shows as the result, so it's tough to help.
Have you tried at 4MHz?
Re: Using hardware capture
Sorry Bruce
But got it going now
i meant it always displayed: start Measure on pc and no measurements but its ok now
thanks for you support
Isaac