stm32定时器3产生1us延时的函数

void init_timer3_delay(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    TIM_TimeBaseInit(TIM3, &(TIM_TimeBaseInitTypeDef) {
        .TIM_Prescaler = (72-1),                                                        // 预分频器
         .TIM_CounterMode = TIM_CounterMode_Down,                                       // 向下计数
          .TIM_Period = (1000-1),                                                       // 设置自动重载值
           .TIM_ClockDivision = TIM_CKD_DIV1,                                           // 时钟分频
            .TIM_RepetitionCounter = 0                                                  // 此处无意义
    });

    while((TIM3->SR & TIM_FLAG_Update)!=SET);                                           // 计时到时,此处为1
    TIM3->SR = (uint16_t)~TIM_FLAG_Update;
}
//

void delay_us(unsigned int us)
{
    TIM3->CNT = us-1;
    TIM3->CR1 |= TIM_CR1_CEN;                                                           // 使能计数器
    while((TIM3->SR & TIM_FLAG_Update)!=SET);
    TIM3->SR = (uint16_t)~TIM_FLAG_Update;
    TIM3->CR1 &= ~TIM_CR1_CEN;
}
//

 

你可能感兴趣的:(STM32)