Go Down

Topic: Sleep mode demo code (Read 13974 times) previous topic - next topic

JChristensen

On the off chance that the internet needs more sleep examples, and because I wanted to play with GitHub Gists:
https://gist.github.com/JChristensen/5616922

Coding Badly


aarondc


On the off chance that the internet needs more sleep examples, and because I wanted to play with GitHub Gists:
https://gist.github.com/JChristensen/5616922



Thanks - that looks far more complete (and complicated) than the one I have already seen.

I do not understand what some of the BOD code is doing, but based on your comments, you turn BOD off for sleeping - but I didn't see any code to turn it back on again. Does that happen by default?

JChristensen


Thanks - that looks far more complete (and complicated) than the one I have already seen.


You're welcome, it's not all that complicated ;)  Anyway it's what works for me. Actually I was looking at it and I may be able to remove a few lines from the ATtinyX5 example. But I'll want to test it first.

Quote

I do not understand what some of the BOD code is doing, but based on your comments, you turn BOD off for sleeping - but I didn't see any code to turn it back on again. Does that happen by default?


Yes. Setting the BODS (BOD Sleep) bit only turns off the BOD once the MCU is in sleep mode, not before. When it wakes, it is automatically turned back on.

The BODSE (BOD Sleep Enable) bit allows the BODS bit to be set.

Quote
Writing to the BODS bit is controlled by a timed sequence and an enable bit, BODSE in MCUCR. To disable BOD in
relevant sleep modes, both BODS and BODSE must first be set to one. Then, to set the BODS bit, BODS must be
set to one and BODSE must be set to zero within four clock cycles.
The BODS bit is active three clock cycles after it is set. A sleep instruction must be executed while BODS is active
in order to turn off the BOD for the actual sleep mode. The BODS bit is automatically cleared after three clock
cycles.


I will admit that it can take a bit to wrap one's head around that part!

aarondc

That makes sense to me, thanks.

I am curious - how much time does it take from Interrupt detected to wake the Arduino up? Would a human notice any lag, if the switch they were pressing was generating the Interrupt and they expected an instantaneous response to the button press? Where instantaneous is human-reaction time based, not electronic.

JChristensen


That makes sense to me, thanks.

I am curious - how much time does it take from Interrupt detected to wake the Arduino up? Would a human notice any lag, if the switch they were pressing was generating the Interrupt and they expected an instantaneous response to the button press? Where instantaneous is human-reaction time based, not electronic.


It seems instantaneous to my well-worn reflexes (try the demo code, it blinks an LED when it wakes). It actually depends on the settings of the CKSEL and SUT bits in the fuse bytes (see the datasheet), but the worst case scenario with the longest start-up time (which is the way the fuse bytes are set for an Uno) is 16K clock cycles, but that's just a bit over a millisecond.

An interrupt takes a minimum of four clock cycles, and when waking from sleep mode, an extra four, plus the start-up time.

aarondc

If it's going to be a millisecond it's a no brainer - takes longer than that to press a button. Awesome.

Erni

Just tested your sleep_ATtinyX5.ino on a ATtiny13.

It worked wothout any changes !
I can't even measure the current when it is in sleep (cheap multimeter)

One thing that is strange:
I have to toggle pin PB1, which according to the datasheet is PCINT1 and not PB0 which is PCINT0

Thanks for sharing

JChristensen

#8
May 22, 2013, 02:14 pm Last Edit: May 22, 2013, 02:17 pm by Jack Christensen Reason: 1

Just tested your sleep_ATtinyX5.ino on a ATtiny13.

It worked wothout any changes !
I can't even measure the current when it is in sleep (cheap multimeter)

One thing that is strange:
I have to toggle pin PB1, which according to the datasheet is PCINT1 and not PB0 which is PCINT0

Thanks for sharing


Cool, thanks for the feedback. I should probably pick up a couple ATtiny13s just to play with. I see in the datasheet that INT0 is PB1 (DIP pin 6) on the '13 where it is PB2 (DIP pin 7) on the 'X5, so that makes sense. But it should wake as soon as the pin is driven low, toggling it (low then high) should not be necessary.

Are you using the Arduino-Tiny core with the '13?

Erni

#9
May 22, 2013, 06:20 pm Last Edit: May 22, 2013, 06:30 pm by Erni Reason: 1
Quote
But it should wake as soon as the pin is driven low, toggling it (low then high) should not be necessary.

That was  my poor english, I am actually connecting pin PB1 to ground via a pushbutton

Quote
I see in the datasheet that INT0 is PB1 (DIP pin 6)

Yes I see it now, I was looking at PCINT0  :smiley-red:

Quote
Are you using the Arduino-Tiny core with the '13?


No, I am using Smeezekitty's core. As far as I know  the Arduino-Tiny core does not support ATtiny13.
I suppose you mean this core: https://code.google.com/p/arduino-tiny/

Smeezekitty's core:
http://forum.arduino.cc/index.php?topic=89781.0

JChristensen


I am using Smeezekitty's core. As far as I know  the Arduino-Tiny core does not support ATtiny13.

Smeezekitty's core:
http://forum.arduino.cc/index.php?topic=89781.0


Right, I didn't think so. Will check out Smeezekitty's core, thanks.

Hope things are well in DK :)

akshaybs

Hi Jack, this is great stuff! How do you wake up the MCU when INT0 is on high?

Thanks,
Akshay

JChristensen


Hi Jack, this is great stuff! How do you wake up the MCU when INT0 is on high?


It depends on which MCU.

There are four settings for the interrupt sense control (ISC) bits, which are in the MCUCR register for ATtinyX4 and ATtinyX5, or in the EICRA register for ATmega328P. These are low level interrupt, pin change interrupt, falling and rising edge. For example, see section 13.2 in the ATmega328P datasheet.

For the ATmega328P, any of the four types of interrupt will wake the MCU. For the ATtinyX4 and ATtinyX5, only the low level or pin change interrupts will wake the MCU.


akshaybs

Thank you! I am using the ATTiny45. I used a pin change interrupt to wake it up.

Akshay

JChristensen


Thank you! I am using the ATTiny45. I used a pin change interrupt to wake it up.

Akshay


Good deal, you are welcome!

Go Up