GPIO (General Purpose Input/Output)
GPIO pins are versatile pins on microcontrollers that can be configured as either inputs or outputs to interface with various electronic components.
GPIO Configuration Modes
- Floating Input: The pin is left unconnected, which can lead to unpredictable states due to noise. Not recommended for practical applications.
- Pull-up Input: The pin is connected to VCC through a resistor, ensuring a high level when no signal is applied.
- Pull-down Input: The pin is connected to GND through a resistor, ensuring a low level when no signal is applied.
- Analog Input: The pin reads continuous voltage values rather than digital high/low states.
- Open-drain Output: Can only pull the pin low; requires an external pull-up resistor to achieve high state. Good for current sinking.
- Push-pull Output: Can actively drive the pin to both high and low states. Provides stronger output signals.
- Alternate Function Output: When the pin is used for a special purpose (like UART, SPI, etc.) rather than as a general-purpose IO.
GPIO Voltage Levels
Low level: 0-3.3V, High level: 3.3V. Some GPIO pins are 5V tolerant.
GPIO Applications
- Output mode: Control LEDs, drive buzzers, generate communication protocol signals, and control any device that responds to digital signals.
- Input mode: Read button states, detect external module signals, collect ADC values, and receive communication data.
LED Control
LEDs (Light Emitting Diodes) emit light when forward-biased. The longer lead is the anode (+), and the shorter lead is the cathode (-).
Basic LED Circuit
Connect the LED's anode to a GPIO pin through a current-limiting resistor (typically 220-330Ω). Connect the cathode to ground.
Example: LED Blinking
#include "stm32f10x.h"
#include "timer.h"
int main(void)
{
// Enable clock for GPIO Port A
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Configure GPIO pin 5 as push-pull output
GPIO_InitTypeDef gpioConfig;
gpioConfig.GPIO_Pin = GPIO_Pin_5;
gpioConfig.GPIO_Mode = GPIO_Mode_Out_PP;
gpioConfig.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpioConfig);
while(1) {
// Turn LED on
GPIO_SetBits(GPIOA, GPIO_Pin_5);
delay_ms(500);
// Turn LED off
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
delay_ms(500);
}
}
LED Running Light Example
#include "stm32f10x.h"
#include "timer.h"
int main(void)
{
// Enable clock for GPIO Port C
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
// Configure all pins of Port C as push-pull outputs
GPIO_InitTypeDef gpioConfig;
gpioConfig.GPIO_Pin = GPIO_Pin_All;
gpioConfig.GPIO_Mode = GPIO_Mode_Out_PP;
gpioConfig.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &gpioConfig);
uint16_t pattern = 0x0001; // Start with first LED
while(1) {
// Display current pattern
GPIO_Write(GPIOC, ~pattern);
// Shift pattern to the right
pattern <<= 1;
if (pattern == 0) pattern = 0x0001; // Reset if all LEDs have been lit
delay_ms(200);
}
}
Buzzer Control
Buzzers are audio signaling devices that can be categorized as active or passive:
- Active Buzzers: Have built-in oscillation circuit. Produce sound at a fixed frequency when connected to DC voltage.
- Passive Buzzers: Require an oscillating signal to produce sound. The frequency of the signal determines the pitch of the sound.
Example: Buzzer Control
#include "stm32f10x.h"
#include "timer.h"
int main(void)
{
// Enable clock for GPIO Port B
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// Configure GPIO pin 8 as push-pull output
GPIO_InitTypeDef gpioConfig;
gpioConfig.GPIO_Pin = GPIO_Pin_8;
gpioConfig.GPIO_Mode = GPIO_Mode_Out_PP;
gpioConfig.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &gpioConfig);
while(1) {
// Turn buzzer on
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
delay_ms(300);
// Turn buzzer off
GPIO_SetBits(GPIOB, GPIO_Pin_8);
delay_ms(700);
}
}
Input Devices and Sensors
Button Basics
Buttons are common input devices that close a circuit when pressed and open it when released. Mechanical buttons exhibit "bouncing" - rapid on/off transitions when pressed or released, which must be handled in software.
Sensor Modules
Sensor modules contain components like photoresistors, thermistors, or IR receivers that change resistance based on environmental conditions. These are typically used with voltage dividers and comparators to produce digital outputs.