/* p11_1.c Using TIM3 for PWM Output * * This program configures TIM3 with prescaler divides by 5 * so that the timer counter is counting at 1.6 MHz. * ARR register is loaded with 26666 and CCR1 is loaded with 8888. * The channel 1 is configured as PWM mode. The output of Ch1 is * turned on when the counter starts counting from 0. When the * counter matches the content of CCR1, Ch1 output is turned off. * When the counter matches ARR, the counter is cleared to 0 and * the output is turned on and the counter starts counting up again. * The output frequency is 1.6M / 26667 = 60Hz. * The LED will be on for 8889/26667 = 33% of the time. * The output pin of TIM3 Ch1 is PA6 and the alternate function * of PA6 should be set to AF1. * * This program was tested with Keil uVision v5.23 with DFP v2.0.0. */ #include "stm32f0xx.h" int main(void) { RCC->AHBENR |= 0x00020000; /* enable GPIOA clock */ GPIOA->AFR[0] |= 0x01000000; /* PA6 pin for TIM3 channel 1 */ GPIOA->MODER &= ~0x00003000; GPIOA->MODER |= 0x00002000; /* setup TIM3 */ RCC->APB1ENR |= 0x00000002; /* enable TIM3 clock */ TIM3->PSC = 5 - 1; /* divided by 5 */ TIM3->ARR = 26667 - 1; /* divided by 26667 */ TIM3->CNT = 0; TIM3->CCMR1 = 0x0060; /* PWM mode */ TIM3->CCER = 1; /* enable PWM Ch1 */ TIM3->CCR1 = 8889; /* pulse width 1/3 of the period */ TIM3->CR1 = 1; /* enable timer */ while(1) { } }