GD32 combat 12__ timer

Primer

1. The common codes and PWM section
2. life often used in the following few samples,

  1. Alarm clock, the time given to remind
  2. Stopwatch (timer), statistics of time spent doing one thing

principle

Here Insert Picture Description

Above, the timer need a clock input, at each clock trigger, do the following

1. 计数器做增加或减少的操作
2. 跟目标值做比较,达到目标则触发中断,并重新把预置值设置到计数器中

Therefore, the actual configuration steps need to be configured as follows

  1. The clock source and the clock frequency dividing coefficient parameter
  2. Preset value and the target value
  3. Counting, growth and reduce
  4. Cycle mode, single cycle or trigger

Configuration Example

feature design

Using the timer 2 control LED lamp light once every second.

Timer Configuration

  1. Divider configuration, as shown, from the timer 2 AHB2 (108M) - / 2-> APB1 (54M) - * 2-> TIMER2 (108M), and therefore in order to achieve timing 1s, here dubbed 108MHz / 108100 = 10KHz, so Prescaler = 10800-1.Here Insert Picture Description

    TIMER_BaseInitParaStructure.TIMER_Prescaler = 10800-1; /* 10KHz */
    
  2. Configuration counting method

    TIMER_BaseInitParaStructure.TIMER_CounterMode = TIMER_COUNTER_UP;
    
  3. Configuration preset value, said first step to a 10KHz clock arranged, at a frequency of 10KHz, 10,000 counts is 1s, so TIMER_Period = 10000-1

    TIMER_BaseInitParaStructure.TIMER_Period = 10000-1; /* 10000*10KHz = 1s */
    
    1. Configuration cycle mode, the default mode is the cycle count, this step can be saved, if a single-step mode, it must be configured.
    TIMER_SinglePulseMode(TIMER2, TIMER_SP_MODE_REPETITIVE);
    
    1. Note that the timer interrupt, the first step must clear the timer interrupt flag, to prevent the interruption repeatedly to enter.
    TIMER_ClearIntBitState(TIMER2,TIMER_INT_UPDATE);
    
    1. The complete code is as follows,
VOID TIMER2_IRQHandler(VOID)
{
    if(TIMER_GetIntBitState(TIMER2,TIMER_INT_UPDATE) != RESET)
    {   
        /* 定时器中断中,第一步必须先清除定时器中断标记,防止中断反复进入 */
        TIMER_ClearIntBitState(TIMER2,TIMER_INT_UPDATE);
        if (gTimerLedFlag != 0)
        {
            DRV_LED_On(DRV_LED1);
            gTimerLedFlag = 0;
            return;
        }
        DRV_LED_Off(DRV_LED1);
        gTimerLedFlag++;
    }
    
}

VOID DRV_TIMER_Timer2Init(VOID)
{
    TIMER_BaseInitPara TIMER_BaseInitParaStructure;
    NVIC_InitPara NVIC_InitStructure;

    RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_TIMER2,ENABLE); 
	
    TIMER_DeInit(TIMER2);
    TIMER_BaseInitParaStructure.TIMER_Prescaler = 10800-1; /* 10KHz */
    TIMER_BaseInitParaStructure.TIMER_CounterMode = TIMER_COUNTER_UP;
    TIMER_BaseInitParaStructure.TIMER_Period = 10000-1; /* 10000*10KHz = 1s */
    TIMER_BaseInitParaStructure.TIMER_ClockDivision = TIMER_CDIV_DIV1;
    TIMER_BaseInit(TIMER2,&TIMER_BaseInitParaStructure);

    TIMER_INTConfig(TIMER2, TIMER_INT_UPDATE, ENABLE);
    NVIC_InitStructure.NVIC_IRQ = TIMER2_IRQn;   
    NVIC_InitStructure.NVIC_IRQPreemptPriority = 0; 
    NVIC_InitStructure.NVIC_IRQSubPriority = 0;       
    NVIC_InitStructure.NVIC_IRQEnable = ENABLE;        
    NVIC_Init(&NVIC_InitStructure);
    
    TIMER_SinglePulseMode(TIMER2, TIMER_SP_MODE_REPETITIVE);
    TIMER_Enable(TIMER2,ENABLE);
}

Code path

https://github.com/YaFood/GD32F103/tree/master/TestTimer

Guess you like

Origin blog.csdn.net/qq_17854661/article/details/91950464