/*systemfrequency/1000 1ms Interrupt once * systemfrequency/100000 10US interrupt once * systemfrequency/1000000 1us interrupt once*/#defineSystickperiod 0.000001#defineSystickfrequency (1/systickperiod)/** * @brief the initialization of Timer 2, time period 1ms * @param no * @retval no*/voidTim2_init (void) {tim_timebaseinittypedef tim_timebasestructure; /*AHB = PPRE1 = 72MHZ,RCC_CFGR = 2, so APB1 = 36MHZ,TIM2CLK = apb1*2 = 72MHz*/rcc_apb1periphclockcmd (rcc_apb1periph_tim2, ENABLE); /*Time Base Configuration*/Tim_timebasestructure.tim_period=999; Tim_timebasestructure.tim_prescaler= Systemcoreclock/systickfrequency-1; Tim_timebasestructure.tim_countermode=tim_countermode_up; Tim_timebaseinit (TIM2,&tim_timebasestructure); Tim_arrpreloadconfig (TIM2, ENABLE); /*setting an update request source only interrupts when the counter overflows or overflows*/Tim_updaterequestconfig (Tim2,tim_updatesource_global); Tim_clearflag (TIM2, tim_flag_update);}/** * @brief ms Delay program, 1MS is a unit * @param * @arg Ntime:delay_ms (10) The implementation of the delay is ten * 1ms = 10ms * @retval No */voidDelay_ms (__io uint32_t ntime) {/*Clear 0 counter and enable tick timer*/TIM2->cnt =0; Tim_cmd (TIM2, ENABLE); for(; Ntime >0; ntime--) { /*wait for the end of a delay unit*/ while(Tim_getflagstatus (TIM2, tim_flag_update)! =SET); Tim_clearflag (TIM2, tim_flag_update); } tim_cmd (TIM2, DISABLE);}
STM32 Normal Timer Implementation delay function