简体   繁体   English

STM32中断Handeling如果条件

[英]STM32 Interrupt Handeling if condition

How I could have 2 interrupts with one handler by this code below: 我如何通过以下代码使用一个处理程序进行2次中断:

SYSCFG->EXTICR[0] |= SYSCFG_EXTICR1_EXTI0_PB | SYSCFG_EXTICR1_EXTI1_PC;
EXTI->IMR = EXTI_IMR_MR0 | EXTI_IMR_MR1;
EXTI->RTSR = EXTI_RTSR_TR0| EXTI_RTSR_TR1;
/* Configure NVIC for External Interrupt */
/* (6) Enable Interrupt on EXTI0_1 */
/* (7) Set priority for EXTI0_1 */
NVIC_EnableIRQ(EXTI0_1_IRQn); /* (6) */
NVIC_SetPriority(EXTI0_1_IRQn,0); /* (7) */

This is the code that the handler excecute: 这是处理程序执行的代码:

void EXTI0_1_IRQHandler(void)
{
    if ((EXTI->PR & EXTI_PR_PR1) == EXTI_PR_PR1)  /* Check line 1 has triggered the IT */
  {
    EXTI->PR = EXTI_PR_PR1; /* Clear the pending bit */
    GPIOC->ODR |= 1<<0;
  }
  if ((EXTI->PR & EXTI_PR_PR0) == EXTI_PR_PR0)  /* Check line 0 has triggered the IT */
  {
    EXTI->PR = EXTI_PR_PR0; /* Clear the pending bit */
    GPIOC->ODR &= ~(1<<0);
 }
} 

The code works fine when I click on the button that is connected to PC1, the LED turns on and when I click on the button that is connected to PB0 the LED turns off. 当我点击连接到PC1的按钮时,代码工作正常,LED亮起,当我点击连接到PB0的按钮时,LED熄灭。 In my if structures I check which line is active but I also want the LED only turns on by clicking on PC1 and not with a click on another pin on line 1, the same for line 0 but I don't know how I can change the conditions for the if structures. 在我的if结构中,我检查哪条线是活动的,但我也希望LED只通过点击PC1而不是点击第1行的另一个引脚而打开,对于第0行也是如此,但我不知道如何更改if结构的条件。

The micro-controller is a STM32F091. 微控制器是STM32F091。

  1. First: you can't connect more than one pin (A..Fx) per EXTIx line (see RM0091 page 177). 第一:您不能为每个EXTIx线连接多个引脚(A..Fx)(参见RM0091第177页)。 So EXTI line 0 IRQ is strictly correspond to one pin: C0 in your code. 所以EXTI第0行IRQ严格对应一个引脚:代码中的C0。
  2. Second: don't use IRQs for serve buttons. 第二:不要将IRQ用于服务按钮。 You must implement bounce filter and best idea for this is check button's pin state by timer. 你必须实现反弹过滤器,最好的想法是由定时器检查按钮的引脚状态。 Human reaction is about 200ms, really pushed button will produce pulse with duration 100-200ms. 人体反应约200ms,真正按下按钮会产生持续时间为100-200ms的脉冲。 So you need timer with 12-24ms and two bytes in RAM for each button... See code example bellow: 因此,对于每个按钮,您需要12-24ms的定时器和RAM中的两个字节...请参阅下面的代码示例:

     uint8_t btn_state = (uint8_t)0x0, btn_mask = (uint8_t)0x1; void some_tim_irq_handler(void) { if (GPIOC->IDR & (uint16_t)0x1) { // PC0 up btn_state |= btn_mask; } else { // PC0 down btn_state &= (uint8_t)~btn_mask; } btn_mask <<= (uint8_t)0x1; // mask cycle if (btn_state == (uint8_t)0x0) { // One state return; } if (btn_state == (uint8_t)0xFF) { // Second state } } 

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

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