Try Embedded

Reading datasheets and schematics

Where the answers are, min/typ/max, calculating from the tables and reading register maps.

The datasheet is the specification

Every chip you use comes with a datasheet, and microcontrollers add a reference manual of a thousand pages or more. Nobody reads them cover to cover. The skill is knowing where the answer is and how to read it precisely, because firmware that works "on my board" but breaks a datasheet limit fails later, on someone else's.

What a datasheet contains, and what you need first

SectionWhat it answersRead it when
Features / overviewIs this the right part at all?Choosing a part
Pinout and pin descriptionWhich pin does what; which are 5 V tolerantDesigning the board, writing pin setup
Absolute maximum ratingsLimits that damage the partAlways: never design near them
Recommended operating conditionsThe range it is guaranteed to work inChoosing supply voltage and clock
Electrical characteristicsCurrents, voltage thresholds, timing, with min/typ/maxChecking interfaces and power
Timing diagramsSetup, hold, clock-to-data, delaysWriting a driver or bit-banging
Register mapEach register's address, fields and reset valueWriting the driver
Application informationA reference circuit that worksDesigning the board
Errata (separate document)Known silicon bugs and workaroundsBefore debugging a "driver bug"

Min, typ and max

Electrical tables give three columns. Design to min and max, never to typ. "Typical" is what an average part does at 25 °C; your thousandth board in a cold car will not be average.

For example, a microcontroller pin might list:

ParameterMinTypMaxUnit
V_IH, input high voltage0.7 × VDDV
V_IL, input low voltage0.3 × VDDV
V_OH, output high at 8 mAVDD − 0.4V
I_IO, current per pin20mA

So at VDD = 3.3 V, an input is only guaranteed to read high above 2.31 V, and a 5 V device's "high" of 3.5 V min is enough, but a 5 V device that needs 0.7 × 5 = 3.5 V to see high may not reliably read this chip's 3.3 V output. That one row decides whether two chips can talk without a level shifter.

Calculating from the tables

Datasheet numbers turn into component values and register settings. Two everyday examples:

#include <stdio.h>

int main(void) {
  /* 1. An LED's resistor: from the LED datasheet, Vf = 2.0 V at 10 mA; driven from a 3.3 V pin. */
  double vdd = 3.3, vf = 2.0, i_led = 0.010;
  double r = (vdd - vf) / i_led;
  printf("LED resistor: %.0f ohm (use the next standard value up: 150 ohm)\n", r);

  /* 2. I2C pull-ups: the bus must rise to 0.7 x VDD within the rise time limit.
     I2C fast mode allows tr = 300 ns; bus capacitance about 100 pF.
     The rise to 70 % takes about 0.8473 x R x C, so R_max = tr / (0.8473 x C). */
  double tr = 300e-9, cbus = 100e-12;
  double r_max = tr / (0.8473 * cbus);
  /* The minimum comes from the 3 mA a device must sink while holding the line at 0.4 V. */
  double r_min = (vdd - 0.4) / 0.003;
  printf("I2C pull-up between %.0f and %.0f ohm (2.2 k is a common choice)\n", r_min, r_max);
  return 0;
}

Reading a register description

A register map entry looks like this (a timer control register, simplified):

BitsNameAccessResetDescription
31:8--0Reserved, keep at reset value
7ARPErw0Auto-reload preload enable
6:5CMSrw00Centre-aligned mode select
4DIRrw0Direction: 0 up, 1 down
3OPMrw0One-pulse mode
0CENrw0Counter enable

Read every column:

  • Bits: 6:5 is a two-bit field; write it with a mask and shift, never by setting bits one at a time.
  • Access: rw (read-write), r (read-only), w (write-only, reads as 0), rc_w1 (cleared by writing 1: write 0 to the other bits, or you clear flags by accident).
  • Reset: the value after reset. Your init code should not assume anything else.
  • Reserved: keep at the reset value, so use read-modify-write, not a plain write of your own bits.

Look for the notes too: "this field can only be written when CEN = 0", or "the flag is set again if the condition persists". They explain most "the register ignores my write" bugs.

Schematics, briefly

A firmware engineer reads the board's schematic to find out: which MCU pin goes to which device, with which pull-ups or pull-downs; whether a line is active-low (a bar over the name, or a trailing N or #: RESET#, nCS); the supply each device runs from; and what default state a pin must have at reset so nothing dangerous happens before the firmware starts.

Pitfall: Check the errata sheet before you spend a day on a "driver bug". Peripherals do have silicon bugs, and the workaround is usually listed.

Interview question: How do you approach a new microcontroller peripheral you have never used?

Answer: I read the reference manual's chapter overview and block diagram first to understand how it works, then the register map, noting reset values, access types and any ordering rules, such as enable the clock first or configure only while disabled. I check the datasheet for which pins and alternate functions it uses and its electrical limits, and the errata for known bugs. Then I write the smallest test, for example one transfer, and verify it with a logic analyzer, before building the full driver. Vendor examples are useful to compare against, but I make sure I understand each line.

Build it: read the fuel gauge from its datasheet

This is step 4, the last of the module project. The improved power bank has a dedicated fuel-gauge chip that tracks the cell's charge far better than a voltage table. Everything you need is in its datasheet's register table, below, written in the style of the real document.

Lab: Convert fuel-gauge registers using the datasheet

Register summary (all registers are 16 bits, read most significant byte first):

AddressNameDescription
0x02VCELLCell voltage. Unsigned. 78.125 µV/LSB
0x04SOCState of charge. High byte: whole %, low byte: 1/256 %
0x08VERSIONProduction version; reads 0x001x
0x16CRATECharge or discharge rate. Signed two's complement. 0.208 %/hr per LSB; negative while discharging

Write the conversions; the raw 16-bit values come already combined from the bus:

  • vcell_mv(raw) returns millivolts, rounded down: raw * 78125 / 1000000.
  • soc_tenths(raw) returns the charge in tenths of a percent, rounded down: raw * 10 / 256.
  • crate_milli_pct_hr(raw) returns the rate in thousandths of a percent per hour: the signed value times 208.
  • version_ok(raw) returns true when the version is 0x0010 to 0x001F.
  • hours_left(soc_raw, crate_raw) returns the whole hours until empty while discharging: charge divided by the discharge rate, using the two conversions above. It returns -1 when not discharging (rate 0 or positive).

Going deeper

Interview question: You must drive a new relay module, LCD backpack and clock chip from their datasheets. What do you look for first?

Answer: Supply and logic levels, the absolute maximum ratings, pinouts, the trigger level of the relay input, the I2C addresses and timing, power-up behaviour and initialisation sequences, and the register maps for the clock. Then the application notes and errata, which often hold what the main datasheet leaves out.

Interview question: What is the difference between absolute maximum ratings and recommended operating conditions?

Answer: Absolute maximum ratings are the limits beyond which the part may be permanently damaged; operating at them is not guaranteed to work, only not to destroy it immediately, and staying near them hurts reliability. Recommended operating conditions are the range in which the electrical characteristics are guaranteed. A design must stay within the recommended conditions with margin, and treat absolute maximum as a line never to approach, including during transients and power-up.

Interview question: Why do you design with minimum and maximum values rather than typical ones?

Answer: Typical values describe an average part at room temperature and nominal supply. Production parts vary from batch to batch, with temperature, supply voltage and ageing, and the datasheet only guarantees the min and max. A design that relies on typical values works on the prototype and fails on a fraction of production units or in the field. Worst-case design means checking every interface, timing and power figure at the limits, sometimes with extra margin.

Explain it: In your own words, explain how to read a register description in a reference manual before writing a driver.

Answer: For each register I note its address and reset value, then go through every field: which bits it occupies, so I know the mask and shift, what each value means, and its access type, such as read-write, read-only, or cleared by writing 1, because that changes how I write it. I keep reserved bits at their reset value by using read-modify-write. Then I read the notes for rules like fields that can only be changed while the peripheral is disabled, and the order in which registers must be set.