STM32F103C8 定时器中断问题

问题描述 投票:0回答:1

我是STM32的新手,我有一个关于中断的问题。 我为 STM32F103C8T6 MCU 编写了以下用于定时器中断的代码,但是当我使用 __enable_irq() 命令激活中断时, while(1) 中的代码不会运行。 它表明 __enable_irq() 有一个无限循环,并且程序不会离开它。 请帮我看看我的问题是什么。 谢谢你。

#include "stm32f10x.h"

#define TIME2EN 0

void configure_pc13(void);

void configure_timer(void);

void configure_clock(void);

volatile uint64_t t_counter = 0;

int main(void){ 
    __disable_irq();
    
    configure_clock();
    
    configure_pc13();
    
    configure_timer();
    
    NVIC_EnableIRQ(TIM2_IRQn);
    
    __enable_irq();
    
    while(1){
        if(t_counter >= 40){
            GPIOC->ODR ^= (0x1 << 13);
            t_counter = 0;
        }
    }
}

void TIM2_IRQHandler(void){
    t_counter++;
    TIM2->SR &= ~TIM_SR_UIF;
}

void configure_clock(void){
    RCC->CR &= ~( (0x1UL << 0) | (0x1UL << 16) );
    
    RCC->CR |= (0x1 << 16);
    
    while( !( ( RCC->CR >> 17 ) & 1 ) );
}

void configure_pc13(void){
    RCC->APB2ENR |= (1 << 4);
    
    GPIOC->CRH &= ~(0xFUL << 20);
    
    GPIOC->CRH |= (0x1 << 20);
    
    GPIOC->ODR |= (0x1 << 13);
}

void configure_timer(void){
    RCC->APB1ENR |= (0x1 << TIME2EN);
    
    TIM2->PSC = 1;
    TIM2->ARR = 8;
    
    TIM2->CR1 = 1;
    
    TIM2->DIER |= 1;
}

我在 TIM2_IRQHandler 函数中使用了 __disable_irq(),但 t_counter 变量计数不正确。

timer interrupt stm32f1
1个回答
0
投票

当您将 ARR 设置为 8,PSC 设置为 1 时,每 (1+1)*(8+1)=18 个机器周期就会有一个中断。最小中断进入/退出为 12+12 个周期,并且不包括中断服务程序 (ISR) 中执行的任何指令。

换句话说,您的程序一遍又一遍地执行 ISR,而没有执行其他任何操作。

你根本不可能拥有这样的中断率。

© www.soinside.com 2019 - 2024. All rights reserved.