Shop OBEX P1 Docs P2 Docs Learn Events
How Can I increase the precision of the COUNT function? — Parallax Forums

How Can I increase the precision of the COUNT function?

SydSyd Posts: 13
edited 2011-11-18 16:39 in BASIC Stamp
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

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-16 14:45
    Set the duration to 10 seconds, and you should get 604 as the result.

    -Phil
  • SydSyd Posts: 13
    edited 2011-11-18 09:59
    Thanks for your reply. Indeed, setting the duration to 10 seconds (as shown in REF below) does achieve this effect (i.e. increase the precision to 60.4). However, frequency measurements are captured every 10s.

    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-18 10:12
    Everything in PBASIC is done with integer arithmetic. There simply is not a way to measure a fractional frequency with the COUNT command. For low frequencies, such as 60 Hz, you could measure the period instead using the PULSIN command. Be sure to measure both the positive half-cycle and negative half-cycle separately, add them, then divide into a large constant to get the frequency.

    -Phil
  • SydSyd Posts: 13
    edited 2011-11-18 12:23
    Hi 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
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-11-18 13:07
    You've got some bad truncation happening. One cycle of 60.4 Hz is 16556 µs. 62500 / 16556 = 3, and 3 * 16 = 48.

    See Tracy Allen's long division routine for a way around the problem:

    Also, if you don't mind:

    attachment.php?attachmentid=78421&d=1297987572

    -Phil
  • cgxcgx Posts: 4
    edited 2011-11-18 16:00
    You have to put the basic stamp ship in the microwave for 30 minutes, in that way will reboot the internal BIOS commands. When you are done plug it up to the computer and is ready to read and accept the new commands.

    Any doubts please just posted in the wall.

  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-11-18 16:39
    Syd,
    Try this,
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    
    N VAR WORD
    D VAR WORD
    F VAR WORD
    J VAR NIB
    
    N= 10000    
    D = 16556  ' microseconds period
    
    '---------binary division loop-----------
    ' enter with N < D < 32768   for valid answer
       FOR J=15 TO 0               ' 16 bits 
          N = N//D<<1                   ' remainder*2 
          F.BIT0(J)=N/D               ' next bit 
       NEXT
    ' now have f = 65536 * N/D in one word
       f = f ** 10000   ' normalize to decimal fraction
    ' now have f = 10000 * N/D, units of Hz*100
       DEBUG CR, "Frequency= ",DEC f/100,".",DEC2 f," Hz"
    

    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?)
Sign In or Register to comment.