• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Mega 2560 usypianie układu
#1
Chciałbym przy pomocy biblioteki:
Kod:
#include <avr/sleep.h>
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 Sad
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.

}
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości