AVR timer overflow calculator

Posted

in

by

Tags:











seconds
seconds

Code for Calculations

<form>
  <label for="F_CPU">Main Clock Frequency (F_CPU):</label>
  <input type="number" id="F_CPU" step="0.01" min="0" value="16000000">
  <hr />

  <label for="Tres">Timer Resolution(8bit or 16bit):</label>
  <input type="number" id="Tres" step="8" value="8" min="8" max="16">
  <hr />
  
  <label for="TCNT">Timer Counter Value:</label>
  <input type="number" id="TCNT" step="1" min="0" max="255" value="0">
  <hr />


  <label for="prescaler">Timer Clock Prescaler:</label><br>
  <input type="radio" id="prescaler1" name="prescaler" value="1" checked>
  <label for="prescaler1">1</label><br>
  <input type="radio" id="prescaler8" name="prescaler" value="8">
  <label for="prescaler8">8</label><br>
  <input type="radio" id="prescaler64" name="prescaler" value="64">
  <label for="prescaler64">64</label><br>
  <input type="radio" id="prescaler256" name="prescaler" value="256">
  <label for="prescaler256">256</label><br>
  <input type="radio" id="prescaler1024" name="prescaler" value="1024">
  <label for="prescaler1024">1024</label><br>
  <hr />
  <label for="stepperiod">Timer Step Period:</label>
  <input type="number" id="stepperiod"  readonly>seconds</br>
  <label for="ovfperiod">Overflow Period:</label>
  <input type="number" id="ovfperiod"  readonly>seconds
  <hr />

  <button type="button" onclick="calculateValue()">Calculate Value</button>
  <button type="button" onclick="clearFields()">Clear Values</button>
</form>

<script>
function calculateValue() {
  // Retrieve input values
  var F_CPU = parseFloat(document.getElementById("F_CPU").value);
  var Tres = parseInt(document.getElementById("Tres").value);
  var TCNT = parseInt(document.getElementById("TCNT").value);
  var prescaler = parseFloat(document.querySelector('input[name="prescaler"]:checked').value);

  // Calculate Timer Clock and Step Period
  var Timer_Clock = F_CPU / prescaler;
  var Step_Period = 1 / Timer_Clock;

  // Calculate Overflow Period based on Timer Resolution
  var ovfperiod;
  if (Tres === 8) {
    ovfperiod = (255 - TCNT + 1) * Step_Period;
  } else if (Tres === 16) {
    ovfperiod = Math.pow(2, 16) * Step_Period;
  }

  // Update the Overflow Period input field
  document.getElementById("stepperiod").value = Step_Period;
  document.getElementById("ovfperiod").value = ovfperiod;
}

function clearFields() {
  document.getElementById("F_CPU").value = "";
  document.getElementById("Tres").value = "";
  document.getElementById("ovfperiod").value = "";
}
</script>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *