I use 'X2' pin for measure the frequency, I have a square signal input on the pin. The signal frequency is 1hz. I use a µs timer. I use ExtInt for detect a rising edge and count time between two rising edge.
Frequency = 1 / Period
Frequency in Hz
Period in second
Well, my period is in µs
So, Frequency = 1000000 / period
I have a problem, my counter detect 1200 µs between two rising edge and it normally should detect 1000000 µs
There is my code :
main.py
Code: Select all
import pyb
import test
blueled=pyb.LED(4)
blueled.on()
test.init()
while True:
test.measure()
pyb.delay(1000)
Code: Select all
import pyb
pinFreq = None
periodTimer = None
micros = None
freq = 0
counter = 0
def callback(line):
global micros
global freq
global counter
print("line =", line)
counter = micros.counter()
freq = 1000000 / counter
def init(timer_id = 2, data_pin = 'Y2'):
global pinFreq
global periodTimer
global micros
pinFreq = pyb.Pin(data_pin)
pinFreq.init(pyb.Pin.IN, pyb.Pin.PULL_UP)
periodTimer = timer_id
micros = pyb.Timer(periodTimer, prescaler=83, period=0x3fffffff) # 1MHz ~ 1uS
pyb.ExtInt(pinFreq, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_UP, callback('toto'))
def measure():
global freq
global counter
print("counter : " + str(counter))
print("freq : " + str(freq) + "Hz")