Floor Plan
Memory Mapped I/O in C Exercise
Task
A control panel has an on/off switch and a status lamp that shows whether the switch is on, so the operator can tell the machine's state from across the room. The switch and the lamp are on two different ports of the chip.
Make the lamp follow the switch for as long as the board runs: switch on, lamp on; switch off, lamp off Write one struct type that describes a GPIO port's registers, and reach port A and port C only through it. The registers are on the Datasheet tab, under GPIOA and GPIOC.
Circuit
Pass condition
The lamp on PA5 matches the switch on PC13 at all times and never turns on or off by itself, and PC13's pull-down is on. Every register on port A and port C is reached through your struct type, and the struct's registers are volatile. A volatile value is read from or written to the hardware each time the code names it, never kept in a copy.
Notes
The switch is on PC13 (pin 13 of port C), and the lamp is on PA5 (pin 5 of port A). In each port, MODER sets what each pin does (input or output), PUPDR sets each pin's pull-up or pull-down, IDR shows each pin's level right now (bit 13 is pin 13), and BSRR drives single pins high or low. A pull-down is a weak link to 0 inside the chip. With the switch off, nothing on the board holds PC13 at 0, but a pull-down does once port C's PUPDR turns it on. The Datasheet tab lists each port's registers in order. Each offset counts bytes from the port's base address to that register.