“第五十一课--睡眠模式/zh”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
设备
程序
第72行: 第72行:
  
 
<source lang="cpp">
 
<source lang="cpp">
 +
  
 
#include <avr/sleep.h>
 
#include <avr/sleep.h>
 
#include <avr/power.h>
 
#include <avr/power.h>
 
+
 
int pin2 = 2;
 
int pin2 = 2;
  
 +
long previousMillis = 0;        // will store last time LED was updated
 +
long interval = 3000;          // interval at which to blink (milliseconds)
 
/***************************************************
 
/***************************************************
 
  *  Name:        pin2Interrupt
 
  *  Name:        pin2Interrupt
第88行: 第91行:
 
  *
 
  *
 
  ***************************************************/
 
  ***************************************************/
void pin2Interrupt(void)
+
void pin2Interrupt(void) {
{
 
 
     /* This will bring us back from sleep. */
 
     /* This will bring us back from sleep. */
   
+
 
     /* We detach the interrupt to stop it from  
 
     /* We detach the interrupt to stop it from  
 
     * continuously firing while the interrupt pin
 
     * continuously firing while the interrupt pin
第98行: 第100行:
 
     detachInterrupt(0);
 
     detachInterrupt(0);
 
}
 
}
 
+
 
/***************************************************
 
/***************************************************
 
  *  Name:        enterSleep
 
  *  Name:        enterSleep
第109行: 第111行:
 
  *
 
  *
 
  ***************************************************/
 
  ***************************************************/
void enterSleep(void)
+
void enterSleep(void) {
{
 
 
     /* Setup pin2 as an interrupt and attach handler. */
 
     /* Setup pin2 as an interrupt and attach handler. */
 
     attachInterrupt(0, pin2Interrupt, LOW);
 
     attachInterrupt(0, pin2Interrupt, LOW);
第121行: 第122行:
 
     sleep_disable();  
 
     sleep_disable();  
 
}
 
}
 
+
 
/***************************************************
 
/***************************************************
 
  *  Name:        setup
 
  *  Name:        setup
第132行: 第133行:
 
  *
 
  *
 
  ***************************************************/
 
  ***************************************************/
void setup()
+
void setup() {
{
 
 
     Serial.begin(9600);
 
     Serial.begin(9600);
 
     /* Setup the pin direction. */
 
     /* Setup the pin direction. */
第139行: 第139行:
 
     Serial.println("Initialisation complete.");
 
     Serial.println("Initialisation complete.");
 
}
 
}
 
+
 
/***************************************************
 
/***************************************************
 
  *  Name:        loop
 
  *  Name:        loop
第150行: 第150行:
 
  *
 
  *
 
  ***************************************************/
 
  ***************************************************/
int seconds=0;
+
 
void loop()
+
void loop() {
{
 
 
     delay(1000);
 
     delay(1000);
     seconds++;
+
     unsigned long currentMillis = millis();
 
     Serial.print("Awake for ");
 
     Serial.print("Awake for ");
     Serial.print(seconds, DEC);
+
     Serial.print(currentMillis - previousMillis, DEC);
 
     Serial.println(" second");
 
     Serial.println(" second");
     if(seconds == 3)
+
     if(currentMillis - previousMillis > interval) {
    {
+
      previousMillis = currentMillis;
 
         Serial.println("Entering sleep");
 
         Serial.println("Entering sleep");
        delay(200);
 
        seconds = 0;
 
 
         enterSleep();
 
         enterSleep();
 
     }
 
     }

2014年11月4日 (二) 06:38的版本

目的

本教程将专门介绍一下睡眠模式,如何在Microduino中运用。

设备

  • 其他硬件设备
    • USB数据连接线 一根
    • 面包板跳线 一盒

睡眠模式

Microduino像电脑和手机一样,也具备睡眠∕休眠∕待机功能。在睡眠状态下,系统几乎完全停止运作,只保留基本的侦测功能,因此只消耗少许电力。以电脑为例,在睡眠状态下,可被键盘按键或者网络和外部设备唤醒。

微处理器省电技术

下面的程序是一个最简单的体现睡眠模式的例子,开机马上启动睡眠模式:

void setup ()
{
  // 设定采用“Power-down”睡眠模式
  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  // 启用睡眠模式
  sleep_enable();
  // 进入睡眠模式
  sleep_cpu ();
}
void loop () { }

上边的代码有一句是设定睡眠模式:set_sleep_mode (SLEEP_MODE_PWR_DOWN);

“SLEEP_MODE_PWR_DOWN”为最省电的模式

ATMega328微控器具有六种睡眠模式,底下是依照「省电情况」排列的睡眠模式名次,排列越靠后越省电。「消耗电流」列指的是ATmega328处理器本身,而非整个控制板。

睡眠模式 Energy指令 中文直译 消耗电流
Idle Idle() 闲置 15mA
ADC Noise Reduction SleepADC() 类比数位转换器 6.5mA
Power-save PowerSave() 省电 1.62mA
Standby Standby() 待机 1.62mA
Extended Standby 延长待机 0.84mA
Power-down PowerDown() 断电 0.36mA


微控器内部除了中央处理器(CPU), 还有内存、类比数位转换器、序列通讯…等模块。越省电的模式,仍在运作中的模块就越少。


好了,在下面的程序中会体现一个完整的有休眠和唤醒功能的例子。


程序

#include <avr/sleep.h>
#include <avr/power.h>
 
int pin2 = 2;

long previousMillis = 0;        // will store last time LED was updated
long interval = 3000;           // interval at which to blink (milliseconds)
/***************************************************
 *  Name:        pin2Interrupt
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Service routine for pin2 interrupt  
 *
 ***************************************************/
void pin2Interrupt(void) {
    /* This will bring us back from sleep. */
 
    /* We detach the interrupt to stop it from 
     * continuously firing while the interrupt pin
     * is low.
     */
    detachInterrupt(0);
}
 
/***************************************************
 *  Name:        enterSleep
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Enters the arduino into sleep mode.
 *
 ***************************************************/
void enterSleep(void) {
    /* Setup pin2 as an interrupt and attach handler. */
    attachInterrupt(0, pin2Interrupt, LOW);
    delay(100);
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    sleep_mode();
    /* The program will continue from here. */
    /* First thing to do is disable sleep. */
    sleep_disable(); 
}
 
/***************************************************
 *  Name:        setup
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Setup for the Arduino.           
 *
 ***************************************************/
void setup() {
    Serial.begin(9600);
    /* Setup the pin direction. */
    pinMode(pin2, INPUT);
    Serial.println("Initialisation complete.");
}
 
/***************************************************
 *  Name:        loop
 *
 *  Returns:     Nothing.
 *
 *  Parameters:  None.
 *
 *  Description: Main application loop.
 *
 ***************************************************/

void loop() {
    delay(1000);
    unsigned long currentMillis = millis();
    Serial.print("Awake for ");
    Serial.print(currentMillis - previousMillis, DEC);
    Serial.println(" second");
    if(currentMillis - previousMillis > interval) {
      previousMillis = currentMillis; 
        Serial.println("Entering sleep");
        enterSleep();
    }
}

程序说明:

首先定义中断侦听引脚:int pin2 = 2; 中断引脚为数字端口D2

void pin2Interrupt(void):此函数为唤醒函数,从睡眠到醒来要做的事情可以在这里做

void enterSleep(void):此函数为睡眠函数,attachInterrupt(0, pin2Interrupt, LOW); 用来设定中断引脚监听,另外,进入睡眠模式时要做的事情在这里做

在setup()函数中做一些初始化工作

在loop()函数中定义每次循环的时间,如果时间达到3秒就进入休眠状态


结果

程序运行3秒后进入休眠模式,如果D2引脚为低电平时唤醒。


视频