Lab #3: Processing GPIO Input - Setting the Clock

— (100 pts)

Overview

In Lab 1, you built a simple LED-based clock display using GPIO.
In Lab 2, you replaced software delays with hardware timers and PWM to improve the power consumption of the clock.

In this lab, you will add user interaction to your clock by implementing a button-controlled time-setting interface. You will use the pushbutton connected to PB8 (GPIO Port B, pin 8) to enter and exit clock-setting modes, and to adjust the displayed time.

This lab introduces:


Hardware

You will continue to use the custom ELEC 327 PCB from Labs 1 and 2.

Relevant circuitry for this lab:

No additional hardware is required.


Starting Point

Start from your Lab 2 working codebase.

Your program should already:

If your Lab 2 code does not work correctly, a solution will be provided Wednesday Feb 4, 2026.

Goal: GetNextState and GetStateOutput should now be functions of both the current state and the input!


Functional Requirements

1. Clock Operating Modes

Your firmware must support three operating modes:

  1. Normal Clock Mode
  2. Hour-Set Mode
  3. Minute-Set Mode
  4. [Extra-Credit] Brightness-setting Mode

2. Button Behavior

The pushbutton on PB8 must behave as follows:

Long Press

Short Press


3. Mode Transitions

Current Mode Long Press Action
Normal Clock Enter Hour-Set Mode
Hour-Set Mode Enter Minute-Set Mode
Minute-Set Mode Return to Normal Clock Mode

4. Visual Feedback

To provide clear user feedback:

Flashing should be implemented using the state machine mechanism (do not use blocking delays).


5. Adjustment

It is strongly recommended (but not required) that the change in LED position should be actuated when the button is released. However it is implemented, the position should not advance when the user changes the mode using a long-press.

When exiting Hour-Set or Minute-Set Mode, the newly set time becomes the active clock time.

Extra Credit: For extra credit, add a third setting mode after hour and minute. In this third mode, the user will change the LED brightness setting.


6. Button Debouncing

Your firmware must handle button debouncing as part of the state machine architecture. You may assume that “real” button presses last longer than 5 ms, and that glitches when the button is depressed longer than this duration will not occur.


Implementation

As with Labs 1 and 2, the final implementation must be a state machine, where the next state depends on the current state. However, now, the next state will depend on the current state AND the input. We will modify the Lab 2 template code together during class to develop a Lab 3 template main() function.

In the final implementation, you are strongly encouraged to construct your state machine in such a way that the the logic is handled heirarchically using state variables corresponding to the operating mode and internal substates. This will enable you to separate button handling logic from clock setting logic from clock operating logic from PWM logic.



Submission

Submit the following:

  1. Source code
    • Clearly organized and commented
    • State machine logic must be identifiable
    • Single zip file as in Lab 3
  2. README (brief)
    • Description of your state machine
    • Explanation of how long vs short presses are detected
    • Any known limitations or assumptions