Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何禁用然后重新启用Arduino的看门狗中断?_Arduino_Watchdog - Fatal编程技术网

如何禁用然后重新启用Arduino的看门狗中断?

如何禁用然后重新启用Arduino的看门狗中断?,arduino,watchdog,Arduino,Watchdog,我正试图使用看门狗中断作为计时器,与我的Arduino一起睡眠一段时间。我的问题在于,当我醒来时,我需要执行的操作将花费8秒以上的时间 目前,我的Arduino将睡眠1分钟,使用看门狗的连续中断来唤醒它并使其重新进入睡眠状态。然而,1分钟后,我开始执行耗时超过8秒的操作,看门狗中断超时 我想关闭看门狗定时器,进行操作,然后重新启用它并返回睡眠状态 这是我的密码: #include "Adafruit_FONA.h" #include <avr/sleep.h> #include &l

我正试图使用看门狗中断作为计时器,与我的Arduino一起睡眠一段时间。我的问题在于,当我醒来时,我需要执行的操作将花费8秒以上的时间

目前,我的Arduino将睡眠1分钟,使用看门狗的连续中断来唤醒它并使其重新进入睡眠状态。然而,1分钟后,我开始执行耗时超过8秒的操作,看门狗中断超时

我想关闭看门狗定时器,进行操作,然后重新启用它并返回睡眠状态

这是我的密码:

#include "Adafruit_FONA.h"
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>

#define FONA_RX 10
#define FONA_TX 9
#define FONA_RST 4
#define LED_PIN 8

// this is a large buffer for replies
char replybuffer[255];
char *SMSnumber = "6015962842";
char stack[128] = {'c'};

//Value for watchdog timer interrupt.
volatile int f_wdt = 1;
int seconds = 0;
int minutes = 1;
int hours = 0;
int interval = ((hours*60*60) + (minutes*60) + (seconds))/8;
int timerCounter = 0;

//Setup for pulse sensor.
volatile int Signal;                // holds the incoming raw data
int pulsePin = 0;                   //Pin to read at analog 0 for the pulse.
volatile int IBI = 600;             // int that holds the time interval between beats! Must be seeded!
volatile boolean Pulse = false;     // "True" when User's live heartbeat is detected. "False" when not a "live beat".
volatile int BPM;                   // int that holds raw Analog in 0. updated every 2mS
volatile boolean QS = false;        // becomes true when Arduoino finds a beat.
unsigned long lastTime; // used to time the Pulse Sensor samples
unsigned long thisTime; // used to time the Pulse Sensor samples

//ISR for watchdog timer.
ISR(WDT_vect)
{
  if(f_wdt == 0)
  {
    f_wdt=1;
    timerCounter++;
  }
  else
  {
    Serial.println("WDT Overrun!!!");
  }
}

// We default to using software serial. If you want to use hardware serial
// (because softserial isnt supported) comment out the following three lines 
// and uncomment the HardwareSerial line
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;

// Hardware serial is also possible!
//HardwareSerial *fonaSerial = &Serial;

// Use this for FONA 800 and 808s
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

void setup() {
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);
  delay(3000);
  digitalWrite(LED_PIN, LOW);
  while (!Serial);
  Serial.begin(115200);
  setupGsm();
  setupWdt();
}

void loop()
{
  if(f_wdt == 1)
  {
    if (timerCounter == interval)
    {
      WDTCSR |= _BV(WDTON);
      Serial.println(F("Tried to stop the watchdog."));
      delay(20000);

      //Reset timer.
      timerCounter = 0;
      WDTCSR |= _BV(WDIE);
      Serial.println(F("Tried to re-enable watchdog."));
    }

    /* Don't forget to clear the flag. */
    f_wdt = 0;

    /* Re-enter sleep mode. */
    enterSleep();
  }
  else
  {
    /* Do nothing. */
  }
}

void setupGsm()
{
  fonaSerial->begin(4800);
  while (! fona.begin(*fonaSerial)) {
    Serial.println(F("Couldn't find FONA"));
    delay(1000);
  }
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
  digitalWrite(LED_PIN, HIGH);
}

void enterSleep(void)
{
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();

  /* Now enter sleep mode. */
  sleep_mode();

  /* The program will continue from here after the WDT timeout*/
  sleep_disable(); /* First thing to do is disable sleep. */

  /* Re-enable the peripherals. */
  power_all_enable();
}

void setupWdt()
{
  /*** Setup the WDT ***/

  /* Clear the reset flag. */
  MCUSR &= ~(1<<WDRF);

  /* In order to change WDE or the prescaler, we need to
   * set WDCE (This will allow updates for 4 clock cycles).
   */
  WDTCSR |= (1<<WDCE) | (1<<WDE);

  /* set new watchdog timeout prescaler value */
  WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */

  /* Enable the WD interrupt (note no reset). */
  WDTCSR |= _BV(WDIE);
}
#包括“Adafruit_FONA.h”
#包括
#包括
#包括
#定义FONA_RX 10
#定义FONA_TX 9
#定义FONA_RST 4
#定义LED_引脚8
//这是一个很大的回复缓冲区
char replybuffer[255];
char*SMSnumber=“6015962842”;
字符堆栈[128]={c'};
//看门狗定时器中断的值。
volatile int f_wdt=1;
整数秒=0;
整数分钟=1;
整小时=0;
整数间隔=((小时*60*60)+(分钟*60)+(秒))/8;
int timerCounter=0;
//脉冲传感器的设置。
易失性int信号;//保存传入的原始数据
int-pulsePin=0//在模拟0处读取脉冲的引脚。
易失性积分IBI=600;//保存两次拍之间的时间间隔的int!必须播种!
易失性布尔脉冲=假;//当检测到用户的实时心跳时为“True”。“假”不是“活拍”。
易失性int BPM;//在0中保存原始模拟值的int。每2毫秒更新一次
易失性布尔QS=false;//当Arduoino找到一个节拍时变为真。
未签名的长lastTime;//用于为脉冲传感器采样计时
这次未签名的long;//用于为脉冲传感器采样计时
//监视定时器的ISR。
行业特殊风险(WDT_vect)
{
如果(f_wdt==0)
{
f_wdt=1;
时间计数器++;
}
其他的
{
Serial.println(“WDT溢出!!!”;
}
}
//我们默认使用串行软件。如果你想使用硬件串口
//(因为不支持softserial)注释掉以下三行
//并取消对HardwareSerial行的注释
#包括
SoftwareSerial fonaSS=SoftwareSerial(FONA_TX,FONA_RX);
SoftwareSerial*fonaSerial=&fonaSS;
//硬件串行也是可能的!
//硬件目录*基金目录=&Serial;
//用于FONA 800和808s
Adafruit_FONA FONA=Adafruit_FONA(FONA RST);
无效设置(){
引脚模式(LED_引脚,输出);
数字写入(LED_引脚,高);
延迟(3000);
数字写入(LED_引脚,低电平);
而(!串行);
序列号开始(115200);
setupGsm();
setupWdt();
}
void循环()
{
如果(f_wdt==1)
{
if(timerCounter==间隔)
{
WDTCSR |=_BV(WDTON);
Serial.println(F(“试图阻止看门狗”);
延迟(20 000);
//重置计时器。
时间计数器=0;
WDTCSR |=_BV(WDIE);
Serial.println(F(“试图重新启用监视程序”);
}
/*别忘了清除旗帜*/
f_wdt=0;
/*重新进入睡眠模式*/
进入睡眠();
}
其他的
{
/*什么也不做*/
}
}
void setupGsm()
{
基金会->开始(4800);
而(!fona.begin(*fonaSerial)){
Serial.println(F(“找不到FONA”);
延迟(1000);
}
数字写入(LED_引脚,高);
延迟(500);
数字写入(LED_引脚,低电平);
延迟(500);
数字写入(LED_引脚,高);
延迟(500);
数字写入(LED_引脚,低电平);
延迟(500);
数字写入(LED_引脚,高);
}
无效进入睡眠(无效)
{
设置睡眠模式(睡眠模式关闭);
sleep_enable();
/*现在进入睡眠模式*/
睡眠模式();
/*WDT超时后,程序将从此处继续*/
sleep_disable();/*首先要做的是禁用睡眠*/
/*重新启用外围设备*/
电源全部启用();
}
void setupWdt()
{
/***设置WDT***/
/*清除重置标志*/

MCUSR&=~(1使用
avr/wdt.h
库中的
wdt_enable()
wdt_disable()
来启动和停止看门狗定时器,而不是直接从代码访问控制器的寄存器

此外,为了系统的可靠性,最好实际保持看门狗计时器运行(而不是禁用它),并在长循环和函数中定期调用
wdt\u reset()
,以防止不适当的系统重置


例如,您可以将代码中的
delay(20000);
行替换为重复20次语句的循环:
delay(1000);wdt\u reset();
而不是直接从代码访问控制器的寄存器,使用
wdt\u enable()
wdt\u disable()
avr/wdt.h
库启动和停止看门狗定时器

此外,为了系统的可靠性,最好实际保持看门狗计时器运行(而不是禁用它),并在长循环和函数中定期调用
wdt\u reset()
,以防止不适当的系统重置


例如,您可以将代码中的
delay(20000);
行替换为重复语句20次的循环:
delay(1000);wdt_reset()“你是不是已经加上了标签?”你想回答哪个问题?你想回答哪个语言?请编辑这个问题,删除一个标签并保留适当的一个。@托比:从文本中,它是ARDUINO。C和C++都不是。这是C++还是C++?你都标记了,你想回答哪个语言?请编辑问题以移除。一个标签,并保留适当的一个。@托比:从文本中,它是ARDUNO。既不是C也不是完全C++。