欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: mega328p watchdog 無效解決方案 [打印本頁]

作者: liuda    時間: 2015-1-22 23:40
標題: mega328p watchdog 無效解決方案
以前一直用mega2560,好不容易解決了watchdog的問題,看門狗正常工作。
新項目中boss要求mega328p也能夠支持看門狗,想想應該是比較容易的,畢竟avr官方文檔中有對watchdog的說明。這個坑就這樣埋下了。
寫一個看門狗測試程序:

#include <avr/wdt.h>
const int ledPin =  13;      // the number of the LED pin

void setup() {
  wdt_disable();
  Serial.begin(9600);
  Serial.println("System Init OK!");
  pinMode(ledPin, OUTPUT);
  Serial.println("Wait 5 Sec..");
  delay(5000);
  wdt_enable(WDTO_8S);
  /*
    WDTO_15MS
    WDTO_30MS
    WDTO_60MS
    WDTO_120MS
    WDTO_250MS
    WDTO_500MS
    WDTO_1S
    WDTO_2S
    WDTO_4S
    WDTO_8S
  */
  Serial.println("Watchdog enabled!");
}


uint8_t timer = 0;
void loop() {


  if (!(millis() % 1000)) {
    Serial.print(millis());
    Serial.print("--");
    timer++;
    Serial.println(timer);
    digitalWrite(ledPin, digitalRead(ledPin) == 1 ? 0 : 1); delay(1);
  }
  //  wdt_reset();
}
第一次運轉正常,成功復位,小case,隨便就搞定了。However.....

復位之后居然就一直復位了...一直無限循環復位,像下面這樣....
System Init OK!
Wait 5 Sec..
Watchdog enabled!
5000--1
6000--2
7000--3
8000--4
9000--5
10000--6
11000--7
12000--8
13000--9
System Init OK!

我去,這怎么回事。猜測可能是bootloader的問題,搞個optiboot來看看,聽說對watchdog支持十分良好。
燒錄上之后,依然這個問題。
解決方案:http://www.nongnu.org/avr-libc/u ... _avr__watchdog.html
這里的說明:
Note that for newer devices (ATmega88 and newer, effectively any AVR that has the option to also generate interrupts), the watchdog timer remains active even after a system reset (except a power-on condition), using the fastest prescaler value (approximately 15 ms). It is therefore required to turn off the watchdog early during program startup, the datasheet recommends a sequence like the following:
對于atmega88以及新型號的單片機(自帶產生中斷的),看門狗可能會在系統復位之后,依然運行(除掉電復位外)。因此,需要在程序啟動早期,關閉看門狗。datasheet中推薦插入一段這樣的程序:

#include <stdint.h>
#include <avr/wdt.h>

uint8_t mcusr_mirror __attribute__ ((section (".noinit")));

void get_mcusr(void) \
__attribute__((naked)) \
__attribute__((section(".init3")));
void get_mcusr(void)
{
mcusr_mirror = MCUSR;
MCUSR = 0;
wdt_disable();
}
然后,問題解決,正常復位.. 還是的多看datasheet..哎

作者: krommel    時間: 2015-4-11 11:15
能把最終代碼上傳下么




歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1