Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

millis() prevents entering to sleep #155

Open
lazarevd opened this issue Jan 25, 2024 · 2 comments
Open

millis() prevents entering to sleep #155

lazarevd opened this issue Jan 25, 2024 · 2 comments

Comments

@lazarevd
Copy link

lazarevd commented Jan 25, 2024

If millis() function appears anywhere in loop(), it prevents enetering to sleep mode. The position of millis() doesn`t matter, it could be after sleep_cpu().
micros() works normally.

#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#define LED PB0

void setup() {
  pinMode(LED, OUTPUT);
  delay(500);
}

void loop() {
  analogWrite(LED, 0);
  delay(200);
  analogWrite(LED, 250);
  delay(200);
  analogWrite(LED, 0);
  system_sleep();
  millis();//commenting this line let attiny13 enter to sleep, if i uncomment it, led blinks continously
}

void system_sleep() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sei();
  sleep_cpu();
}
@radiantwave
Copy link

radiantwave commented Aug 29, 2024

millis() in the MicroCore are watchdog timer based.
Do you still need help with this?

WDTCR &= ~(1 << WDTIE); because of the implementation of the millis() function it is necessary to disable the watchdog interrupt before entering sleep mode
WDTCR |= (1 << WDTIE); after waking the cpu from sleep mode it is necessary to enable the watchdog interrupt again, otherwise the millis() function does not work

@lazarevd
Copy link
Author

Thank you. After all I used micros() instead of millis(), but i’ll try your solution.
Is it in documentation? May be it’s worth to add it if it’s not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants