4.1.1.6. Timer Lesson
4.1.1.6.1. Overview of Timers
Timers are essential peripherals in microcontrollers, including the STM32F411, that offer precise timing and counting functionalities. In applications requiring time delays, signal generation, or event counting, timers provide versatility and efficiency.
4.1.1.6.2. Timer Modes in STM32F411
Basic Mode: - Ideal for generating periodic interrupts and simple time delays. - Common in software timing applications requiring precision interrupts.
Capture/Compare Mode: - Allows capturing external events and comparing internal counter values. - Suitable for measuring frequency, input capture for timing, or precise waveform generation.
PWM (Pulse Width Modulation) Mode: - Generates PWM signals with adjustable frequency and duty cycle. - Applications include motor control and LED dimming.
Encoder Mode: - Used for rotary encoder inputs to track rotational motion. - Applied in robotics, motor positioning, and motion tracking.
4.1.1.6.3. Key Timer Configurations for PWM Signal Generation
Timer Frequency: - Derived from the microcontroller clock and prescaler. - Formula:
Timer Frequency = Timer Clock / (Prescaler + 1)
PWM Frequency: - Defined by the auto-reload register (ARR) for the PWM period. - Formula:
PWM Frequency = Timer Frequency / (ARR + 1)
Duty Cycle: - Controlled by the capture/compare register (CCR) to set high-time percentage. - Formula:
Duty Cycle (%) = (CCR / (ARR + 1)) * 100
Output Mode: - Configures PWM output on designated pins.
4.1.1.6.4. Practical Example: Configuring PWM Signal Generation
This example configures Timer 3 on the STM32F411 to generate a PWM signal with a frequency of 1 kHz and a duty cycle of 50%.
Example Setup: - Timer: Timer 3, Channel 1 - System Clock: 84 MHz - Desired PWM Frequency: 1 kHz - Desired Duty Cycle: 50%
// Enable Timer 3 clock
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
// Configure the prescaler for 1 MHz timer frequency
TIM3->PSC = 83;
// Set ARR for 1 kHz PWM frequency
TIM3->ARR = 999;
// Set CCR1 for a 50% duty cycle
TIM3->CCR1 = 500;
// Configure PWM mode on channel 1 and enable output
TIM3->CCMR1 |= TIM_CCMR1_OC1M_PWM1;
TIM3->CCER |= TIM_CCER_CC1E;
// Start Timer 3
TIM3->CR1 |= TIM_CR1_CEN;
Explanation:
Timer Clock Enable: Enables Timer 3.
Prescaler Configuration: Divides the clock for 1 MHz frequency.
ARR: Sets PWM frequency to 1 kHz.
CCR1: Sets duty cycle to 50%.
Start Timer: Finalizes configuration.
4.1.1.6.5. Applications of PWM Generation
Motor Speed Control: Vary duty cycle for speed adjustments.
LED Brightness Control: Adjust brightness with varying duty cycles.
Audio Signal Generation: Generate tones or modulate signals for transmission.