2020-06-13 01:16 AM
Hi everyone. I wrote this code to run the stm32f0 timer3, which toggle 2 LEDs at a time, but it doesn't work.
Can anyone please help me؟
void TIM3_Init()
{
TIM_TimeBaseInitTypeDef TIM3_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM3_InitStructure.TIM_Prescaler = 359;
TIM3_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM3_InitStructure.TIM_Period = 199;
TIM3_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM3_InitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM3_InitStructure);
TIM_Cmd(TIM3, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET){
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
GPIOB->ODR ^= GPIO_Pin_2|GPIO_Pin_3;
}
}
2020-06-13 01:33 AM
Read out and check/post the TIM and relevant GPIO registers content.
JW
2020-06-13 01:00 PM
Line:
RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);
is wrong. You want call function:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
to enable clock ! For next similar issues i suggest to do thist "checklist":