C1252 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 52 · Embedded lab

Sampling, statistics and reports, all in C

A data logger is a small embedded system with every part this course has covered: a timer interrupt sampling a sensor at a steady rate, fixed-point conversion to real units, a ring buffer holding the recent readings, statistics computed without floating point, a text report over the UART, and sleep between samples to save the battery. This lab builds one in C, a part at a time, and the play shows the finished logger running.

  • The logger samples a thermistor 10 times a second from a timer interrupt, converts each reading to tenths of a degree with the lesson 18 table, and keeps the last second's in a ring buffer.
  • Every second the main loop reports the average, minimum and maximum over the UART, then sleeps. The room warms 2 °C a second, so the reports climb: 21.1, 23.1, 25.1 °C on average.
  • The power panel shows about 0.55 mA: asleep nearly all the time. The program is about 1.2 KB.

Predict

The ISR runs 10 times a second and takes about 400 cycles (the conversion's 10 µs wait plus the table lookup). What share of the processor does it use?

Name it

The parts

PartLesson
a timer interrupt at a fixed rate9, 10
the ADC and the thermistor table12, 13
a ring buffer of recent readings20
integer statistics: min, max, rounded average4, 13
formatting numbers as text for the UART19
sleeping between samples29

Integer statistics

For readings in tenths of a degree, the average rounded to the nearest tenth is (sum + N/2) / N, with the sum in a type wide enough for N readings. Min and max start at the first reading (never at 0, which may be lower than every reading).

Formatting tenths

Split v into its whole part v / 10 and its tenth v % 10, print the whole part's digits (most significant first, from a reversed % 10 loop), then a point and the tenth. Handle the sign first, since % of a negative number is negative in C.

Sharing data with the ISR

The ISR writes ring and count; the report reads them. A report taken while a sample is half-written could mix old and new values; this logger reports right after a sample and long before the next, so it's safe, but a faster logger would copy the buffer with interrupts disabled for those few instructions.

Worked example

How much RAM does a ring buffer of one minute's readings take at 10 Hz, stored as 16-bit tenths?

Show the solution
  1. Count the readings. One minute at 10 Hz is 600 readings.
  2. Multiply by their size. 600×2bytes=1200bytes about 2% of this board's 60 KiB.

Answer 1200 bytes, about 2% of the board's RAM.

Worked example

Readings (tenths) are 231, 228, 240 and 236. What do min, max and the rounded average come to?

Show the solution
  1. Min and max. Min 228, max 240.
  2. Sum. The sum is 935.
  3. Rounded average. Adding half the divisor first makes the integer division round: 935+24=234 (234.25 rounds to 234).
  4. Format the report. In degrees, it reads avg 23.4 min 22.8 max 24.0.

Answer min 228, max 240 and average 234, so the report reads avg 23.4 min 22.8 max 24.0.

Check

A sampling ISR of 400 cycles runs 100 times a second. What share of the processor does it use?

Check

A logger keeps 10 s of readings at 10 Hz, 4 bytes each. How much RAM does its buffer need?

Check

A report line is 32 characters at 9600 baud. How long does it take to send?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

N = 4 readings in tenths sum to 1007. What does (sum + N/2) / N give, in integer arithmetic?

Check

The logger averages 0.55 mA from a 220 mAh battery. How many days does it run?

Check

Part 1: sample ADC0 exactly 10 times a second from the timer interrupt, toggling P0 at each sample, and sleep between samples.

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

Part 2: the readings are below, each plus OFFSET (0). Print their minimum, their maximum and their average rounded to the nearest whole tenth, in integer arithmetic.

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

Part 3: send T= and the value W * 10 + D tenths of a degree (7.0) as a decimal number with one decimal place, then a newline.

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Explain why

Why does the logger do its sampling in an interrupt but its reporting in the main loop?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.