Skip to content

Instantly share code, notes, and snippets.

@apla
Created October 24, 2019 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apla/613051a3d2ba99d8da897f67f9f4d984 to your computer and use it in GitHub Desktop.
Save apla/613051a3d2ba99d8da897f67f9f4d984 to your computer and use it in GitHub Desktop.
avr-uart-calc
<!DOCTYPE html>
<html lang="en">
<head>
<script type="module">
</script>
</head>
<body>
<main id="app"></main>
</body>
</html>
// calculators
// http://www.wormfood.net/avrbaudcalc.php - server script, wont't work anymore
// https://trolsoft.ru/en/uart-calc - server script
// http://www.gjlay.de/helferlein/avr-uart-rechner.html - js
//
// TWI bitrate ???
// fuse calc ???
// registers
// http://maxembedded.com/2013/09/the-usart-of-the-avr/
// http://mainloop.ru/avr-atmega/avr-usart-setting.html
// import { h, app } from "https://unpkg.com/hyperapp"
const { h, app } = hyperapp;
/** @jsx h */
const state = {
counter: 0,
text: '',
multiplier: 16,
baudrate: '',
frequency: 16*1000*1000
};
const _actions = {
onBaudInput (evt) {
return state => {
return {baudrate: evt.target.value}
}
},
onFreqInput (evt) {
return state => {
return {frequency: parseInt(evt.target.value)}
}
},
onSpeedModCheck (evt) {
return state => {
return {
multiplier: evt.target.value === "normal"
? 16
: evt.target.value === "double"
? 8
: 2}
}
}
};
function calcUBBRRaw (frequency, baudrate, multiplier) {
return (frequency / ( baudrate * multiplier ) ) - 1
}
function calcUBBR (frequency, baudrate, multiplier) {
return Math.trunc (calcUBBRRaw (frequency, baudrate, multiplier));
}
function calcUBBRError (frequency, baudrate, multiplier) {
const rawUBBR = calcUBBRRaw (frequency, baudrate, multiplier);
/*
const hiUBBR = Math.floor (ubrr);
const loUBBR = Math.ceil (ubrr);
bauda = f/(m*(hiUBBR+1));
baudb = f/(m*(loUBBR+1));
erra = bauda/baud-1;
errb = baudb/baud-1;
if (Math.abs (erra) < Math.abs (errb))
{
err = erra;
ubrr = ubrra;
}
else
{
err = errb;
ubrr = ubrrb;
}
*/
return ((rawUBBR - Math.trunc (rawUBBR) ) / rawUBBR * 100);
}
/*
UBRR out of range // gray
UBRR and clock speed are a perfect match // light green
UBRR and clock speed are less than 1% off // green
UBRR and clock speed are between 1% and 2% off // dark green
UBRR and clock speed are between 2% and 3% off // yellow
UBRR and clock speed are between 3% and 10% off // orange
UBRR and clock speed is over 10% off // red
*/
function calcUBBRColor (frequency, baudrate, multiplier) {
const rawUBBR = calcUBBRRaw (frequency, baudrate, multiplier);
return ((rawUBBR - Math.trunc (rawUBBR) ) / rawUBBR * 100);
}
function renderView (state, actions) {
const baudrates = state.baudrate
? state.baudrate.split(/\s,\s/).map (b => parseInt(b, 10))
: [300, 1200, 9600, 19200, 57600, 115200, 230400, 250000];
return <div class="row">
<div class="column">
{ /* <h1>{state.counter}</h1> */ }
<h3>UBRR = ( FREQ / ( BAUDRATE * { state.multiplier } ) ) - 1</h3>
<div>
<input id="bps" type="text" oninput={evt => actions.onBaudInput(evt)}></input>
<label for="bps">bps</label>
</div>
<div>
<input id="freq" value={state.frequency} type="text" oninput={evt => actions.onFreqInput(evt)}></input>
<label for="freq">Hz</label>
</div>
<div>
<input name="speed-mod" value="normal" id="speed-mod-normal" type="radio" oninput={evt => actions.onSpeedModCheck(evt)} checked={state.multiplier === 16}></input>
<label for="speed-mod-double">normal speed</label>
<input name="speed-mod" value="double" id="speed-mod-double" type="radio" oninput={evt => actions.onSpeedModCheck(evt)} checked={state.multiplier === 8}></input>
<label for="speed-mod-double">double speed</label>
<input name="speed-mod" value="sync" id="speed-mod-sync" type="radio" oninput={evt => actions.onSpeedModCheck(evt)} checked={state.multiplier === 2}></input>
<label for="speed-mod-sync">synchronous</label>
</div>
</div>{ /* column */ }
<div class="column">
<table>
<tr>
<th>Baud rate (bps)</th>
<th>UBBR</th>
<th>Error (%)</th>
</tr>
{baudrates.map (b => <tr>
<td>{ b }</td>
<td>{ calcUBBR (state.frequency, b, state.multiplier) }</td>
<td>{ calcUBBRError (state.frequency, b, state.multiplier) }</td>
</tr>)}
</table>
</div>
</div>;
}
app(state, _actions, renderView, document.querySelector ('#app'));
<script src="https://unpkg.com/hyperapp@1.x.x"></script>
.row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
width: 100%;
}
.column {
display: flex;
flex-direction: column;
flex-basis: 100%;
flex: 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment