STM32 Normal Timer Implementation delay function

Source: Internet
Author: User

/*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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.