Shop OBEX P1 Docs P2 Docs Learn Events
Frequency & duty-cycle measurements using a single pin in TAQOZ — Parallax Forums

Frequency & duty-cycle measurements using a single pin in TAQOZ

Peter JakackiPeter Jakacki Posts: 10,193
edited 2020-05-24 05:10 in Propeller 2
Looking at the Reciprocal Counter Demonstration code I figured I should be able to cycle through all 3 modes and report on the frequency and duty cycle.
On my P2, the RTC outputs onto P34 through a 100k resistor so it can be overdriven very easily for other functions, but I can still check the RTC frequency if need be.
After loading my handful of lines in 114 bytes of code, I can select a pin and take a measurement. In TAQOZ, many smartpin operations select and latch the target pin with PIN, and then all operations are performed on that pin until changed. My demo function is called .FREQ (print frequency). I will try it out first on P34.
TAQOZ# 34 PIN .FREQ --- Duty = 49.2% Frequency = 32,757Hz ok
Not quite 32.768kHz, so I will check the P2 clock and RTC on my scope later and measure the actual frequencies.
Now I have P36 and P37 shorted, so I can output a frequency on P37 and measure on P36.
TAQOZ# 37 PIN 1,234,567 HZ ---  ok
TAQOZ# 36 PIN .FREQ --- Duty = 49.9% Frequency = 1,234,577Hz ok
TAQOZ# 37 PIN 100 KHZ ---  ok
TAQOZ# 36 PIN .FREQ --- Duty = 49.9% Frequency = 99,999Hz ok
To vary the duty cycle I will have to try a PWM mode and supply it with 3 parameters, the on-time, frame-time, and divider. So I choose a frame time of 100 so that the on-time represents a percentage. Here we go.
TAQOZ# 37 PIN 15 100 1 PWM ---  ok
TAQOZ# 36 PIN .FREQ --- Duty = 15.0% Frequency = 2,000,000Hz ok
TAQOZ# 37 PIN 35 100 10 PWM ---  ok
TAQOZ# 36 PIN .FREQ --- Duty = 35.0% Frequency = 200,000Hz ok
Cool? Hey?


Here's the code:
--- Set up the pin in frequency counting mode
pub FREQCNT ( clocks mode0..2 -- )
---	calculate & write selected mode
	F 2* %00_10101_0 + WRPIN
	CLKMHZ * WXPIN   %00 WYPIN   H
	;

--- Return with raw frequency measurements from current pin
pub RAWFREQ? ( us -- clocks states periods )
	3 FOR DUP I FREQCNT AKPIN WAITPIN RQPIN SWAP NEXT DROP
	;

--- demo to print the frequency and duty cycle
pub .FREQ
	100,000 RAWFREQ? ( clocks states periods )
---	duty = states * 1_000 / clocks
	SWAP 1,000 4TH */ ." Duty = " 2 .DP ." %"   ( clocks periods )
---	'frequency = periods * sysfreq / clocks
	CLKHZ ROT */ ( GETQY SWAP ) ."  Frequency = " .DECL ." Hz"
	;

Comments

  • jmgjmg Posts: 15,173
    Looking at the Reciprocal Counter Demonstration code I figured I should be able to cycle through all 3 modes and report on the frequency and duty cycle.
    ...
    TAQOZ# 34 PIN .FREQ --- Duty = 49.2% Frequency = 32,757Hz ok
    
    Not quite 32.768kHz, so I will check the P2 clock and RTC on my scope later and measure the actual frequencies.

    That's quite a large 336ppm error ?
    If you already know the frequencies as here, P2 has a 'for X Whole periods capture dT SysCLKs' mode.
    IIRC X can be 16 bits, so ~2 seconds of capture time of 32.768k with a 100MHz SysCLK would resolve to ~5ppb LSB
    A low cost TCXO Osc can be good to +/- 0.5ppm over temperature, and within 1-2ppm after reflow, and < 1ppm / year drift.

  • Removing the fixed "us" value from .FREQ and supplying it externally, we can vary that easily.
    TAQOZ# 2,000,000 .freq --- Duty = 49.3% Frequency = 32,767Hz ok
    TAQOZ# 2,000,000 .FREQ --- Duty = 49.3% Frequency = 32,767Hz ok
    

    These are only quick calculations just to check it out and make sure it was working, but I can improve that easily and use period measurements for low frequencies etc. The duty cycle will probably have a bit to do with the 100k resistor perhaps.
Sign In or Register to comment.