0
\$\begingroup\$

I'm trying to write a long term timer function on an attiny85 and currently I'm using a counter. I'm experimenting using sleep mode as detailed here. I'm wondering if the chip "retains" information in sleep mode. For instance if I increment a variable and then go into sleep mode, will that variable be changed when the chip wakes again?

\$\endgroup\$
1
  • \$\begingroup\$ Depends if you store data in non-volatile or volatile memory. SRAM, DRAM, and RAM memory are volatile whereas ROM, EEPROM are non-volatile memory. \$\endgroup\$
    – 12Lappie
    Commented Jan 3, 2017 at 15:24

2 Answers 2

3
\$\begingroup\$

Yes, RAM is preserved during sleep mode on AVR8s (including the ATTINYs). From the datasheet...

enter image description here

Interestingly, it is even persevered across resets, and seems to even be persevered for as long as the chip is even slightly, slightly powered...

https://wp.josh.com/2014/03/03/the-mystery-of-the-zombie-ram/

\$\endgroup\$
5
  • \$\begingroup\$ Interestingly, it is even persevered across resets, and seems to even be persevered for as long as the chip is even slightly, slightly powered... I would consider that normal and expected. It is very rare for there to be a guarantee that RAM will be 0 on startup. It normally is 0 but counting on that is a huge potential source of bugs. I've seen a system that worked perfectly unless you switched it on below -10C at which point the memory wasn't all 0 on power up and it crashed. A temperature related software bug, unusual but it can happen. \$\endgroup\$
    – Andrew
    Commented Jan 3, 2017 at 16:45
  • \$\begingroup\$ It's extremely bad code that does not initialize SRam and registers (stack pointers etc) after a Reset, so there should never be the type of issue that 'Zombie...' described. \$\endgroup\$ Commented Jan 3, 2017 at 17:27
  • \$\begingroup\$ @JackCreasey I'd say it depends on the application. When you are coding for a chip with 0.5k of program memory running for a decade on a CR2032, then I don't think is it "extremely bad code" to avoid wasting bytes and cycles and Jules in cases when it makes sense! Why initialize a stack pointer for a program that never makes call or enables interrupts? Why statically initialize a variable that is guaranteed to be written to before it is read the first time? \$\endgroup\$
    – bigjosh
    Commented Jan 3, 2017 at 18:46
  • \$\begingroup\$ @bigjosh. Point taken, but I can't think of a situation where you don't have stack pointers unless you are doing very specialized assembly programming. \$\endgroup\$ Commented Jan 3, 2017 at 19:23
  • \$\begingroup\$ Lots of applications on the very tiny ATTINYs where this makes sense. C is no problem if you know what you are getting into- but you usually do want to replace the default C start-up code though. \$\endgroup\$
    – bigjosh
    Commented Jan 3, 2017 at 19:35
0
\$\begingroup\$

AVRs do preserve its state even during reset. This is the exact property I took advantage of in my first LED chaser project many years ago with an Attiny85. The only button was the reset button. The internal state was preserved, so I only needed to increment a counter if it wasn't initialized yet (after a power on) and the current LED animation was selected according to this counter.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.