/* * Use TIM2_CH4 PWM function to drive the speaker on * the EduBase board to generate a 440 Hz sound. * The timer TIM2 clock is running on 16 MHz. Timer divider is * 16,000,000 / 440 = 36364 */ #include "stm32f4xx.h" int main(void) { RCC->AHB1ENR |= 2; /* enable GPIOB clock */ // configure PB2 as TIM2_CH4 PWM output pin GPIOB->AFR[0] &= ~0x00000F00; GPIOB->AFR[0] |= 0x00000100; /* PB2 pin for TIM2 */ GPIOB->MODER &= ~0x00000030; GPIOB->MODER |= 0x00000020; /* PB2 uses alternate function */ /* setup TIM2_CH4 PWM */ RCC->APB1ENR |= 1; /* enable TIM2 clock */ TIM2->PSC = 0; /* prescaler divided by 1 */ TIM2->ARR = 36364 - 1; /* counter divided by 36364 */ TIM2->CNT = 0; /* clear counter */ TIM2->CCMR2 = 0x6000; /* PWM mode */ TIM2->CCER = 0x1000; /* enable PWM Ch4 */ TIM2->CCR4 = 18182 - 1; /* pulse width 1/2 of the period */ TIM2->CR1 = 1; /* enable timer */ while(1) { } }