/* p11_2.c Using TIM16 for PWM Output * * This program configures TIM16 with prescaler divides by 10 * 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 Ch1N is * turned on when the counter starts counting from 0. When the * counter matches the content of CCR1, Ch1N 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 LED will be on for 8889/26667 = 30% of the time. * The output pin of TIM16 Ch1N is PB6 and the alternate function * of PB6 should be set to AF2. * * This program is similar to p11_1.c except: * 1. Each PWM output channel (Chx) has a complementary output (ChxN) * and we are using the Ch1N output. * 2. The output needs to be enabled at TIM16_BDTR register. * * This program was tested with Keil uVision v5.23 with DFP v2.0.0. */ #include "stm32f0xx.h" int main(void) { RCC->AHBENR |= 0x00040000; /* enable GPIOB clock */ GPIOB->AFR[0] |= 0x02000000; /* PB6 pin for TIM16 channel N1 */ GPIOB->MODER &= ~0x00003000; GPIOB->MODER |= 0x00002000; /* setup TIM16 */ RCC->APB2ENR |= 0x00020000; /* enable TIM16 clock */ TIM16->PSC = 5 - 1; /* divided by 5 */ TIM16->ARR = 26667 - 1; /* divided by 26667 */ TIM16->CNT = 0; TIM16->CCMR1 = 0x0060; /* PWM mode */ TIM16->CCER = 4; /* enable PWM ChN1 */ TIM16->BDTR = 0x8000; /* enable main output enable */ TIM16->CCR1 = 8889; /* pulse width 1/3 of the period */ TIM16->CR1 = 1; /* enable timer */ while(1) { } }