PostHeaderIcon ATtiny261: The Calibration Constants Are the Product

Most small microcontrollers are chosen for their pins or their price. This one is usually chosen for its converter: a ten-bit successive-approximation unit with a differential input stage and a selectable gain ahead of it, which lets a part that costs very little measure a bridge, a thermocouple or a shunt without any external amplifier.

That has a direct consequence for a recovery. The code in such a device is mostly plumbing — timer, serial port, a state machine — and the part that a competitor or a repair engineer actually wants is a small set of constants that convert raw counts into physical units. Those constants are not a secret algorithm. They are the product.

What the converter offers

The array is 2 KB, the SRAM and EEPROM are 128 bytes each, and the package has twenty pins. Within that budget the analogue side is unusually complete: eleven single-ended channels, differential pairs selected by the input multiplexer, two gain settings ahead of the converter, and an internal reference selectable in software.

feature setting where it shows in the image
input selection single-ended or differential pair channel bits written to the multiplexer register
gain unity or a high fixed gain the same register, adjacent bits
reference internal or an external pin reference bits written during initialisation
conversion mode single shot or free running the enable and start bits in the control register

The differential gain setting is the one worth understanding. A bridge output is a few millivolts sitting on a common-mode level of half the excitation, and unity gain would spend most of the converter’s range on the offset. The gain stage multiplies the difference before conversion, which is why a design can resolve microvolts with a ten-bit converter and why the same converter is useless for that job without it.

How a conversion sequence reads in assembly

The register-level pattern is short and repetitive: select the channel, start a conversion, wait for the completion flag, read the result register pair, and either use the value or accumulate it.

Two details distinguish a careful design from a rushed one, and both are visible after the fact. The first is whether the code discards the first conversion after a channel change. The sample-and-hold circuit needs time to settle when the multiplexer moves, and the first result after a switch is systematically wrong by an amount that depends on the source impedance. Designs that discard it read cleanly; designs that do not have a fixed offset on every channel change.

The second is whether the reference is allowed to settle. Switching the reference and converting immediately produces a first reading that is low, and on a device that samples once per second that error is invisible in testing and present in every unit.

The constants that carry the model

After the raw counts come the arithmetic. A conversion is turned into a physical value by an expression of the form offset plus gain times the count, sometimes with a second-order correction term, and on a part this small the arithmetic is fixed point because there is no floating-point unit and no room for a library.

That makes the constants readable. A sixteen-bit gain term, a signed offset, a shift count that implements the division — those three values state the sensor’s sensitivity, its zero point and the scaling the designer chose. For a bridge sensor the gain term is proportional to the excitation voltage and the gauge factor; for a thermocouple it corresponds to the Seebeck coefficient over the reference junction range; for a current shunt it is the reciprocal of the shunt resistance.

A disassembly that labels those constants is therefore more useful than one that labels the control flow, because the control flow is generic and the constants are specific to the product.

Averaging, and what it says about the noise budget

A single conversion on this converter has a noise floor set by the reference and the sample time. Designs that need better resolution do not change hardware; they average. The number of samples averaged appears in the image as a loop count, and the loop count states the designer’s noise budget directly.

Four samples buys one bit. Sixteen buys two. A design averaging sixty-four conversions per reading is telling you that it needed three more bits than the converter could give and was willing to spend the time. That number also predicts the sample rate, which is often the specification the customer cares about and never has documentation for.

samples averaged resolution gained typical use
1 none monitoring a supply rail
4 1 bit a slow temperature reading
16 2 bits a bridge with moderate precision
64 or more 3 bits or more a scale or a precision instrument

What the deliverable contains

For a code-level recovery the return set is the flash image, the EEPROM as a separate file, the fuse and lock state as read, a disassembly with the converter configuration annotated, and — the part customers use — a table of the calibration constants with the arithmetic they feed, written out as an equation that reproduces the device’s output from a raw count.

That last item is often the whole engagement. A customer with a failed unit and no source code does not need the state machine; they need to know what the number on the display means.

The wider vector table of this part’s larger sibling is covered in ATtiny261 vector table notes, and the marginal-array read problem in ATtiny261V code recovery. For a device where the analogue front end is external and the constants therefore live in a separate part, see ATtiny4313 code recovery.

FAQ

Is the gain stage internal? Yes, it sits ahead of the converter and is selected through the same register that chooses the input pair.

Why discard the first conversion after a channel change? The sample-and-hold needs to settle. The first result after a multiplexer change carries an error proportional to the source impedance.

Can the calibration constants be recovered without the code? They can be measured from the device by applying known inputs, but reading them from the image is faster and exact.

Conclusion

On a converter-centric part the firmware is mostly scaffolding and the value is in a handful of numbers. Reading the multiplexer configuration says what is measured; reading the averaging count says how precisely; reading the gain and offset constants says what the numbers mean. Those three together reconstruct the product’s measurement model without needing a single line of the original source.

Comments are closed.