In [9]:
import numpy as np
import pylab as pl
# User settings
freq = 2 # in [Hz]
sys_freq = 50e6 # in [Hz]
step = 1 # counter increment
# Calculation
def unsigned_num_bits(num):
_nbits = 1
_n = num
while(_n > 1):
_nbits = _nbits + 1
_n = _n / 2
return _nbits
def calcCounter(freq=2, sys_freq=50e6, step=1):
""" freq in [Hz]
sys_freq in [Hz]
step as [integer]
"""
do = True
period = (1.0/freq)
sys_period = (1.0/sys_freq)
count = (period/sys_period)/step
if (period < sys_period):
print("Requested period smaller than system period")
do = False
if (count < 1):
print("Too small steps not possible")
do = False
count_int = int(count)
period_int = (count_int*sys_period)*step
freq_int = 1/period_int
bitNb = unsigned_num_bits(count_int)
error = (100*freq_int)/freq-100
if do:
# Output Values
print("Requested Period : {:g} s".format(period))
print("Requested Frequence : {:g} Hz".format(freq))
print("Counter Step : {:g} increment".format(step))
print("Should count to : {:g} counts".format(count))
print("Result Period : {:g} s".format(period_int))
print("Result Frequence : {:g} Hz".format(freq_int))
print("Required Bits : {:g} bits".format(bitNb))
print("Count to : {:g} counts".format(count_int))
print("Error : {:g} %".format(error))
print("-------------------------------------")
print("SysClk : {:g} Hz".format(sys_freq))
print("Counter Bit Nb : {:g} Bits".format(bitNb))
print("Counter Stepssize : + {:g}".format(step))
print("Count max value (int) : {:g}".format(count_int))
print("Count max value (hex) : 0x{:X}".format(count_int))
#if False:
# ## Plot
# init = 0
# time = np.linspace(init, (count_int*sys_period), count_int*2) # 1sec in ns steps
# counter = np.linspace(init,numpy.mod(count_int*2,count_int),count_int*2)
# # Plot graph
# pl.plot(time,counter,label="Counter")
# #
# ## Place legend, Axis and Title
# pl.legend(loc='best')
# pl.xlabel("time [s]")
# pl.ylabel("Counter Value [int]")
# pl.title("Counter Waveform")
calcCounter(freq, sys_freq, step)
In [6]:
calcCounter(freq=2, sys_freq=50e6, step=1)
In [3]:
calcCounter(1e6, 50e6, 1)
In [4]:
import numpy as np
import pylab as pl
# User settings
incr_quadenc = 500e3 # in [Hz]
step = 26214 # steps per increment
maxval = pow(2,16)-1 # 2^16-1
cntbits = 48
In [10]:
calcCounter(1e6, 100e6, 1)
unsigned_num_bits(128)
Out[10]: