Skip to content

Instantly share code, notes, and snippets.

@tanakamasayuki
Created July 11, 2025 10:30
Show Gist options
  • Save tanakamasayuki/1d4416fcf6f3aaf5a14e1f03cafc5906 to your computer and use it in GitHub Desktop.
Save tanakamasayuki/1d4416fcf6f3aaf5a14e1f03cafc5906 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>ESP32 PWM パラメータ計算</title>
<style>
body {
font-family: sans-serif;
margin: 2em;
}
label {
display: block;
margin: 0.5em 0 0.2em;
}
input {
padding: 0.3em;
width: 100px;
}
#result {
margin-top: 1em;
font-weight: bold;
}
canvas {
margin-top: 1em;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<h1>ESP32 PWM パラメータ計算</h1>
<label for="freq">周波数 (Hz):</label>
<input type="number" id="freq" value="50">
<label for="bits">ビット数:</label>
<input type="number" id="bits" value="10" min="1" max="20">
<label for="dutyVal">デューティ値 (0 ~ 2^ビット数):</label>
<input type="number" id="dutyVal" value="77" min="0">
<div id="result"></div>
<canvas id="pwmCanvas" width="500" height="100"></canvas>
<script>
const freqInput = document.getElementById('freq');
const bitsInput = document.getElementById('bits');
const dutyValInput = document.getElementById('dutyVal');
const resultDiv = document.getElementById('result');
const canvas = document.getElementById('pwmCanvas');
const ctx = canvas.getContext('2d');
function calculatePWM() {
const freq = parseFloat(freqInput.value);
const bits = parseInt(bitsInput.value);
const dutyVal = parseInt(dutyValInput.value);
const maxDuty = Math.pow(2, bits);
if (isNaN(freq) || freq <= 0 || isNaN(bits) || bits < 1 || isNaN(dutyVal) || dutyVal < 0 || dutyVal > maxDuty) {
resultDiv.textContent = '正しい値を入力してください。';
return;
}
const period_us = 1_000_000 / freq;
const dutyRatio = dutyVal / maxDuty;
const high_us = period_us * dutyRatio;
const low_us = period_us - high_us;
resultDiv.innerHTML = `
最大デューティ値: ${maxDuty}<br>
デューティ比: ${(dutyRatio * 100).toFixed(2)} %<br>
周期: ${period_us.toFixed(2)} μs<br>
HIGH時間: ${high_us.toFixed(2)} μs<br>
LOW時間: ${low_us.toFixed(2)} μs
`;
drawPWM(dutyRatio);
}
function drawPWM(dutyRatio) {
const width = canvas.width;
const height = canvas.height;
const highWidth = width * dutyRatio;
const lowWidth = width - highWidth;
// クリア
ctx.clearRect(0, 0, width, height);
// HIGH (上のバー)
ctx.fillStyle = '#FF0000'; // 赤
ctx.fillRect(0, 20, highWidth, 30);
// LOW (下のバー)
ctx.fillStyle = '#DDDDDD'; // 灰
ctx.fillRect(highWidth, 20, lowWidth, 30);
ctx.fillStyle = '#FF0000'; // 赤
ctx.fillRect(highWidth, 49, lowWidth, 1);
}
freqInput.addEventListener('input', calculatePWM);
bitsInput.addEventListener('input', () => {
const bits = parseInt(bitsInput.value);
if (!isNaN(bits)) {
const maxDuty = Math.pow(2, bits);
dutyValInput.max = maxDuty;
}
calculatePWM();
});
dutyValInput.addEventListener('input', calculatePWM);
calculatePWM(); // 初期表示
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment