Frequency Scaling - remapping
email@example.com email (remove spam text)
To measure the frequency of an input signal (be it 50/50 or 60/40 duty
cycle - or whatever), you can do the following: set up the capture and
compare module (mid-range PICs) CCP1, using timer1, to sample the signal on
its rising edge (but not enable the interrupt for the CCP!), and then use
timer 2, to interrupt at a rate a bit faster than the rate of your incoming
signal, then at each interrupt save the count from the capture register (if
its flag is set) and subtract the previous value. That way you'll get a
count at 1/4 the crystal frequency that represents the time period of your
Example: set CAP1 to gather timer1 count each rising edge. Let timer1 free
run. Use timer2 set to overflow at a count of say 250 and an interrupt on
overflow. That means you'll be sampling your "captured" signal every 1/4 ms
(250 useconds - assuming a 4 Mhz xtal). A signal of 2000 kHz would have a
count of 500 (plus or minus roughly one by jitter).
You can then map that signal to something else.
Hope that helps.
On Tuesday, June 06, 2000 8:40 AM, Peter Betts [SMTP:NOKIA.COM] peter.betts
> I'm Pete and I'm a recent convert to the PIC chip.
> I am trying to build a project at home which measures the frequency of a
> digital signal, modifies the frequency value and uses this to generate a
> frequency output.
> e.g. 1kHz in gets scaled by 2/3 say and frequency output is 666.66Hz
> Firstly I wanted to just get the reading of the frequency working (exact
> frequency is not important just the relative scaling)
> So I set up a timer at a rate of approx 3.9ms which I used to call an
> interrupt routine. The interrupt routine then tested/checked the input
> signal level (hi or lo) and then incremented a counter. This gave me a
> resolution from 1Hz to 256Hz using a 5MHz XTAL. (Just using a single 8
> The counter was used to count the time between successive high pulses
> was to ensure if I had an input which was not a 50:50 duty cycle I could
> still scale it and generate a 50:50 duty cycle output.) The counter
> was the hi or lo period respectively.
> First: Does anybody have a better way of measuring the period of an
> incomming signal in order to generate a scaled frequency output.
> Second: My output signal seems to gitter quite a bit and I don't know
> Third: (and finally) I ideally want to remap the input so I guess I could
> use a 256 element look up table and for every frequency input value I can
> look up a new output value so I can have my own user defined mapping.
> or bad?
> I have a basic system working but it doesn't look at all elegant so I
> appreciate new ideas.
See also: www.piclist.com/techref/microchip/devices.htm?key=pic
You must be a member of the
piclist mailing list
(not only a www.piclist.com member) to post to the