26-12-2017, 15:44
Chciałbym przy pomocy biblioteki:
usypiać moją Mega, i po podaniu na pin2 stanu HIGH, ma się wybudzić i robić dalej w pętli wszystko do czasu aż nie zniknie HIGH na pinie2.
Próbowałem na wszelkie sposoby, ale za nic mi to nie wychodzi
tutaj przykład, niestety on też nie działa....
Kod:
Kod:
#include <avr/sleep.h>
Próbowałem na wszelkie sposoby, ale za nic mi to nie wychodzi
tutaj przykład, niestety on też nie działa....
Kod:
Kod:
/******************************************
PURPOSE: Tutorial on using the Power Down sleep mode of the ATmega328p
Created by Rudy Schlaf for www.makecourse.com
Based on: http://playground.arduino.cc/Learning/ArduinoSleepCode
DATE: 4/2014
*******************************************/
#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
void setup()
{
Serial.begin(9600); //start serial communication at 9600 baud
pinMode(13,OUTPUT); // set pin 13 as an output so we can use LED to monitor
digitalWrite(13,LOW); // turn pin 13 LED off
pinMode(2, INPUT); //Set interrupt pin 2 as input
}
void loop()
{
if (digitalRead(2) == LOW)
{
sleepSetup(); //now we see ~27 mA until pin 2 is connected to GND
}
if (digitalRead(2) == HIGH)
{
digitalWrite(13,HIGH);
}
}
void sleepSetup()
{
sleep_enable();
attachInterrupt(0, pinInterrupt, HIGH);//Set pin 2 as interrupt and attach Interrupt Service Routine (ISR)
set_sleep_mode(SLEEP_MODE_PWR_DOWN);//define power down sleep mode
digitalWrite(13,LOW); // turn LED off to indicate sleep
sleep_cpu();//Set sleep enable (SE) bit, this puts the ATmega to sleep
Serial.println("just woke up!");//When it wakes up due to the interrupt the program continues with the instruction following sleep_cpu()
digitalWrite(13,HIGH); // turn LED on to indicate wake up
}
void pinInterrupt()//ISR
{
sleep_disable();//this is important. It is possible that the interrupt is called between executing "attachInterrupt(...)" and sleep_CPU() in the main loop
//if that happens without the sleep_disable() in the ISR, the ISR would be called, the interrupt detached and the device put to sleep.
//since the interrupt would be disabled at that point, there would be no way to wake the device up anymore.
//by putting sleep_disable() in the ISR, sleep_cpu() would not be effective during that loop, i.e. the main loop would run one more time
//and then properly attach the interrupt before hitting the sleep_cpu() a second time. At that point the device would go to sleep, but
//the interrupt would now be activated, i.e. wake-up can be induced.
detachInterrupt(0);//disable the interrupt. This effectively debounces the interrupt mechanism to prevent multiple interrupt calls.
}