2016-04-03 110 views
0

我有和arduino草图需要使用TimeAlarms.h库按照定时计划执行几个操作。然而,其中一项操作是通过中断读取霍尔传感器,似乎与TimeAlarms库的交互性很差。 我使用TimeAlarms库从这里:http://www.pjrc.com/teensy/td_libs_TimeAlarms.html 而从这里适应霍尔传感器脚本: http://www.seeedstudio.com/wiki/G3/4_Water_Flow_sensorArduino中断干扰TimeAlarms.h

我可以运行在其上的霍尔传感器代码自己的优秀。但是,当我尝试运行霍尔传感器代码以及Alarm.timerRepeat时,它在进入check_flow函数后挂起。

运行下面的代码只输出enter CF,然后挂起。如果您尝试使用DelayAlarm TimeAlarm版本的check_flow_alarm_delay函数,则会发生同样的情况。

但是,如果您在设置 和Alarm.delay(0);环路中注释掉Alarm.timerRepeat(10, showseconds);,则霍尔传感器正常工作。

奇怪的是,如果你在check_flow函数中注释掉sei();cli();,那么脚本工作正常,并且似乎可以用霍尔传感器正确计数。为什么这会工作?我应该担心我没有积极设置sei()cli()之间的时间,从而导致传感器出现可靠性问题?

注意:您应该可以在没有霍尔传感器的情况下运行代码,输出仅为0 L/hr。

// reading liquid flow rate using Seeeduino and Water Flow Sensor from Seeedstudio.com 
// Code adapted by Charles Gantt from PC Fan RPM code written by Crenn @thebestcasescenario.com 
// http:/themakersworkbench.com http://thebestcasescenario.com http://seeedstudio.com 

#include <Time.h> 
#include <TimeAlarms.h> 
#include <Wire.h> 


volatile int NbTopsFan; //measuring the rising edges of the signal 
int Calc;        
int hallsensor = 2; //The pin location of the sensor 

void rpm()  //This is the function that the interupt calls 
{ 
    NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal 
} 

void setup() // 
{ 
    Serial.begin(9600); //This is the setup function where the serial port is initialised, 

    pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input 
    attachInterrupt(0, rpm, RISING); //and the interrupt is attached 

    Alarm.timerRepeat(10, showseconds); 

} 

void loop()  
{ 
// Serial.println(second()); 

// stalls at enter CF 
// check_flow(); 

// stalls at enter CF 
    check_flow_alarm_delay(); 

    Alarm.delay(0); 
} 

void showseconds() 
{ 
    Serial.println(second()); 
} 

void check_flow() 
{ 
    Serial.println("enter CF"); 
    int Calc;  
    NbTopsFan = 0; //Set NbTops to 0 ready for calculations 
// sei();  //Enables interrupts 
    delay(1000); //Wait 1 second 
// cli();  //Disable interrupts 
    Calc = (NbTopsFan * 60/5.5); //(Pulse frequency x 60)/5.5Q, = flow rate in L/hour 

    Serial.print (Calc, DEC); //Prints the number calculated above 
    Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a new line 
} 


void check_flow_alarm_delay() 
{ 
    Serial.println("enter CFAD"); 
    int Calc;  
    NbTopsFan = 0; //Set NbTops to 0 ready for calculations 
// sei();  //Enables interrupts 
    Alarm.delay(1000); //Wait 1 second 
// cli();  //Disable interrupts 
    Calc = (NbTopsFan * 60/5.5); //(Pulse frequency x 60)/5.5Q, = flow rate in L/hour 

    Serial.print (Calc, DEC); //Prints the number calculated above 
    Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a new line 
} 

回答

1

delay()使用中断。禁用它们会干扰该功能。

+0

这似乎是你的问题。你可能不得不重写你的代码而不使用中断。在http://stackoverflow.com/questions/36382676/arduino-uno-r3-input-pins-with-gsm-shield/36392173上查看我的回复(附带示例代码) –