Skip to content

Instantly share code, notes, and snippets.

@rogersguedes
Created December 14, 2020 20:01
Show Gist options
  • Save rogersguedes/23a4c2297177af807763b10a36f00a1d to your computer and use it in GitHub Desktop.
Save rogersguedes/23a4c2297177af807763b10a36f00a1d to your computer and use it in GitHub Desktop.
STM32 G.P. Timer Prescaler and Period Calc
#!/usr/bin/env python
timerBusClkFreq=216000000
timerDesiredIntFreq=5000
#counts=43200
counts=timerBusClkFreq/timerDesiredIntFreq
prescaler=0
period=0
#counts = (prescaler + 1)*(period + 1)
prescalerMax = counts - 1
periodMax = counts - 1
print "prescalerMax: ", prescalerMax
print "periodMax: ", periodMax
minDiffDelta=prescalerMax
for prescaler in range(0, counts):
for period in range(0, counts):
curCount = (prescaler + 1)*(period + 1)
delta = abs(prescaler - period)
if curCount > counts:
break
if curCount == counts:
print "prescaler: ", prescaler,
print "period: ", period,
print "delta: ", delta,
if delta < minDiffDelta:
minDiffDelta = delta
print "<"
else:
print ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment