树莓派外部中断的几点注意事项(未验证)

  1. 是与主函数并行的,可以认为是给回调函数新开了一个线程。
  2. 没有取消中断和关闭中断标志位的操作(stm32有),进入中断函数前会先清除中断标志位,所以执行中断函数中触发的中断能且只能记录一个,如果想屏蔽进入中断函数的中断,防止再次进入中断函数,可以在回调函数中先判断当前的电平状态。

使用(来自微雪教程)

/* Interrupt.c
 * you can build this like: 
 * gcc -Wall Interrupt.c -o Interrupt -lwiringPi
 * sudo ./Interrupt
*/
#include <stdio.h>
#include <wiringPi.h>

#define button 28
char flag = 0;
void myInterrupt()
{
    flag ++;
}

int main()
{
    if(wiringPiSetup() < 0)return 1;
    pinMode(button,INPUT);
    pullUpDnControl(button,PUD_UP);
    if(wiringPiISR(button,INT_EDGE_RISING,&myInterrupt) < 0)
    {
        printf("Unable to setup ISR \n");
    }
    printf("Interrupt test program\n");
    while(1)
    {
        if(flag)
        {
            while(digitalRead(button) ==0);
            printf("button press\n");
            flag = 0;
        }
    }
}

gpio的占用错误

在真正使用树莓派的时候,我发现了gpio口被占用的问题。

# 运行后报错
gpio: Unable to open GPIO direction interface for pin 26: No such file or directory
wiringPiISR: unable to open /sys/class/gpio/gpio26/value: No such file or directory
#查看资源占用情况
cat /sys/kernel/debug/gpio 
#我这里显示26号引脚开启了lirc服务
gpiochip0: GPIOs 0-53, parent: platform/3f200000.gpio, pinctrl-bcm2835:
gpio-26  (                    |ir-receiver@1a      ) in  hi IRQ ACTIVE LOW
gpio-29  (                    |led0                ) out lo ACTIVE LOW
#卸载此按键上的功能即可

引用CSDN的介绍--->

代码来源-微雪教程

Last modification:February 16th, 2023 at 01:37 pm