Shop OBEX P1 Docs P2 Docs Learn Events
Counting frequencies. — Parallax Forums

Counting frequencies.

Steve in NMSteve in NM Posts: 54
edited 2008-11-09 15:23 in General Discussion
EDIT: I get a difference of almost 20% between the code in Experiment #17 and Experiment #18. Can anyone tell me why?
===============================================================================

This is very slightly modified code, right out of Stampworks Experiment #18. Unmodified it works fine to display

100 Hz or
10 Hz or
1 Hz.

Problem is, I want to see more (100.nnn Hz). So I made this modification:

' This program monitors and displays the frequency of a signal on Pin 0.
'
' I/O Definitions
'
FreqPin CON 0 ' frequency input pin
'
' Constants
'
Convert CON $0200 ' input to uSeconds (BS2)
' Convert CON $00CC ' BS2sx
' Convert CON $00C0 ' BS2p
'
' Variables
'
pHigh VAR Word ' high pulse width
pLow VAR Word ' low pulse width
period VAR Word ' cycle time (high + low)
freqH VAR Word ' frequency
freqL VAR Word ' frequency
'
' Program Code
'
Main:
PULSIN FreqPin, 0, pHigh ' get high portion of input
PULSIN FreqPin, 1, pLow ' get low portion of input
period = (pHigh + pLow) */ Convert ' calculate cycle width in uSecs
freqH = 50000 / period * 10 ' calculate frequency
freqL = 50000 // period * 10 ' calculate frequency
' display on DEBUG screen
DEBUG HOME
DEBUG "Period...... ", DEC period, " uS ", CR
DEBUG "Frequency... ", DEC freqH , ".", DEC freqL, " Hz "
PAUSE 200
GOTO Main ' do it again
END

When I check the result against a calculator, it looks close but not spot on. Is this the correct way to do this?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Post Edited (Steve in NM) : 11/9/2008 5:21:17 PM GMT
Sign In or Register to comment.