actual hz calculation........
HZ = 16x10^7/count (from ccp) within range of 2500hz to 10MHZ ?
for ccp @ every 16 edges.
don f
actual hz calculation........
HZ = 16x10^7/count (from ccp) within range of 2500hz to 10MHZ ?
for ccp @ every 16 edges.
don f
skimask your last description is exactly what I am trying to do. My code essentially does just that over a 200 cycle period but not very gracefully as written, the start and stop of Timer1 I don't thinks is well done as I am using software to do it rather than hardware. In my attempt to measure the frequency fast, the physics of the problem limits how fast the measurement can be done for a given accuracy, but when comparing to a frequency counter approach I would need a 10 second period for 0.1Hz resolution, where by measuring the period as you point out can provide great resolution fast relative to a frequency counter, I guess I should have been more clear in my original posting, because my target is ~100 msecs for the measurement time. After doing more research it looks like the approach of using the capture/compare module with Timer1 as you point out Don is the way to go. I have not used the capture mode in the PIC's in an interrupt mode, are there any good code examples.
Terry
As they say, you can have 2 of 3 things...fast,good,cheap...
You can have it fast, you can have it good, it won't be cheap.
You can have it good, and you can have it cheap, but it won't be fast.
You can have it cheap, and you can have it fast, but it won't be good
I think you're method will work, and I wouldn't doubt that what you are using right now could work well enough. Why not 'remember' the last bunch of readings, say the last 10 readings, then average them out, discard the oldest, plug in the newest...a moving average if you will. You won't necessarily gain resolution, well, yes you will since you've increased the effective sampling time. You can keep your 100ms measurement time, and still get fairly decent resolution.
Terry, I don't know specifically of capture code examples, the microchip data books lay out general routines. You probably know;
-set ccp for capture on 16 rise edge
-tmr1 prescale to 1
-make int routine in asm and add int pointer name for basic
-set pie and pir's for interrupt enables
---on int -- check for tmr1 overflow(not usable count)
---stop ccp and tmr1
---move count high and low to basic count word
---reset flags capture and tmr1 overflow
---tmr1h and tmr1L = 0
---start ccp and tmr1
basic prog can start and stop interrupts as desired.
took me some good head scratching for int's but is very reliable when working.
Once working, pic will just keep spitting out counts automatically.
don
See this thread for a few examples http://www.picbasic.co.uk/forum/showthread.php?p=23401
You might want to download the .PDF Steve linked to in that thread for more info on using capture. It's very handy once you get the hang of using it.
If you really want to have some fun, take a peek at the 18F2431 series. This one has a TON of options for measuring frequency, pulse width, PWM motor control, high-speed simultaneous 2-channel A/D sampling, and lots of other goodies.
Bruce,
Thanks for the link, I just found it a shortly before you posted your message, I also found a few more of your posts with code for using the capture mode with Timer1, once I realized I needed to search on Capture I think you are the expert on the hardware Timers! I hope I can now greatly improve my code.
Terry
Hi Terry,
Thanks, but I wouldn't call myself an expert on much beyond getting in trouble. At least, that's what my wife says..;o}
However, I have spent an hour or two messing with timers & hardware. Grab yourself an 18F2431, and give this a shot. It's really simple, but very precise.
I'm not really sure what the benefits of (sub hertz) measurements are going to give you. Oversampling A/D measurements is normally a good idea, so you can average measurements over time, but I really don't think this approach is going to give you any advantage when measuring a frequency. I mean, it's either X hZ or it's not.
Oversampling is just going to give you an average over time / the number of samples. If your frequency is changing, the result is garbage anyhow.
This is real simple. It sets up an 18F2431 to measure the time period between rising edges, which is the frequency.
With another PIC programmed to output a 4kHz signal into the 18F2431 CAP1 pin, this returns;Code:DEFINE OSC 4 DEFINE DEBUG_REG PORTC DEFINE DEBUG_BIT 6 DEFINE DEBUG_BAUD 9600 DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true T1 VAR WORD Capture VAR PIR3.1 False CON 0 True CON 1 ' auto time base reset, frequency mode, capture on every rising edge Frequency CON %01000101 ANSEL0=0 ' All digital TRISA.2 = 1 ' this is your (CAP1) input pin measuring the frequency INTCON = 0 ' Interrupts off TMR5H = 0 ' Clear high byte of TMR5 counter TMR5L = 0 ' Clear low byte T5CON = %00000001 ' prescale=1:1, int clock, TMR5=on CAP1CON = Frequency ' we're measuring a frequency DEBUG "START",13,10 Main: Capture = False ' Reset capture flag GOSUB Freq ' get frequency PAUSE 500 GOTO Main Freq: ' Frequency measurement mode, capture Timer5 count every rising edge ' with auto reset of Timer5 on each event WHILE Capture = False WEND T1.HighByte = CAP1BUFH T1.LowByte = CAP1BUFL DEBUG "Freq = 1/",DEC T1,"uS",13,10 RETURN END
Freq = 1/250uS
Freq = 1/250uS
Freq = 1/250uS
1/250uS = 4kHz.
The 18F2431 has a lot of nifty features that can save you a boat-load of code & time.
It's definitely worth looking into. This captures the time between rising edges + resets the timer after each capture event. Very handy stuff...;o}
Bookmarks