ST意法半导体
直播中

李杰

7年用户 1335经验值
私信 关注
[问答]

为什么用NUCLEO板上编写的中断代码触发LED会失败呢

我正在尝试使用我的 NUCLEO 板(PC13)上的用户按钮编写一个中断代码来触发 LED(PB14)。然而,正如我从调试器中观察到的那样,第 13 行的挂起位始终为 1,而根本没有按下按钮。
EXti->PR1 = 1<<13 仍在执行,但无法清除未决位。
有人可以指出我的问题吗?这是我的代码。
谢谢你。
  • /*--Header--------------------------------------------------------------------------------*/
  • #include
  • #include
  • #include "STM32l4r5xx.h"
  • /*--Define--------------------------------------------------------------------------------*/
  • /*--Global-Variable---------------------------------------------------------------------- */
  • /*--Function Prototypes-------------------------------------------------------------------*/
  • void initPort (void);
  • void Blink(void);
  • /*--Main----------------------------------------------------------------------------------*/
  • int main()
  • {
  •         initPort();
  •   while(1)
  •   {
  •   }
  • } // EO Main
  • /*--Function------------------------------------------------------------------------------*/
  • /************************************************/
  • /*             Name: initPort                   */
  • /*-Initiating PC13 as input/interrupt           */
  • /*-Initiating PB14 as LED red                        */
  • /*                                              */
  • /************************************************/
  • void initPort(void)
  • {
  •         RCC->AHB2ENR |= 0x04;// Enable GPIO port D
  •         GPIOC->MODER &= ~(1<<26);
  •         GPIOC->MODER &= ~(1<<27); // Enable PC13 as input - Button
  •         EXTI->RTSR1 |= 0x1<<13; // Enable EXTI15-PD13 triggers in raising edge
  •         EXTI->IMR1 |= 0x1<<13; // Un-mask EXTI15 to enable the interrupt in PD13
  •         NVIC->ISER[1] |= 1<<8; //Enable EXTI15_10 - vector position 40
  •         RCC->AHB2ENR |= 1<<1; //Enable GPIO port B
  •         GPIOB->MODER &= ~(1<<29);
  •         GPIOB->MODER |= 1<<28; //Set PD14 as general output LED3
  • }
  • void Blink(void)
  • {
  •         GPIOB->ODR ^= 1<<14; // toggle pin PB14 - LED3
  •         for(int i=0; i<10000; i++);
  • }
  • void EXTI15_10_IRQHandler (void)
  • {
  •         if (EXTI->PR1 & 1<<13) // check if the pending register is on
  •                 {
  •                         EXTI->PR1 = 1<<13; // clear the pending register by writing 1 to it
  •                 }
  •         Blink();
  • }









回帖(1)

袁富存

2022-12-8 16:43:16
在 SYSCFG->EXTICR[] 中设置 EXTI 源。
复位时,所有 EXTI 源都映射到 GPIOA,因此 EXTI13 由 PA13 上的边沿触发。这恰好是 SWDIO 引脚,因此每次调试器探测器发出某些请求时都会触发它。(禁用板载调试器,它应该停止闪烁)
举报

更多回帖

发帖
×
20
完善资料,
赚取积分