[ST_MICRO]/STM322008. 4. 29. 12:26

[ STM32 ] Timer 테스트 - 1초 만들기

[ STM32 ] Timer 테스트 - 1초 만들기
 
타이머 인터럽트로 1ms만든 후 1초를 만들어 보자
STM32는 16비트 타이머를 가지고 있고 ... 정리를 위해 코드를 작성

//----------------------------------------------------------------------------- 
// Timer2 Interrupt Handler

void Timer2IntrHandler(void)
{
 Led1Toggle();
 Timer2_Counter++;
// SetTimer2(value);
}
//-----------------------------------------------------------------------------
 
//-----------------------------------------------------------------------------
//Timer2 Init

void Timer2Init(void)
{
 NVIC_InitTypeDef NVIC_InitStructure;
 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 /* TIM2 clock enable */
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 
 /* Enable the TIM2 gloabal Interrupt */
 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure); 
 
  /* ---------------------------------------------------------------
    TIM2 Configuration: Output Compare Timing Mode:
    TIM2CLK = 36 MHz, Prescaler = 4, TIM2 counter clock = 7.2 MHz
    CC1 update rate = TIM2 counter clock / CCR1_Val = 146.48 Hz
    CC2 update rate = TIM2 counter clock / CCR2_Val = 219.7 Hz
    CC3 update rate = TIM2 counter clock / CCR3_Val = 439.4 Hz
    CC4 update rate = TIM2 counter clock / CCR4_Val =  878.9 Hz
  --------------------------------------------------------------- */
 
 /* Time base configuration */
 TIM_TimeBaseStructure.TIM_Period = 7200;
 TIM_TimeBaseStructure.TIM_Prescaler = 0;
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
 /* Prescaler configuration */
 TIM_PrescalerConfig(TIM2, 4, TIM_PSCReloadMode_Immediate);
 
 /* TIM IT enable */
 TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
 
 /* TIM2 enable counter */
 TIM_Cmd(TIM2, ENABLE); 
}
//-----------------------------------------------------------------------------

간단히 만든 MCU 타이머 계산기로 계산 한 결과
    TIM2CLK = 36 MHz, Prescaler = 4, TIM2 counter clock = 7.2 MHz

 



main에서 1초마다 LED 점등..
 
 if(Timer2_Counter>1000)
 {
  Led2Toggle();
  Timer2_Counter = 0;
 }
 
STM32 1Hz Test

Posted by nexp

댓글을 달아 주세요