Skip to main content

Subsection 9.2.9 Part 8: Before Next Class

Tonight you’ll add a second pin interrupt to the counter you wrote today: the Nucleo’s blue user button on PC13, alongside the button on PB4. The blue button will also toggle the on-board LED, using the atomic writes to BSRR we found above. The delay_ms(1000) stays in the loop. An interrupt is taken whether or not the loop is blocked, which is what lets a press be noticed during the delay.
The homework can be done in the simulator as well as on your board. The link below opens the simulator with the counterResetButtonInt.c starter. Paste your own version from today in its place, then click + Button in the component bay and click the Arduino header pin labeled D5 to attach the PB4 button again. The blue user button is already on the simulated Nucleo, labeled B1 (PC13): hold it down to press it.

Activity 9.2.7. Homework: counterTwoButtons.c.

The program is due on Thursday, uploaded to Canvas.

(a)

Start from counterResetButtonInt.c as you wrote it today, and add the Nucleo’s blue user button on PC13, so that pressing it reverses the counting direction. PC13 is also on lines 4 to 15, so it shares EXTI4_15_IRQHandler with PB4, and the handler has to tell the two apart. You will need a second initialization function, and a second field in a different EXTICR register. One thing to think about before you build: the test that wraps the count at MAXCOUNT was written when the count only ever went up. What should happen when it runs the other way?

(b)

The same press of the blue user button should also toggle the on-board LED on PA5 (header D13), and a press of PB4 should not. Do the toggle inside the handler rather than in main(). The handler runs the moment the edge arrives, while main() only reaches its button test after delay_ms(1000) has finished, so a toggle placed there could be up to a second late. The LED’s clock enable and MODER setup can come from the program you wrote last night. Drive it with GPIOA->BSRR, not GPIOA->ODR: BSRR has no toggle of its own, so keep the LED’s state in a variable that main() never writes and set or clear the pin from it, exactly the way redOn does in the TIM14_IRQHandler we wrote with BSRR.

Activity 9.2.8. Optional: Good Practice, and Two Coding Challenges.

All three of these are optional.

(a)

Modify counterResetButtonInt.c, the single-button version you wrote in class, to use a TIM14 interrupt instead of delay_ms(), with an update period of one second, and save it as counterResetButtonIntTimer.c. The main loop then blocks on nothing. We’ll take a look at two AI-written answers to this program on Thursday.

(b)

It is good practice to move pb4_exti_init() (and pc13_exti_init() if you wrote it) into exti.c and exti.h. Give the header an include guard, the #ifndef / #define / #endif wrapper that keeps the compiler from reading the same declarations twice when more than one source file includes it. Then place the header file into your project’s Inc directory and the source file into Src. This makes your counterResetButtonInt.c shrink down to the part that is actually about counting.

(c)

Rewrite ADCPot.c to take a measurement every second from a timer interrupt rather than a delay. You will still poll the ADC to find out when the conversion is done; what goes away is the delay_ms().
Next we’ll change subject to I2C (Inter-Integrated Circuit), a two-wire bus that lets one microcontroller talk to many devices. We’ll start it in Wednesday’s x-hour by wiring a seven-segment display to two of the Nucleo’s pins.