A relay and a sensor's power switch, from the transistors
One transistor turns its load on with a high pin, the other with a low pin. Work out which from the schematic, and keep both off from reset. A medium…
medium · Reading datasheets and schematics · schematics · gpio · driver
PA8 switches a 12 V relay and PA9 switches the humidity sensor's supply. Working from the Datasheet excerpt below, write:
power_init(), which makes PA8 and PA9 outputs with the relay off and the sensor unpowered. Change no other pin.relay_set(on), which energises the relay's coil whenonis 1 and releases it when it is 0.sensor_power(on), which powers the sensor whenonis 1 and cuts its supply when it is 0.
Every change of an output pin shows up in the output as gpioa: pin 8 high.
Datasheet excerpt
Board schematic, sheet 4 (switched loads)
12V
│
D5 (flyback, across K1)
│
K1 relay coil
│ collector
PA8 ──[R30 1 kΩ]──┬──── Q2 base Q2: NPN (MMBT3904)
│ │ emitter
[R31 10 kΩ] GND
│
GND
3V3
│ source
3V3 ─[R33 100 kΩ]─┐
│
PA9 ──────────────┴── Q3 gate Q3: P-channel MOSFET (DMG2305)
│ drain
└── VDD_SENSOR ── U8 (humidity sensor) VDD
GPIO registers (reference manual)
| Offset | Register | Pin y occupies | Values |
|---|---|---|---|
| 0x00 | MODER | bits 2y+1:2y | 00 input, 01 output, 10 alternate function. Resets to 0xA8000000: PA13 and PA14 are the debugger's |
| 0x14 | ODR | bit y | the output latch |
| 0x18 | BSRR | bit y sets pin y, bit y+16 clears it | write-only |
Starter code
void power_init(void) {
GPIOA->MODER = (1u << 16) | (1u << 18);
}
void relay_set(int on) {
GPIOA->BSRR = on ? 1u << 8 : 1u << 24;
}
void sensor_power(int on) {
GPIOA->BSRR = on ? 1u << 9 : 1u << 25;
}Example tests
Both loads on and off
power_init();
relay_set(1);
sensor_power(1);
relay_set(0);
sensor_power(0);Expected output:
gpioa: pin 9 high
gpioa: pin 8 high
gpioa: pin 9 low
gpioa: pin 8 low
gpioa: pin 9 highLearn it first
- Reading datasheets and schematics · Where the answers are, min/typ/max, calculating from the tables and reading register maps.
More Reading datasheets and schematics problems
- Build a register from its datasheet table · Assemble the ADS1115 ADC's 16-bit config register from its fields.
- Baud rate divider from the reference manual · Turn the reference manual's baud rate formula into a BRR register value, in both oversampling modes.
- ADC timing from the electrical characteristics · Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time.
- Bit-bang SPI from a timing diagram · Read the clock polarity and phase off a sensor's timing diagram and drive the pins to match.
- A sensor frame, its CRC and its transfer function · Check the CRC the datasheet specifies, then convert raw readings to temperature and humidity.
- Apply the errata sheet's workarounds · An ADC driver that follows the reference manual still reads wrong values. The errata sheet explains why.
- Route a UART to its pins from the alternate-function table · Find USART2's pins and function number in the datasheet, then set MODER, AFRL, OSPEEDR and PUPDR without disturbing other pins.
- An open-drain interrupt line, from the sensor's pin description · The datasheet says INT is open-drain and active low. Configure the pin so it reads correctly, and report when the sensor asserts it.
- A 10 ms tick, from the clock tree · Work out the timer's input clock from the clock tree, including the APB timer rule, then set the prescaler and auto-reload for a 10 ms period.
- Configure a sensor over SPI from its datasheet · Pick the SPI mode and the fastest clock the sensor allows, then set its data rate and range from the register map, including a non-obvious field encoding.
- Fast-mode I2C and the EEPROM's address, from three documents · Compose TIMINGR from the reference manual's timing table, work out the EEPROM's address from its datasheet and the schematic, and probe for it.
- Receive from a GPS module, from its interface description · Set the baud rate divider and frame format the module's datasheet gives, with only the receiver enabled, and read one NMEA sentence.
- An LED and a button, from the board schematic · The schematic decides which level lights the LED and which level means pressed. Write the three functions that hide it.
- Battery voltage through a resistor divider · Read the divider ratio and the ADC's reference voltage off the schematic, and turn a raw reading into millivolts.
- Trace an expansion header to the microcontroller's pins · Follow each header pin's net through resistors, a level shifter and solder bridges to the pin it ends at.
- Route a pin's interrupt, from the reference manual · Use the SYSCFG and EXTI register descriptions and the vector table to send any pin's edges to its interrupt.
- Choose a DMA stream from the request mapping table · Find a free stream and channel for a peripheral's DMA request in the reference manual's table, and compose the stream's control register.