Microcontrollers (Advanced Theory)
Last updated
Last updated
Registers are small, high-speed memory locations within the CPU used to store data, addresses, or control flags during operations. They enable rapid access to critical information, reducing latency compared to external memory.
General-Purpose
Store intermediate results or operands for arithmetic/logic operations
R0-R12 (ARM)
Program Counter (PC)
Holds the address of the next instruction to execute
PC (E.g., 0x08000100
)
Stack Pointer (SP)
Points to the top of the stack for saving context during interrupts
MSP (Main SP), PSP (Process SP)
Status Register
Tracks CPU state (e.g., carry, zero, overflow flags)
APSR (Application Status Reg)
Control Registers
Configure peripherals (e.g., timers, PWM, UART)
TIM2_CR1 (Timer 2 Control Reg)
Example: To enable a timer interrupt on an STM32:
Interrupts allow the CPU to pause current tasks and handle urgent events (e.g., sensor input, timer overflow). They ensure timely responses in applications like robotics and IoT.
Event Trigger: A peripheral (e.g., timer, ADC) raises an interrupt flag.
Priority Check: The Interrupt Controller (NVIC) prioritizes interrupts.
Context Save: CPU pushes registers (PC, SP, status) onto the stack.
ISR Execution: Jumps to the Interrupt Service Routine (ISR) to handle the event.
Context Restore: Registers are popped, and the CPU resumes prior tasks.
Example (Arduino External Interrupt):
PWM generates variable-width pulses to control power delivery. The duty cycle (ON-time vs. period) determines the effective voltage.
Frequency: Set by the timer’s auto-reload register (e.g., 1 kHz → 1 ms period).
Duty Cycle: Determined by the capture/compare register (CCR).
Formula:
Duty Cycle (%)=(CCR ValueARR Value)×100Duty Cycle (%)=(ARR ValueCCR Value)×100
Example (STM32 PWM for Motor Control):
Microcontrollers use Harvard or Von Neumann architectures to manage instructions and data.
Separate Buses: Instructions and data have dedicated buses (faster, parallel access).
Common in MCUs: STM32, PIC, AVR.
Shared Bus: Single bus for instructions/data (simpler, slower).
Example: Legacy systems (e.g., Intel 8051).
Timers/Counters
Generate PWM, measure intervals, count events
Motor speed control
ADC/DAC
Convert analog signals to digital (ADC) and vice versa (DAC)
Sensor reading, audio output
UART/SPI/I2C
Serial communication protocols
GPS module, OLED display
GPIO
General-purpose input/output pins
Button input, LED control
Example (ADC for Temperature Sensor):
Clocks synchronize operations. Common sources:
Internal RC Oscillator: Low accuracy, low power (e.g., 8 MHz).
External Crystal: High accuracy (e.g., 16 MHz for Arduino).
PLL: Multiplies clock speed (e.g., STM32’s 72 MHz from 8 MHz).
Clock Tree Example (STM32):
Sleep Modes: Reduce power during inactivity (e.g., STOP, STANDBY).
Peripheral Clocks: Disable unused peripherals to save power.
Example (Arduino Low-Power Mode):
Sensors: Potentiometers (ADC) measure joint angles.
PWM: Control servo motors for precise movement.
Interrupts: Handle emergency stop button presses.
UART: Send/receive commands from a PC.
Code Snippet (Servo Control):