Knowledge Base

Example: OBCB frequency measurement with long time base

The OBCB has a fixed timebase of 1.1 seconds, of which 1 second is normally spent counting. That will give a resolution of 1 count in 1s, or 1Hz. Suppose you need to extend that to say 0.1Hz?

In a regular frequency counter you would use a 10s timebase, which would give you a resolution of 0.1Hz. The other counter, OBCA, has that ability, but not OBCB. What you can do, however, is accumulate counts over several time base cycles. For example, you could use 9 cycles. This would give a total count time of 8 times 1.1S, plus 1 second in the 9th cycle, for a total count period of 9.8s. You then divide the total count by 9.8 to get back to Hz.

;High resolution frequency counting with OBCB

                LaunchTask      HiResFreak
                RunTasksForever

;===== High resolution frequency measurement ============
;A new value is stored in fHRFreq every 9.8s

iHRF            iEQU            0       ;Sensor connected to input 0

fHRFreq:        defFLOAT
bHRFCount       defByte

HiResFreak:
HRF_0:
                YieldTask                       ;Initial sync to Phase A
                OBCB_GoIfB      HRF_0
HRF_1:
                YieldTask                       ;Initial sync to Phase B
                OBCB_GoIfA      HRF_1
HRF_Set2:
                SetMem          bHRFCount,8     ;Initialize (next) count sequence
                OBCB_StartA     iHRF            ;Start count on next A
HRF_2:
                YieldTask                       ;Wait for Phase A
                OBCB_GoIfB      HRF_2
HRF_3:
                YieldTask                       ;Phase A (repeats 8 times)
                OBCB_GoIfA      HRF_3
HRF_4:
                YieldTask                       ;Phase B (repeats 8 times)
                OBCB_GoIfB      HRF_4
                DecMGoIfNZ      bHRFCount,HRF_3 ;Count off A/B cycles
HRF_Set5:
                OBCB_StopB      iHRF            ;Prepare to stop counting after 9th A
HRF_5:
                YieldTask                       ;Wait for end of 9th A
                OBCB_GoIfA      HRF_5

                OBCB_fRdClr     iHRF            ;Get result
                fLoadQ          9.8             ;We've been counting this many seconds
                fSwap
                fDiv                            ;Scale to Hz
                fStore          fHRFreq         ;Save result
                GoTo            HRF_Set2        ;Loop back for the next reading

Notes:

  1. This program has two states at the start to ensure that the very first reading is fully synchronized to the A/B timebase phases, so there will be no spurious 1st reading.