小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

我的Arduino筆記(2)

 quasiceo 2015-08-23
看門狗! 聽起來就足夠“高大上”的。

曾一度以為Arduino有bootloader就不會有watchdog了,但是事實上是有的。

我參考了如下兩個鏈接:
http:///articles/arduino/item/46-arduino-and-watchdog-timer
http://blog.csdn.net/chn89/article/details/17199171

然后寫了如下代碼實驗。

該代碼正常情況下啟動watchdog,并設(shè)定watchdog定時器為1s。 loop里面每次循環(huán)開始的時候“喂狗”。
主循環(huán)loop里有按鍵檢測,檢測到pin#7上的按鍵按下就切換pin#13上的LED狀態(tài),啟動時默認(rèn)LED熄滅。
如果檢測到串口有數(shù)據(jù)輸入則進入死循環(huán),watchdog定時器1s到時間后會自動重啟。

實驗,燒入程序后,按按鍵使得LED亮起,然后在電腦上打開串口終端,發(fā)送任何字符,1秒后LED會熄滅(重啟后的LED初始狀態(tài)),表示arduino重啟了。

個人實驗, 如有前輩發(fā)現(xiàn)問題,請多多指教。




[C] 純文本查看 復(fù)制代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
    Button Pressing Testing
*/
#include <avr/wdt.h>
#define BUTTON_PIN          7       // Button pin
#define LED_PIN             13      // Led pin
#define BUTTONS_SAMPLES     6000   // Affect the sensitivity of the button
#define BUTTON_PRESSED      LOW     // The state of the pin when button pressed
unsigned int o_prell          = 0;      // counter for button pressing detection
boolean button_state          = false
unsigned int led_state        = LOW;    // Led off at the beginning
void setup()
{
    Serial.begin(9600);
    pinMode(BUTTON_PIN, INPUT);
    pinMode(LED_PIN, OUTPUT);
     
    // set initial LED state
    digitalWrite(LED_PIN, led_state);
    wdt_enable(WDTO_1S);    // enable the watchdog timer : 1 second timer
}
void loop()
{
    wdt_reset();    // feed the dog
    check_button();
    digitalWrite(LED_PIN, led_state);
    if (Serial.available()>0)
    {
        while(1) ;
    }
}
void check_button()
{
    int button_input    =   digitalRead(BUTTON_PIN);
    if ((button_input == BUTTON_PRESSED) && (o_prell < BUTTONS_SAMPLES))
    {
        o_prell++;      // counting for button pressing
    }
    else if ((button_input == BUTTON_PRESSED) && (o_prell == BUTTONS_SAMPLES) && !button_state)
    {
        button_state = true;    // button pressed
        //led_state = HIGH;
        led_state = !led_state;
    }
    else if ((button_input != BUTTON_PRESSED) && (o_prell > 0))
    {
        o_prell--;      // counting for button releasing,  or debouncing / immunity
    }
    else if ((button_input != BUTTON_PRESSED) && (o_prell == 0) && button_state)
    {
        button_state = false;
        //led_state = LOW;
    }
}



 


    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多