Skip to main content

Subsection 11.5.6 Part 6: Build

The rest of the class is yours to build in. Start on paper: the loop you are about to write has three rates in it and one counter shared between an interrupt handler and main(), and both are easier to get right on paper than in the editor. Then work down the build order, and test each stage before you add the next one.

Activity 11.5.7. Plan the main loop before you write it.

Start on paper. Your program has to do three things at three different rates. The interrupt that counts the pulses is a falling-edge interrupt on PA15 (D7), and it is the same one you wrote for the button on PB4: start from counterResetButtonInt.c and adapt it.

(a)

Work out what the loop does on every pass, what it does every 10 ms, and what it does once a second, and for each one, what decides that it is time.

(b)

The rpm comes from counting pulses over one second. Where in your pseudocode does the count get read, and where does it get set back to zero? What happens to a pulse that arrives between those two moments?

(c)

If the count is incremented in an interrupt handler and read in the main loop, what does its declaration need that an ordinary variable does not, and why?

Activity 11.5.8. Going further.

These are optional, once you are able to display the rpm.

(a)

The sign on the displayed rpm comes from what your program commanded, not from what it measured. Describe a situation in which the motor is not doing what it was told, and say what the display would show. What would have to change for the measurement itself to carry the sign?

(b)

A quadrature encoder puts a second sensor a quarter of a cycle away from the first. Sketch the two output waveforms for a wheel turning clockwise, then for the same wheel turning counter-clockwise, and say precisely what a program would have to look at to tell them apart.

(c)

Counting whole pulses in a one-second window quantizes the answer: work out how many rpm one pulse of miscount is worth on your wheel, and what fraction of the reading that is at a slow speed. Then take three ways of doing better (count for longer, count both edges, or time the interval between two pulses instead of counting) and say what each one costs and where each one wins.