General Counter Calculations

Calulation about VHDL Counters


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)


Requested Period       : 0.5 s
Requested Frequence    : 2 Hz
Counter Step           : 1 increment
Should count to        : 2.5e+07 counts
Result Period          : 0.5 s
Result Frequence       : 2 Hz
Required Bits          : 26 bits
Count to               : 2.5e+07 counts
Error                  : 0 %
-------------------------------------
SysClk                 : 5e+07 Hz
Counter Bit Nb         : 26 Bits
Counter Stepssize      : + 1
Count max value (int)  : 2.5e+07
Count max value (hex)  : 0x17D7840

KS412S HIB CPLD

Test Counter


In [6]:
calcCounter(freq=2, sys_freq=50e6, step=1)


Requested Period       : 0.5 s
Requested Frequence    : 2 Hz
Counter Step           : 1 increment
Should count to        : 2.5e+07 counts
Result Period          : 0.5 s
Result Frequence       : 2 Hz
Required Bits          : 26 bits
Count to               : 2.5e+07 counts
Error                  : 0 %
-------------------------------------
SysClk                 : 5e+07 Hz
Counter Bit Nb         : 26 Bits
Counter Stepssize      : + 1
Count max value (int)  : 2.5e+07
Count max value (hex)  : 0x17D7840

Motor Reset Counter


In [3]:
calcCounter(1e6, 50e6, 1)


Requested Period       : 1e-06 s
Requested Frequence    : 1e+06 Hz
Counter Step           : 1 increment
Should count to        : 50 counts
Result Period          : 1e-06 s
Result Frequence       : 1e+06 Hz
Required Bits          : 7 bits
Count to               : 50 counts
Error                  : 0 %
-------------------------------------
SysClk                 : 5e+07 Hz
Counter Bit Nb         : 7 Bits
Counter Stepssize      : + 1
Count max value (int)  : 50
Count max value (hex)  : 0x32

Jetmapping

Hw_Encoder Pulse


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

SCLK Counter Xaar


In [10]:
calcCounter(1e6, 100e6, 1)
unsigned_num_bits(128)


Requested Period       : 1e-06 s
Requested Frequence    : 1e+06 Hz
Counter Step           : 1 increment
Should count to        : 100 counts
Result Period          : 1e-06 s
Result Frequence       : 1e+06 Hz
Required Bits          : 8 bits
Count to               : 100 counts
Error                  : 0 %
-------------------------------------
SysClk                 : 1e+08 Hz
Counter Bit Nb         : 8 Bits
Counter Stepssize      : + 1
Count max value (int)  : 100
Count max value (hex)  : 0x64
Out[10]:
8