This is an old revision of the document!
Timer Compare
Programming Example
The code snippet bellow shows how to configure and use a GPIO pin as input.
#include "reg_stm32f4xx.h" RCC->AHBENR[0] |= (0x1 << 0u); /* Enable GPIOA clock */ RCC->APBENR[0] |= (0x1 << 0u); /* Enable TIM2 clock */ /* configure output */ GPIOA->MODER |= (0x2 << 0u); /* Set pin 0 to AF */ GPIOA->AFR[0] |= (0x1 << 0u); /* Set pin 0 to AF1 (TIM2) */ /* configure basic timer */ TIM2->PSC = 84000u - 1u; /* Counting with f = 84MHz / 84000 = 1MHz */ TIM2->ARR = 512u; /* Count to 512 */ /* configure timer output */ TIM2->CCMR1 |= (0x6 << 4u); /* Enable PWM mode 1 on channel 1 */ TIM2->CCER |= (0x1 << 0u); /* Enable output on channel 1 */ TIM2->CR1 |= (0x1 << 0u); /* Start timer */
Configuration Registers
CCMR1/2
DIER
DMA / Interrupt enable register
CCxDE | 0 | CCx DMA request disabled (reset state) |
1 | CCx DMA request enabled | |
CCxIE | 0 | CCx interrupt disabled (reset state) |
1 | CCx interrupt enabled |
CCER
Capture/compare enable register
CCxE | 0 | Capture/compare output x disabled (reset state) |
1 | Capture/compare output x enabled |