打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
我的Arduino笔记(2)
看门狗! 听起来就足够“高大上”的。

曾一度以为Arduino有bootloader就不会有watchdog了,但是事实上是有的。

我参考了如下两个链接:
http://tushev.org/articles/arduino/item/46-arduino-and-watchdog-timer
http://blog.csdn.net/chn89/article/details/17199171

然后写了如下代码实验。

该代码正常情况下启动watchdog,并设定watchdog定时器为1s。 loop里面每次循环开始的时候“喂狗”。
主循环loop里有按键检测,检测到pin#7上的按键按下就切换pin#13上的LED状态,启动时默认LED熄灭。
如果检测到串口有数据输入则进入死循环,watchdog定时器1s到时间后会自动重启。

实验,烧入程序后,按按键使得LED亮起,然后在电脑上打开串口终端,发送任何字符,1秒后LED会熄灭(重启后的LED初始状态),表示arduino重启了。

个人实验, 如有前辈发现问题,请多多指教。




[C] 纯文本查看 复制代码
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;
    }
}



 


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Arduino教程:按键消抖
arduino 中断例程
STM32-Stduino小白练习第四弹--按键控制LED灯闪灭&按键去抖
Sharp Dust Sensor and Arduino
Arduino关于旋转编码器程序的介绍
Arduino教程 Lesson 16:遥控灯
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服