我已将树莓派pi 1的GPIO引脚17(在WiringPi引脚17 =引脚0中)与中断源(IR-LED发射器/接收器会在红外线被某种障碍物中断时触发中断)相连接。为了设置ISR,我一直在使用WiringPi库(我也已经在Pigpio库中尝试过,但是那里也有同样的问题)。
为了验证我是否确实在Pin17上收到了中断,我已经使用逻辑分析仪对其进行了检查,如您所见,肯定在该引脚上发生了一些中断:
这是我的代码:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <wiringPi.h>
#include "MCP3008Driver.h"
#include "DHT11.h"
#define INT_PIN 0
volatile int eventCounter = 0;
void myInterrupt(void){
printf("hello ISR!\n");
eventCounter++;
}
volatile sig_atomic_t stopFlag = 0;
static void stopHandler(int sign) { /* can be called asynchronously */
stopFlag = 1; /* set flag */
}
int main(void) {
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
// sets up the wiringPi library
if (wiringPiSetup () < 0) {
printf("Unable to setup wiring pi\n");
fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror
(errno));
return 1;
}
// set Pin 17/0 to generate an interrupt on high-to-low transitions
// and attach myInterrupt() to the interrupt
if(wiringPiISR(INT_PIN, INT_EDGE_FALLING, &myInterrupt) < 0){
printf("unable to setup ISR\n");
fprintf(stderr, "Unable to setup ISR: %s\n", strerror(errno));
}
DHT11_data data;
configureSPI();
while(1){
if(stopFlag){
printf("\n Ctrl-C signal caught! \n");
printf("Closing application. \n");
return 0;
}
//read_dht_data(&data);
int analogBoiler = readChannel(0);
int analogHeater = readChannel(1);
int analogPress = readChannel(2);
int analogACS712 = readChannel(3);
int analogDynamo = readChannel(4);
printf("Channel 0 / Boiler = %f\n", evaluateChannelValue(ePT100_BOILER, analogBoiler));
printf("Channel 1 / Heater = %f\n", evaluateChannelValue(ePT100_HEATER, analogHeater));
printf("Channel 2 / Pressure = %f\n", evaluateChannelValue(ePRESS, analogPress));
printf("Channel 3 / Power ACS712 = %f\n", evaluateChannelValue(eACS712, analogACS712));
printf("Channel 4 / Power Dynamo = %f\n", evaluateChannelValue(eDYNAMO, analogDynamo));
//printf("Humidity Environment: %f\n", data.humidity);
//printf("Temperature (Celsius) Environment: %f\n", data.temp_celsius);
// display counter value every second.
printf("%d\n", eventCounter);
sleep(5);
}
return 0;
}
connectionPiSetup和connectionPiISR方法已成功调用,并且未返回错误。
我正在使用以下链接选项构建此示例:-lwiringPi -lm -lpthread。也许我缺少链接选项?
我一直在使用this code here作为参考。那我在做什么错呢?谢谢您能给我的任何建议!
答案 0 :(得分:0)
我不确定为什么,但是我发现在wiringPiISR
的函数调用输入前面删除一元运算符可以解决我的问题。
所以不要打电话
wiringPiISR(INT_PIN, INT_FALLING_EDGE, &MyInterrupt)
致电
wiringPiISR(INT_PIN, INT_FALLING_EDGE, MyInterrupt)
我的猜测是,这与wiringPiISR
将该参数用作指针(* function)的事实有关,因此将调用地址放在它前面会导致发生奇怪的事情。尝试调用MyInterrupt
函数,对我来说,这导致我的程序崩溃!
希望这会有所帮助/也许其他人将能够详细说明为什么会发生这种情况。