
Originally Posted by
cncmachineguy
I don't see how to set this up for 40Mhz. At any rate, what speed are you running?
Do you have a free pin to toggle each interupt to see exactly what the frequency is? While I agree the seeming like 1K is a far cry from 10K, but for me I just like to know for sure to fine tune it.
Also as a suggestion, DT pointed me to TMR2 and the use of the PR register. The advantage is no reloading of the preload.

it also has pre and post scaling to turn your 8 bit counter into a slower counter.
Now for more confusion, your preload will give you 32 instructions before overflowing. I think you have that in the int handler itself. not that it will matter, but if you are running at 32 Mhz, 4uSec timing, or 250KHz. So maybe you are flooding HSEROUT?
Hi Bert.
Yeah, I got carried away with the max Osc value this pic can run at - it's 32Mhz.
that said, I'm only running it at 8Mhz (there an errata sheet out that says early versions of this PIC had probs with ADC above 8Mhz clock - so I'm staying on the safe side)
Good call about the toggle - I've put a toggle pin line in the interrupt handler, my scope measures the square wave at 2.66Khz, but I'm assuming the interrupt rate is double that? (half, the time low, half the time high)...therefore I've an interrupt frequency of about 5Khz
I'll have a a go with Timer2, but the PICcalc timers that abound are doing my head in! :-)
Edit:
Just tried timer2...
Code:
@ __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF & _LVP_OFF
@ __CONFIG _CONFIG2, _LVP_OFF
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
Osccon = %01110010 'Osc 8Mhz
DEFINE OSC 8
Now the timer2 settings...
Code:
'Timer2 Registers:
'Prescaler=1:1; TMR2 PostScaler=1:1; PR2=66 - Freq = 30,303.0303Hz - Period = 33,000 ns
T2CON.6 = 0 ' bits 6-3 Post scaler 1:1 thru 1:16
T2CON.5 = 0
T2CON.4 = 0
T2CON.3 = 0
T2CON.2 = 1 ' Timer2 on bit: 1=Timer2 is on;
T2CON.1 = 0 ' bits 1-0 Prescaler Rate Select bits
T2CON.0 = 0
T2CON = %00000100
PR2 = 66 ' PR2 (Timer2 Match value)
Now the interrupt routine...
Code:
ADC_Sample:
@ INT_DISABLE TMR2_INT ; enable Timer 0 interrupts
toggle PortC.3
ADCIN 8, Signal_In 'sample the incoming signal
IF SIGNAL_IN > 130 THEN 'a level of 128 is the zero crossing point (130 adds a bit of margin for error)
zero_cross = 1 'a simple toggle flag to track whether we're above zero or below
IF SIGNAL_IN > PREVIOUS_SAMPLE THEN previous_sample = signal_in ; if this latest incoming sample is bigger than the last then it supercedes the prvious value
GOTO INTERRUPT_END
ENDIF
if signal_in < 126 and zero_cross = 1 then 'if sample is 128 or less, then we're at the zero cross point, (126 adds some margin for error) therefore we've finished trying to sample peak
peak_sample = previous_sample ' our peak sample is therefore whatever value is in the variable 'previous_sample' from above
previous_sample = 0 'zero it, ready for when the waveform goes into positive territory again.
zero_cross = 0 ' toggle the above zero/below zero flag
endif
INTERRUPT_END:
LOOP_COUNT1 =LOOP_COUNT1+1
if loop_count1 = 100 then
hserout [dec signal_in,9,dec peak_sample,13, 10]
loop_count1 = 0
endif
@ INT_ENABLE TMR2_INT ; enable Timer 0 interrupts
@ INT_RETURN
Still just seeing 5khz 'interrupt frequency' on my scope (this is the pin I'm toggling in the interrupt routine)...

AaaaaaarrrrrggggghhhhhHHHH!!!!!!!
Bookmarks