为什么delay()导致我的arduino重置?

时间:2017-02-09 15:06:26

标签: arduino delay arduino-uno

我使用的是Arduino Uno,连接USB屏蔽,RFID屏蔽(adafruit PN532),LCD,EEPROM(24AA256)和RTC模块(DS1307)。我不会在这里发布我的代码,因为它太大而且在多个文件中分开。

在我的程序中,我意识到如果我的程序进入某个函数,在函数之后输入函数,如果我在当前函数的末尾使用delay(),则arduino将重置。我的意思的一个例子如下。

void a() { b(); }
void b() { c(); }
void c() { d(); }
void d()
{
  lcd_string("Testing", 0x80);
  delay(2000);      <---- Arduino resets at the delay here
}

起初,我认为这是因为我的动态内存是80%,当我编译时,他们说Arduino可能有一些稳定性问题。所以我修改了我的代码,使我的动态内存现在为57%。问题仍然存在。

我想也许延迟()函数有一些溢出或其他东西,所以我尝试用以下代码替换延迟。

unsigned long timing;

timing = millis();
timing += 2000;
while(millis() < timing);

Arduino仍在重置。

接下来,我想也许是因为我的arduino连接到我的电脑,一些串行引脚可能导致复位,所以我使用外部电源为arduino供电并断开USB连接。 arduino仍在重置。

接下来,我想可能Timer1可能已经使用delay()函数崩溃了,虽然延迟函数使用了Timer0,因此我禁用了Timer1。 arduino仍在重置。

我错过了其他任何可能性吗?我的程序存储空间为69%,我认为这不应成为问题。

修改

这是我的Timer1 ISR代码

ISR(TIMER1_OVF_vect)
{
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  OCR1A = 34286;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  TCCR1B |= (1 << CS12);  
  // enable timer compare interrupt
 TIMSK1 |= (1 << TOIE1);
 triggered = 1;
}

使用的任何其他标志中断都在库头文件中。 我使用以下外部库
USB主机屏蔽库2.0
Adafruit PN532大师

2 个答案:

答案 0 :(得分:0)

接近RAM损坏的一点点样本......

#define MEM_PER_LEVEL 50
#define TRY_TO_SURVIVE 10
void KillMe(int level) {
   byte dummy[MEM_PER_LEVEL];
   for ( byte  i = 0; i < MEM_PER_LEVEL; i++)
      dummy[i]= i;
   Serial.println(level);
   delay(1000);  // not sure why this would hurt more than others
   if (level < TRY_TO_SURVIVE) KillMe(level+1);
   for ( byte  i = 0; i < MEM_PER_LEVEL; i++) {
       if (dummy[i] != i) {
           Serial.println(F("corruption happened")); 
           while(1) {} // HALT
       }

  }
   if (level == 0) 
   Serial.println(F("survived"));
}
void setup() {
  Serial.begin(9600);
  KillMe(0);
}

void loop() { }

答案 1 :(得分:0)

我遇到了同样的问题-只要我在设置功能中延迟一下,Arduino就会重新启动。

对我来说,问题出在SoftwareSerial的实例中,其PIN码无效。

SoftwareSerial mySerial(30, 31);

着陆于此问题的其他任何人都应检查其引脚号是否适合他们所针对的董事会。不确定为什么只有在调用延迟时才发生崩溃,如果有人对此有所了解,会对它感兴趣!

相关问题