STM32F4 타이머 인터럽트 테스트
STM32F4에서 TIM2, TIM5는 32bit 타이머 모드를 지원한다. 이를 이용하여 1초 만들기 테스트를 해 보았다.
STM32F4 타이머 인터럽트 초기화 함수
void TimerInit2(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t PrescalerValue = 0;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* TIM2 gloabal Interrupt 초기화 */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//--------------------------------------------------------------------------
/* Compute the prescaler value */
PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 6000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = (6000-1); //Autoreload value (ARR) -> 1ms
TIM_TimeBaseStructure.TIM_Prescaler = (6000-1);
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Prescaler configuration */
TIM_PrescalerConfig(TIM2, PrescalerValue, TIM_PSCReloadMode_Immediate);
/* TIM Interrupts enable */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
//--------------------------------------------------------------------------
}
STM32F4 타이머 인터럽트 핸들러
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
Led2Toggle();
}
}