简体   繁体   English

STM32F103 Timer2不中断

[英]STM32F103 Timer2 not interrupting

Background 背景

I'm relatively inexperienced with the STM32 series, so I'm sure that this is simple and I'm missing a setup somewhere. 我对STM32系列相对缺乏经验,所以我确定这很简单,而且我在某处缺少设置。

I am trying to set up the timer to simply interrupt on an update event, which should be when the counter rolls over at the TIM2->ARR value. 我正在尝试将计时器设置为仅在更新事件时中断,这应该是在计数器以TIM2->ARR值翻转时发生的。

  • I am currently setting a breakpoint inside the timer interrupt and it is simply not triggering 我当前在计时器中断中设置一个断点,而只是不触发
  • I have tried to use other timer modules 我试图使用其他计时器模块
  • The counter is counting (I can observe through the debugger) 计数器正在计数(我可以通过调试器观察)
  • The registers are loaded appropriately based on the below code 根据以下代码适当加载寄存器
  • The TIM2->SR UIF (update interrupt flag) is set when the counter rolls over 计数器翻转时设置TIM2->SR UIF (更新中断标志)

Code

void TIM_init(void){
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

    TIM2->PSC = 1000;
    TIM2->ARR = 1000;
    TIM2->DIER = TIM_DIER_UIE;
    TIM2->EGR = TIM_EGR_UG;
    NVIC_EnableIRQ(TIM2_IRQn);

    DBGMCU->CR |= DBGMCU_CR_DBG_TIM2_STOP;
    TIM2->CR1 |= TIM_CR1_CEN;
}

void TIM2_IRQHandler(void){
    TIM2->SR &= ~TIM_SR_UIF;    // clear the interrupt flag
}

I also tried setting the priority grouping as follows, same results: 我还尝试将优先级分组设置如下,结果相同:

void TIM_init(void){
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

    TIM2->PSC = 10;
    TIM2->ARR = 1000;
    TIM2->DIER = TIM_DIER_UIE;
    TIM2->EGR = TIM_EGR_UG;

    // Enable the Timer2 Interrupts
    uint32_t priorityGroup, priority;
    priorityGroup = NVIC_GetPriorityGrouping();
    priority = NVIC_EncodePriority(priorityGroup, 3, 6);
    NVIC_SetPriority(TIM2_IRQn, priority);

    NVIC_EnableIRQ(TIM2_IRQn);

    DBGMCU->CR |= DBGMCU_CR_DBG_TIM2_STOP;
    TIM2->CR1 = TIM_CR1_CEN;
}

The project also contains an assembly file startup_stm32f10x.s . 该项目还包含一个程序集文件startup_stm32f10x.s An excerpt from that file: 该文件的摘录:

__vector_table
        DCD     sfe(CSTACK)
        DCD     Reset_Handler             ; Reset Handler
        DCD     NMI_Handler               ; NMI Handler
        DCD     HardFault_Handler         ; Hard Fault Handler.....

which continues on, including the TIM2_IRQHandler . 继续,包括TIM2_IRQHandler This indicates to me that there is a vector table there. 这向我表明那里有一个向量表。

It might not be an exact solution but more of a suggestion untill someone gives a better solution for you. 可能不是确切的解决方案,而是更多的建议,直到有人为您提供更好的解决方案。

Case 1 : If your task needs only few driver initialization like interrupt and you have enough time, then you can go for obvious reading of the whole initialization chapter, for your case interrupt chapter. 情况1 :如果您的任务只需要很少的驱动程序初始化(例如中断),并且您有足够的时间,那么您可以轻松阅读整个中断章节的整个初始化章节。 Keep an eye for the following points. 请注意以下几点。

  • Small foot notes in the chapter. 本章中的小脚注。
  • See the chapter related to clock for exception 请参阅与时钟相关的章节以了解异常
  • See the chapter related to Power for exception 请参阅与电源相关的章节以了解例外情况

In some cases the datasheet writes initialization related information in the clock and power chapter which occasionally caused a desired driver not to function. 在某些情况下,数据表在时钟和功率章节中写入了与初始化相关的信息,这有时会导致所需的驱动器无法工作。

Case 2 : If you need several initialization and lots of development to be done in limited time then here is an alternative approach. 情况2 :如果您需要在有限的时间内进行多次初始化和大量开发工作,那么这里是另一种方法。

During my work with different kind of µc what I found is that it is better to leave the lower level initialization for the manufacturers, at least at beginning stages. 在使用不同种类的µc进行工作时,我发现,最好是至少在开始阶段,将较低级别的初始化留给制造商。 Now a days most of the manufacturers have options where you can create a blank skeleton project online or with some kind of software. 如今,大多数制造商都可以选择在网上或使用某种软件来创建空白的骨架项目。 For your case it is STM32Cube initialization code generator . 对于您的情况,它是STM32Cube初始化代码生成器

The advantages being you can select your µc and related drivers and they will create a full skeleton project with all initialization and sometimes fill it up with example code for you to start. 优点是您可以选择µc和相关驱动程序,它们将创建带有所有初始化的完整框架项目,有时还会用示例代码填充该项目以供您启动。 It reduces developer effort to go through the lower level initialization and directly go for development. 它减少了开发人员进行较低级别的初始化并直接进行开发的精力。 You can also see their code to get the grasp of how the driver has been initialized and do relevant modification. 您还可以查看他们的代码,以了解如何初始化驱动程序并进行相关修改。 Keep a close eye on the comments in their initialization code for understanding or any framework documentation that comes with it. 请密切注意初始化代码中的注释,以便理解或随附注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM