Skip to main content

Subsection 9.2.2 Part 2: The Press the Polled Counter Misses

Before any code, confirm the hardware. We’ll re-use the button circuit on PB4 with its internal pull-up and the debouncing capacitor (Figure 9.2.2). If you no longer have it wired on your breadboard, rebuild it now, with the button on D5. Then open our earlier button.c, compile it and run it, and check that everything still works as expected: if it does not, the fault is a wire and not your code. Re-seat the button and the capacitor against the photo, and run it again. Still stuck? We’re here to help!
Figure 9.2.2. The button circuit to check before any code: the button is on D5 (PB4) and the debouncing capacitor runs from D5 to GND, in parallel with the button. The red dashed line shows where the capacitor connects.
counterResetButtonPolled.c prints a counter once a second and resets it when the button is down. The button read is the same one we used before. The pin is HIGH when the button is open and LOW when it is pressed, so “pressed” is bit 4 of the input data register reading 0, (GPIOB->IDR & BUTTON_PIN) == 0:
#include <stdio.h>
#include "ES28.h"
#include "uart.h"
#define WAIT        1000000
#define MAXCOUNT    100
#define BUTTON_PIN  (1U<<4)   // Button on PB4

int main(void) {
    int counter = 0;
    int buttonPushed = 0;
    uart2_init();                                          // initialize UART
    RCC->IOPENR |= RCC_IOPENR_GPIOBEN;                     // enable clock

    GPIOB->MODER = (GPIOB->MODER & ~GPIO_MODER_MODE4_Msk)
                 | (GPIO_INPUT << GPIO_MODER_MODE4_Pos);  // input mode

    GPIOB->PUPDR = (GPIOB->PUPDR & ~GPIO_PUPDR_PUPD4_Msk)
                 | (GPIO_PULLUP << GPIO_PUPDR_PUPD4_Pos);  // pullup

    while(1) {
        printf("%x\t%d\r\n", counter, counter);
        delay_ms(1000);                                    // kill time
        buttonPushed = ((GPIOB->IDR & BUTTON_PIN) == 0);   // Poll (1 => pressed)
        if ( (buttonPushed) || (counter == MAXCOUNT) ) {
            counter = 0;                                   // Reset the counter
        } else {
            counter++;
        }
    }
    return 0;
}

Activity 9.2.1. Watch the Counter Miss a Press.

counterResetButtonPolled.c is in the book’s starter files. You will need Coolterm open to see the output.

(a)

Copy your TemplateProject to a new project named CounterResetButton, download counterResetButtonPolled.c, and put it in the project’s Src folder. Build and flash it.

(b)

Also download counterResetButtonInt.c and put it in Src, then exclude it from the build for now (right-click → Resource Configurations → Exclude from Build). We will work on it later.

(c)

Watch the counter change every second in Coolterm. Give the button a short tap. Then press and hold it for two seconds. Do both reset the counter?

(d)

Compare your observations with your table group: what did the program miss, and where in the program was it when it missed it?
A tap-and-hold works every time; a short tap works only if you are lucky enough to still be holding the button at the instant the loop reaches its one read of GPIOB->IDR. The program spends 1 second inside delay_ms() followed by one moment looking at the pin. Everything that happens during the 1 second delay is invisible to it.