Simvik

Decode a scaled signal

Turn a raw signal into a physical value with sign, factor and offset. A medium embedded C problem (CAN), run and graded in the browser.

medium · CAN · can · fixed-point · sign-extension

A raw CAN signal becomes a physical value with value = raw × factor + offset, where the raw value may be signed (two's complement in length bits).

Write decode_milli(raw, length, is_signed, factor_milli, offset_milli): raw holds the signal in its low length bits; return the value in thousandths of the unit (factor and offset are also given in thousandths), using integer arithmetic.

For example, a battery current in 12 signed bits with factor 0.1 A: raw 0xFF6 is −10, so −1.0 A, returned as −1000.

Starter code

int32_t decode_milli(uint32_t raw, int length, bool is_signed, int32_t factor_milli, int32_t offset_milli) {
  (void)length; (void)is_signed;
  return (int32_t)raw * factor_milli + offset_milli;
}

Example tests

Unsigned with an offset

show(130, 8, false, 1000, -40000);   /* coolant: 1 C per bit, -40 C offset */

Expected output:

raw 0x82 -> 90.000

A positive current

show(0x064, 12, true, 100, 0);

Expected output:

raw 0x64 -> 10.000

Learn it first

  • CAN · Arbitration, bit timing, frames, CAN FD and error handling.

More CAN problems