• STM32定时器配置


    void TIM1_Int_Init(u16 arr,u16 psc)
    {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //①时钟 TIM1 使能
    //定时器 TIM1 初始化
    TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置时钟频率除数的预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数
    TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //高级定时器1是用定时器功能配置这个才可以是正常的计数频率一开始的72mhz 值得注意的地方
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //②初始化 TIM1
    TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //③允许更新中断
    //中断优先级 NVIC 设置
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1 中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级 1 级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级 3 级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能
    NVIC_Init(&NVIC_InitStructure); //④初始化 NVIC 寄存器
    TIM_Cmd(TIM1, ENABLE); //⑤使能 TIM1
    }



    void TIM1_UP_IRQHandler(void) //TIM1 中断
    {
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) //检查 TIM1 更新中断发生与否
    {
    TIM_ClearFlag(TIM1, TIM_IT_Update ); //清除 TIM1 更新中断标志
    LED1=!LED1;
    }
    }
    上面红字必须注意 因为高级定时器的时基是四个 比通用定时器多一个:




    顺便再来一个通用定时器的配置:
    void TIM4_Int_Init(u16 arr,u16 psc)
    {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能
     
    //定时器TIM4初始化
    TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
     
    TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //使能指定的TIM4中断,允许更新中断

    //中断优先级NVIC设置
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
    NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器


    TIM_Cmd(TIM4, ENABLE); //使能TIMx
    }
    //定时器4中断服务程序
    void TIM4_IRQHandler(void) //TIM3中断
    {
    if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查TIM4更新中断发生与否
    {
    TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx更新中断标志
    LED0=!LED0;
    // Get_Angle();
    }
    }
  • 相关阅读:
    rebar
    namenode ha
    jmx
    doclint in jdk8
    maven source
    avd
    ccw-ide
    ST3使用
    Web worker
    离线web-ApplicationCache
  • 原文地址:https://www.cnblogs.com/luckytimor/p/8320763.html
Copyright © 2020-2023  润新知