问题描述
我编写了以下代码来对基于 ARM Cortex-M4 处理器内核的 STM32F439 微控制器进行编程。我定义了一个定时器中断处理程序,它在每次 TIM7 计数到 1 秒结束时触发,以便它每秒执行一段指定的代码。函数 InitRCC()(初始化 RCC 以启用 GPIO)和 ConfGPIO()(配置 GPIO 引脚)的内容被省略。
#include "main.h"
#include "stm32f439xx.h"
#include "core_cm4.h"
#include "gpioControl.h"
// Configure Timer 7 and automatically start the timer
void ConfTimer7()
{
RCC->APB1ENR |= RCC_APB1ENR_TIM7EN;
// Reset the peripheral interface
RCC->APB1RSTR |= RCC_APB1RSTR_TIM7RST;
// Wait a minimum of two clock cycles
__ASM("nop");
__ASM("nop");
// Clear the reset bit
RCC->APB1RSTR &= ~(RCC_APB1RSTR_TIM7RST);
// Wait a minimum of two clock cycles
__ASM("nop");
__ASM("nop");
// disable Timer 7
TIM7->CR1 &= ~(TIM_CR1_CEN);
// Clear the prescaler register of Timer 7
TIM7->PSC &= ~(TIM_PSC_PSC_Msk);
// Set the prescaler value of Timer 7 to 24
TIM7->PSC |= 2499; // Timer 7 frequency = 42*10^6/(2499+1) = 16.8 kHz
// Clear the auto-reload register of Timer 7
TIM7->ARR &= ~(TIM_ARR_ARR_Msk);
// Set the count to 16800 (count to 1s)
TIM7->ARR |= 16800;
// Set Timer 7 to run in "free-run" mode
TIM7->CR1 &= ~(TIM_CR1_OPM);
// Enable timer interrupt for Timer 7
TIM7->DIER |= TIM_DIER_UIE;
// Enable Timer 7
TIM7->CR1 |= TIM_CR1_CEN;
}
void TIM7_IRQHandler()
{
TIM7->SR &= ~(TIM_SR_UIF); // Clear the timer interrupt flag
// Code to be executed every second
}
int main(void)
{
InitRCC();
ConfGPIO();
// disable interrupts before configuring the system
_disable_irq();
// Set the timer interrupt to priority 0
NVIC_SetPriority(TIM7_DAC_IRQn,0);
// Configure Timer 7
ConfTimer7();
// Enable the global interrupt system
_enable_irq();
while (1)
{
}
}
当我尝试在 Keil µVision 5 中构建目标时,显示以下警告和错误:
src\main.c(526): warning: #223-D: function "_disable_irq" declared implicitly
_disable_irq();
src\main.c(529): error: #20: identifier "TIM7_DAC_IRQn" is undefined
NVIC_SetPriority(TIM7_DAC_IRQn,0);
src\main.c(535): warning: #223-D: function "_enable_irq" declared implicitly
_enable_irq();
如何修复这些错误和警告?我是否需要添加更多头文件,以便定义函数 void _enable_irq(void)
和 void _disable_irq(void)
以及标识符“TIM7_DAC_IRQn”?或者在现有的头文件中是否有这些函数或标识符的替代方法?
解决方法
切勿直接包含 core_cm4.h 或 stm32f439xx.h。
您需要使用命令行标志定义正确的部件号宏 STM32F439xx,例如:-DSTM32F439xx
。
之后,您应该只包含 "stm32f4xx.h"
。这将包括定义 _enable_irq
和 _disable_irq
的正确 CMSIS 标头以及部件的所有有效 IRQ 编号。
关于 TIM7_DAC_IRQn,这是不正确的。 DAC 与 TIM6 共享一个中断,TIM7 有自己独立的中断。选择 TIM6_DAC_IRQn
或 TIM7_IRQn
。
矢量表定义在启动文件中,而不是 CMSIS。向量表必须放在内存中的特定位置,因此还需要适当的链接描述文件。
要拥有所有这些遗漏的部分,我宁愿建议使用 CubeMx,创建项目并提取所需的文件。链接脚本、启动文件等