Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
STM32F446 TIMER2 Ooutput COmpare PA3 1MHz square wave
void HAL_AFE_Init1MHZ_Clock(void){
//RCC_AHB1PeriphClockCmd(HAL_DS_AFE_CLK1MHZ_PORT_GPIO_AHB_BIT, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_Init(HAL_DS_AFE_CLK1MHZ_PORT, (GPIO_InitTypeDef *) &HAL_DS_1MHz_CLK_GpioConfiguration);
GPIO_PinAFConfig(HAL_DS_AFE_CLK1MHZ_PORT, HAL_DS_AFE_CLK1MHZ_AF_PINSOURCE ,GPIO_AF_TIM2);
TIM_TimeBaseInitTypeDef TIM2_TimeBaseStructure;
TIM2_TimeBaseStructure.TIM_Period = 21;
TIM2_TimeBaseStructure.TIM_Prescaler = 0;
TIM2_TimeBaseStructure.TIM_ClockDivision = 0;
TIM2_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM2_TimeBaseStructure);
/* Prescaler configuration */
TIM_PrescalerConfig(TIM2, 1, TIM_PSCReloadMode_Immediate);
TIM_OCInitTypeDef TIM_OCInitStructure;
/* always initialise local variables before use */
TIM_OCStructInit (&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1;
TIM_OC4Init (TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment