How Can I increase the precision of the COUNT function?
Syd
Posts: 13
Hi Everyone,
I am using the basic stamp (BS2px) to detect the frequency of an incoming digital stream. How can I increase the precision of the result? For example, if I measure the frequency using a common hand-held analyzer, it displays 60.4 Hz, where as the basic stamp would record 60 Hz.
Cheers!
Syd
Ref:
Below is the sample code that I am using;
DurAdj CON $37B ' Duration / 0.287
Setup:
CONFIGPIN SCHMITT, 11000000000000 'Enable Schmitt-Triggers P12-P15
'
[ I/O Definitions ]
FreqIn PIN 15 ' frequency input pin
'
[ Constants ]
OneSec CON 500 ' capture window = 1 sec
'
[ Variables ]
cycles VAR Word ' counted cycles
'
[ Program Code ]
Main:
DO
COUNT FreqIn, OneSec */ DurAdj , cycles ' count for 1 second
DEBUG HOME,
"Frequency: ", DEC cycles*2, " Hz" ' display
LOOP
I am using the basic stamp (BS2px) to detect the frequency of an incoming digital stream. How can I increase the precision of the result? For example, if I measure the frequency using a common hand-held analyzer, it displays 60.4 Hz, where as the basic stamp would record 60 Hz.
Cheers!
Syd
Ref:
Below is the sample code that I am using;
DurAdj CON $37B ' Duration / 0.287
Setup:
CONFIGPIN SCHMITT, 11000000000000 'Enable Schmitt-Triggers P12-P15
'
[ I/O Definitions ]
FreqIn PIN 15 ' frequency input pin
'
[ Constants ]
OneSec CON 500 ' capture window = 1 sec
'
[ Variables ]
cycles VAR Word ' counted cycles
'
[ Program Code ]
Main:
DO
COUNT FreqIn, OneSec */ DurAdj , cycles ' count for 1 second
DEBUG HOME,
"Frequency: ", DEC cycles*2, " Hz" ' display
LOOP
Comments
-Phil
Assuming that I would like to obtain the frequency every 500ms (half a second), how would I go about this? Would I have to set the count to 500ms, and modify the "DurAdj" multiplier? If yes, how does $37B translate to 0.287? Thanks.
-Syd
REF:
DurAdj CON $37B ' Duration / 0.287
Setup:
CONFIGPIN SCHMITT, 11000000000000 'Enable Schmitt-Triggers P12-P15
'
[ I/O Definitions ]
FreqIn PIN 15 ' frequency input pin
'
[ Constants ]
OneSec CON 10000 ' capture window = 10 sec
'
[ Variables ]
cycles VAR Word ' counted cycles
'
[ Program Code ]
Main:
DO
COUNT FreqIn, OneSec */ DurAdj , cycles ' count for 1 second
DEBUG HOME,
"Frequency: ", DEC cycles, " Hz" ' display
LOOP
-Phil
Below is my new sample script using PULSIN (I am using BS2px). However, the frequency results from the stamp shows 48, whereas the actual frequency is 60.4 Hz (measured from a hand-held device). The stamp's manual (pg 108) shows that PULSIN works best for frequencies above 100 Hz (which may be the reason for a measurement of 48 rather than 60).
What do you think? Is there anyway I could modify some of the parameters below to obtain 60.4Hz using pulsin? Thanks.
Cheers!
Syd
REF:
Setup:
CONFIGPIN SCHMITT, %1111000000000000 'Enable Schmitt-Triggers P12-P15
'
[ I/O Definitions ]
FreqIn PIN 15 ' frequency input pin
Scale CON $0CF ' 0.81 us per unit
'
[ Variables ]
pHigh VAR Word ' high pulse timing
pLow VAR Word ' low pulse timing
period VAR Word ' cycle time (high + low)
freq VAR Word ' frequency
'
[ Initialization ]
Reset:
DEBUG CLS, ' setup report output
"Period.(uS)... ", CR,
"Freq (Hz)..... "
'
[ Program Code ]
Main:
DO
PULSIN FreqIn, 0, pLow ' get high side of input
PULSIN FreqIn, 1, pHigh ' get low side of input
period = (pLow + pHigh) */ Scale ' scale to uSecs
freq = 62500 / period * 16 ' calculate frequency
DEBUG CRSRXY, 15, 0, DEC period, CLREOL, ' display values
CRSRXY, 15, 1, DEC freq, CLREOL
LOOP
See Tracy Allen's long division routine for a way around the problem:
Also, if you don't mind:
-Phil
Any doubts please just posted in the wall.
Try this,
This uses the binary division routine that Phil pointed out.
The period limitation at the low end is due to the fact that the Stamp returns a word value up to 65535, and combined with the 0.81 microsecond resolution of the BS2px PULSIN, that comes out to 53.0842 milliseconds maximum. A pulse longer than that will time out and will return a value of zero. On the original Stamp (and the 'e and 'pe) the resolution is 2 µs, so their maximum pulse width is 131.072 ms.
(cgx, as a forum moderator, I have to ask that you refrain from "off the wall" comments. Is it supposed to make any sense?)