/* p5_6.c Toggle PWM/D9 PIN using TIM3 Compare mode * * This program configures TIM3 with prescaler divides by 800 * and counter wraps around at 10000. So the 8 MHz system clock * is divided by 1600 then divided by 10000 to become 1 Hz. * The channel 1 is configured for compare mode to toggle the output * pin every time the timer counter matches the CCR2 register. * The output pin of TIM3 CH2 is PC7 (PWM/D9) and the alternate function * of PC7 should be set to AF0. * * This program was tested with Keil uVision v5.23 with DFP v2.0.0 */ #include "stm32f0xx.h" int main(void) { // configure PC7 as output RCC->AHBENR |= 0x00080000; /* enable GPIOC clock */ GPIOC->MODER &= ~0x0000C000; /* clear pin mode */ GPIOC->MODER |= 0x00008000; /* set pin to alternate function */ GPIOC->AFR[0] &= 0xF0000000; /* clear pin AF bits to set pin to AF0 for TIM3 CH2 */ // configure TIM3 to wrap around at 1 Hz // and toggle CH1 output when the counter value is 0 RCC->APB1ENR |= 0x00000002; /* enable TIM3 clock */ TIM3->PSC = 800 - 1; /* divided by 800 */ TIM3->ARR = 10000 - 1; /* divided by 10000 */ TIM3->CCMR1 = 0x3000; /* set output to toggle on match */ TIM3->CCR2 = 0; /* set match value */ TIM3->CCER |= 0x10; /* enable CH2 compare mode */ TIM3->CNT = 0; /* clear counter */ TIM3->CR1 = 1; /* enable TIM3 */ while (1) { } }