Arduino中断干扰TimeAlarms.h

时间:2016-04-03 20:57:03

标签: arduino

我有和arduino sketch需要使用TimeAlarms.h库在定时计划上进行多项操作。但是,其中一项操作是通过中断读取霍尔传感器,似乎与TimeAlarms库的交互性很差。 我在这里使用TimeAlarms库:http://www.pjrc.com/teensy/td_libs_TimeAlarms.html 并从这里改编了大厅传感器脚本: http://www.seeedstudio.com/wiki/G3/4_Water_Flow_sensor

我可以自己运行霍尔传感器代码。但是,当我尝试与Alarm.timerRepeat一起运行霍尔传感器代码时,它会在输入check_flow函数后挂起。

运行下面的代码只输出enter CF然后挂起。如果您尝试使用check_flow_alarm_delay函数,而使用TimeAlarm版本的Delay。

,则会出现同样的情况

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

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

注意:您应该能够在没有霍尔传感器的情况下运行代码,输出将仅为0升/小时。

// 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 个答案:

答案 0 :(得分:1)

{{1}}使用中断。禁用它们会干扰功能。