-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
289 lines (255 loc) · 15.6 KB
/
index.html
File metadata and controls
289 lines (255 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculadora de Timers & Interrupções</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<!-- Configuração do MathJax para aceitar cifrão simples para fórmulas inline -->
<script>
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
}
};
</script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<style>
.card {
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
transition: all 0.3s ease;
}
input:focus {
outline: none;
border-color: #3b82f6;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}
.tab-active {
border-bottom: 2px solid #3b82f6;
color: #3b82f6;
}
</style>
</head>
<body class="bg-slate-50 text-slate-900 font-sans min-h-screen">
<div class="max-w-4xl mx-auto p-4 md:p-8">
<!-- Banner de Programação Eletrônica -->
<div class="mb-8 overflow-hidden rounded-xl shadow-md border border-slate-200">
<img src="https://raw.githubusercontent.com/programacao-eletronica/img/master/banner.png"
alt="[Banner Programação Eletrônica]"
class="w-full h-auto block"
onerror="this.style.display='none'">
</div>
<!-- Header -->
<header class="text-center mb-10">
<h1 class="text-3xl font-bold text-slate-800 mb-2">Calculadora de Timers</h1>
<p class="text-slate-600">Projete suas interrupções para microcontroladores (STM32, AVR, PIC, ESP32)</p>
</header>
<!-- Main App Card -->
<div class="card rounded-2xl shadow-xl overflow-hidden border border-slate-200 mb-12">
<!-- Tabs -->
<div class="flex border-b border-slate-200 bg-slate-100">
<button onclick="setMode('freq')" id="tab-freq" class="flex-1 py-4 font-semibold text-sm uppercase tracking-wider tab-active transition-colors">
Calcular Frequência
</button>
<button onclick="setMode('count')" id="tab-count" class="flex-1 py-4 font-semibold text-sm uppercase tracking-wider text-slate-500 hover:text-slate-700 transition-colors">
Calcular Contagem (ARR)
</button>
</div>
<div class="p-6 md:p-10">
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<!-- Inputs -->
<div class="space-y-6">
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Clock do Periférico (f<sub>clk</sub>)</label>
<div class="relative">
<input type="number" id="fclk" value="100" step="any" oninput="calculate()" class="w-full px-4 py-2 border border-slate-300 rounded-lg">
<select id="fclk-unit" onchange="calculate()" class="absolute right-2 top-2 bg-transparent font-semibold text-blue-600 outline-none">
<option value="1000000">MHz</option>
<option value="1000">kHz</option>
<option value="1">Hz</option>
</select>
</div>
</div>
<div>
<label class="block text-sm font-medium text-slate-700 mb-1">Prescaler (PSC)</label>
<input type="number" id="psc" value="64" step="1" min="0" oninput="calculate()" class="w-full px-4 py-2 border border-slate-300 rounded-lg">
<p class="text-xs text-slate-500 mt-1">Valor real no registrador.</p>
</div>
<div id="input-variable-container">
<label id="variable-label" class="block text-sm font-medium text-slate-700 mb-1">Contagem / Period (ARR)</label>
<input type="number" id="variable-input" value="500" step="1" min="0" oninput="calculate()" class="w-full px-4 py-2 border border-slate-300 rounded-lg">
<p id="variable-hint" class="text-xs text-slate-500 mt-1">Valor de estouro do contador.</p>
</div>
</div>
<!-- Result Display -->
<div class="bg-blue-50 rounded-xl p-8 flex flex-col items-center justify-center text-center border border-blue-100">
<span id="result-label" class="text-blue-600 font-semibold uppercase text-xs tracking-widest mb-2">Frequência Resultante</span>
<div id="result-value" class="text-5xl font-black text-blue-900 mb-2">---</div>
<span id="result-unit" class="text-xl font-bold text-blue-700">Hz</span>
<div class="mt-6 pt-6 border-t border-blue-200 w-full space-y-2">
<div id="extra-result-container" class="hidden flex justify-between text-sm text-blue-800">
<span>Freq. Real:</span>
<span id="real-freq-value" class="font-mono font-bold">---</span>
</div>
<div class="flex justify-between text-sm text-blue-800">
<span>Período:</span>
<span id="period-value" class="font-mono font-bold">---</span>
</div>
<div id="warning-box" class="mt-4 hidden p-2 bg-amber-100 text-amber-800 text-xs rounded border border-amber-200">
⚠️ Valor excede 16 bits (65535)!
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Theory Section -->
<section class="bg-white rounded-2xl p-8 shadow-sm border border-slate-200 mb-12">
<h2 class="text-2xl font-bold text-slate-800 mb-6 border-b pb-4">Teoria e Fundamentos</h2>
<div class="prose prose-slate max-w-none space-y-6">
<p>
A maioria dos microcontroladores modernos utiliza uma arquitetura de timer onde o sinal de clock passa por um divisor inicial (Prescaler) e depois incrementa um contador até atingir um valor limite (Auto-Reload Register).
</p>
<div class="bg-slate-50 p-4 rounded-lg overflow-x-auto text-center">
<p class="mb-4 font-semibold text-slate-700 italic">Fórmula Geral:</p>
$$f_{int} = \frac{f_{clk}}{(PSC + 1) \times (ARR + 1)}$$
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<h3 class="font-bold text-lg text-slate-700 mb-2">Os Parâmetros</h3>
<ul class="list-disc ml-5 space-y-2 text-slate-600">
<li><strong>$f_{clk}$</strong>: Frequência do sinal que entra no periférico.</li>
<li><strong>$PSC$ (Prescaler)</strong>: Reduz a frequência do clock. O hardware soma 1 ao valor configurado.</li>
<li><strong>$ARR$ (Contagem)</strong>: Define o número de pulsos antes do estouro. Também soma-se 1 para o cálculo total.</li>
</ul>
</div>
<div>
<h3 class="font-bold text-lg text-slate-700 mb-2">Por que somar 1?</h3>
<p class="text-slate-600 text-sm">
O hardware conta de $0$ até o valor definido. Se você define o $ARR$ como $999$, o timer contará $1000$ vezes ($0, 1, 2, ..., 999$). O mesmo princípio se aplica ao divisor do Prescaler.
</p>
</div>
</div>
<div class="mt-6 border-t pt-6">
<h3 class="font-bold text-lg text-slate-700 mb-2">A Importância da Frequência Real</h3>
<p class="text-slate-600 text-sm">
Como o registrador <strong>ARR</strong> só aceita valores inteiros, o valor calculado frequentemente precisa ser arredondado. Isso introduz um pequeno erro na frequência final. Ferramentas de precisão devem sempre verificar a "Frequência Real" atingida com o valor arredondado.
</p>
</div>
</div>
</section>
<!-- Footer -->
<footer class="text-slate-600 py-8 border-t border-slate-200">
<div class="flex flex-col items-center space-y-4">
<p class="font-medium text-slate-800">Desenvolvido por <strong>Wenderson Oliveira</strong></p>
<div class="flex items-center space-x-6">
<!-- GitHub Link -->
<a href="https://github.com/wnoliveira" target="_blank" class="flex items-center space-x-2 hover:text-blue-600 transition-colors">
<svg class="w-6 h-6 fill-current" viewBox="0 0 24 24"><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.43.372.823 1.102.823 2.222 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg>
<span class="text-sm font-semibold">wnoliveira</span>
</a>
<!-- Licença Simples -->
<div class="text-[11px] font-bold uppercase tracking-wide text-slate-500 bg-slate-100 px-3 py-1 rounded-full border border-slate-200">
Licença: Creative Commons
</div>
</div>
<p class="text-xs">
O código fonte desta página está disponível no
<a href="https://github.com/programacao-eletronica/apps" target="_blank" class="text-blue-600 hover:underline font-semibold italic">GitHub</a>.
</p>
</div>
</footer>
</div>
<script>
let currentMode = 'freq'; // 'freq' ou 'count'
function setMode(mode) {
currentMode = mode;
const tabFreq = document.getElementById('tab-freq');
const tabCount = document.getElementById('tab-count');
const label = document.getElementById('variable-label');
const hint = document.getElementById('variable-hint');
const resultLabel = document.getElementById('result-label');
const resultUnit = document.getElementById('result-unit');
const input = document.getElementById('variable-input');
const extraContainer = document.getElementById('extra-result-container');
if (mode === 'freq') {
tabFreq.classList.add('tab-active');
tabFreq.classList.remove('text-slate-500');
tabCount.classList.remove('tab-active');
tabCount.classList.add('text-slate-500');
label.innerHTML = 'Contagem / Period (ARR)';
hint.innerText = 'Valor de estouro do contador.';
resultLabel.innerText = 'Frequência Resultante';
resultUnit.innerText = 'Hz';
input.value = 500;
extraContainer.classList.add('hidden');
} else {
tabCount.classList.add('tab-active');
tabCount.classList.remove('text-slate-500');
tabFreq.classList.remove('tab-active');
tabFreq.classList.add('text-slate-500');
label.innerHTML = 'Freq. Desejada (f<sub>int</sub>)';
hint.innerText = 'Frequência de interrupção em Hz.';
resultLabel.innerText = 'Valor ARR Sugerido';
resultUnit.innerText = '(Inteiro)';
input.value = 1000;
extraContainer.classList.remove('hidden');
}
calculate();
}
function calculate() {
const fclkInput = document.getElementById('fclk').value;
const fclkUnit = document.getElementById('fclk-unit').value;
const fclk = parseFloat(fclkInput) * parseFloat(fclkUnit);
const psc = parseFloat(document.getElementById('psc').value);
const variable = parseFloat(document.getElementById('variable-input').value);
const warning = document.getElementById('warning-box');
const displayResult = document.getElementById('result-value');
const displayPeriod = document.getElementById('period-value');
const displayRealFreq = document.getElementById('real-freq-value');
if (isNaN(fclk) || isNaN(psc) || isNaN(variable) || (currentMode === 'freq' && variable < 0) || (currentMode === 'count' && variable <= 0)) {
displayResult.innerText = "---";
displayPeriod.innerText = "---";
if(displayRealFreq) displayRealFreq.innerText = "---";
return;
}
if (currentMode === 'freq') {
const arr = variable;
const freq = fclk / ((psc + 1) * (arr + 1));
const period = 1 / freq;
displayResult.innerText = freq.toFixed(2);
formatPeriod(period, displayPeriod);
warning.classList.add('hidden');
} else {
const targetFint = variable;
const idealArr = (fclk / ((psc + 1) * targetFint)) - 1;
const roundedArr = Math.round(idealArr);
displayResult.innerText = roundedArr;
const realFreq = fclk / ((psc + 1) * (roundedArr + 1));
const realPeriod = 1 / realFreq;
displayRealFreq.innerText = realFreq.toFixed(2) + " Hz";
formatPeriod(realPeriod, displayPeriod);
if (roundedArr > 65535) {
warning.innerText = "⚠️ ARR > 65535: Use um Prescaler maior ou Timer 32 bits.";
warning.classList.remove('hidden');
} else if (roundedArr < 0) {
warning.innerText = "⚠️ Freq. impossível: Tente diminuir o Prescaler.";
warning.classList.remove('hidden');
} else {
warning.classList.add('hidden');
}
}
}
function formatPeriod(period, element) {
if (period < 0.001) {
element.innerText = (period * 1000000).toFixed(4) + " µs";
} else {
element.innerText = (period * 1000).toFixed(4) + " ms";
}
}
window.onload = calculate;
</script>
</body>
</html>