sleep mode and wake-up with timer

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Hello,ı am using attiny84A.I want to do this:


main()

{

if(adc_value>10)

sleep.mode() //for 10 seconds,nothing needs to be done here.Just wait.

//After waiting 10 seconds, attiny must wake up

}

I don't want external clock source.



I did some research.I think,I can't use internal timer.And I should use watchdog interrupt.You can look to picture in below.


*how can I do that?

*Does the processor automatically reset after waking up?

*Which sleep mode makes sense for my request?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While sleeping do you need to reduce power consumption?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Idle sleep mode will do what you want, set up a timer for your 10 sec period, either overflow interrupt or compare match interrupt can be used for wakeup, enable timer just before entering sleep mode.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>Which sleep mode makes sense for my request?
It seems to me that the power down sleep is a good option if this mode doesn't hurt any of your requirements, or in other words you should have a "thumbs up" after looking at the row Power-down at table 7-1.

 

>how can I do that?
You can use the watchdog timer(WDT) to count the time and a WDT interrupt to wake up. It's more easy and efficient if you can establish a timeout of 8s instead of 10s, because there is a WDT timer prescaler of 8s. The last option requests a software counter to be used in conjunction with the WDT. Let's suppose that you can use 8s as a timeout value.

 

Steps:
0. The interrupt vector related to the WDT interrupt must be defined. It should be empty if don't need to handle anything more there.
1. The WDT should be configured and enabled to use a prescaler value of 8s with the Interrupt Mode;
2. The interrupts should be enabled;
3. The power down sleep mode should be enabled;
4. The MCU goes to sleep.
5. After wake up the WDT is disable.

 

Let's translate the above steps to some code:

 

#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

 

ISR(WDT_vect){} //step 0

 

//sleep_for 8s
wdt_enable(WDTO_8S); //step 1 Update [1]
sei(); //step 2
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //step 3
sleep_mode(); //step 4

wdt_disable(); //step 5
//next "statement to be executed"

 

>Does the processor automatically reset after waking up?
No if you're using the above approach, but it's imperative to catch the WDT interrupt, otherwise avr-libc runtime will process a reset.

 

[1] Update: I have remembered that avr/wdt.h doesn't have support to Interrupt Mode but it isn't hard to write the on/off to WDT. Let me know you need help if this.

github.com/ricardocosme

avrIO: Operation of I/O port pins and I/O registers.

Last Edited: Tue. Apr 6, 2021 - 03:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would suggest to use the newest ATtinyXXX e.g. ATtiny202 which I am using for sleep STANDBY mode and measuring the internal temperature by ADC triggered by RTC event each 60 seconds.
 

Have a look at ADC and Power Optimization with tinyAVR® 0- and 1-series, and megaAVR® 0-series

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
While sleeping do you need to reduce power consumption?

Yes,but with timer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Idle sleep mode will do what you want, set up a timer for your 10 sec period, either overflow interrupt or compare match interrupt can be used for wakeup, enable timer just before entering sleep mode.

 

Jim


So,can I use the software timer while in sleep mode?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rcosme wrote:

>Which sleep mode makes sense for my request?
It seems to me that the power down sleep is a good option if this mode doesn't hurt any of your requirements, or in other words you should have a "thumbs up" after looking at the row Power-down at table 7-1.

 

>how can I do that?
You can use the watchdog timer(WDT) to count the time and a WDT interrupt to wake up. It's more easy and efficient if you can establish a timeout of 8s instead of 10s, because there is a WDT timer prescaler of 8s. The last option requests a software counter to be used in conjunction with the WDT. Let's suppose that you can use 8s as a timeout value.

 

Steps:
0. The interrupt vector related to the WDT interrupt must be defined. It should be empty if don't need to handle anything more there.
1. The WDT should be configured and enabled to use a prescaler value of 8s with the Interrupt Mode;
2. The interrupts should be enabled;
3. The power down sleep mode should be enabled;
4. The MCU goes to sleep.
5. After wake up the WDT is disable.

 

Let's translate the above steps to some code:

 

#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

 

ISR(WDT_vect){} //step 0

 

//sleep_for 8s
wdt_enable(WDTO_8S); //step 1 Update [1]
sei(); //step 2
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //step 3
sleep_mode(); //step 4

wdt_disable(); //step 5
//next "statement to be executed"

 

>Does the processor automatically reset after waking up?
No if you're using the above approach, but it's imperative to catch the WDT interrupt, otherwise avr-libc runtime will process a reset.

 

[1] Update: I have remembered that avr/wdt.h doesn't have support to Interrupt Mode but it isn't hard to write the on/off to WDT. Let me know you need help if this.

is this code no longer valid?I may also need waiting times of 10 seconds and its multiples. like 30 sec and like 40 sec.maybe i shouldn't use wdt?
Can the processor wake up from sleep mode with the software timer?
Or any solution?
In summary, this is what I want:
In the main code block, whenever I want, the microcontroller should sleep for t seconds.When the t seconds is over, the microcontroller should wake up.(t can be 10 sec or 30sec,40sec)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hrn97ta wrote:

So,can I use the software timer while in sleep mode?

 

No, not if by software timer you mean something other than one of the on-chip peripherals. In all sleep modes the CPU is no longer executing any instructions. Depending on the slepp mode selected different peripherals continue to run; the 'deeper' the sleep, the less peripherals are left running.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kevil wrote:
 I would suggest to use the newest ATtinyXXX e.g. ATtiny202 which I am using for sleep STANDBY mode and measuring the internal temperature by ADC triggered by RTC event each 60 seconds. 

Datasheet:
The RTC peripheral includes a 15-bit programmable prescaler ...
... the maximum timeout period is more than 18 hours.

 

Silicon Errata:
Any write to the RTC.CTRLA register resets the 15-bit prescaler.
Work Around: None.

 

The truth is more important than the facts.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

poftamunk wrote:

Kevil wrote:
 I would suggest to use the newest ATtinyXXX e.g. ATtiny202 which I am using for sleep STANDBY mode and measuring the internal temperature by ADC triggered by RTC event each 60 seconds. 

Datasheet:
The RTC peripheral includes a 15-bit programmable prescaler ...
... the maximum timeout period is more than 18 hours.

 

Silicon Errata:
Any write to the RTC.CTRLA register resets the 15-bit prescaler.
Work Around: None.

 


I am using old attiny.There must be a way.
All I want is this:
main()
enter_sleep_mode_for 30 seconds()
wake_up()

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hrn97ta wrote:

poftamunk wrote:

Kevil wrote:
 I would suggest to use the newest ATtinyXXX e.g. ATtiny202 which I am using for sleep STANDBY mode and measuring the internal temperature by ADC triggered by RTC event each 60 seconds. 

Datasheet:
The RTC peripheral includes a 15-bit programmable prescaler ...
... the maximum timeout period is more than 18 hours.

 

Silicon Errata:
Any write to the RTC.CTRLA register resets the 15-bit prescaler.
Work Around: None.

 


I am using old attiny.There must be a way.
All I want is this:
main()
enter_sleep_mode_for 30 seconds()
//wake up

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know why I'm still doing this hobby

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

hrn97ta wrote:

I am using old attiny.There must be a way.
All I want is this:
main()
enter_sleep_mode_for 30 seconds()
//wake up

If your not concerned with a low power sleep mode, then depending on your cpu clock speed, then set up t0, t1 or t2 to give you your 30 second timeout period, and use idle sleep mode, one and done.

However if current demand needs to be as low as possible, then you can not use one of the timers, but must use the WDT, the longest it will wait however is 8 sec's, so you will need to set it to say 1 sec timeout and then use WD interrupt ISR to count your 30 timeouts, if less then 30, then go back to sleep! Does that help?

 

Jim

 

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The table you posted in #1 surely tells you all you need to know? If you are going to sleep then the key question is how will you wake up again. So the  "wakeup sources" at the end of the table is key. As you want periodic wakeup most don't  help but "watchdog interrupt" does.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>is this code no longer valid?
It's but we need to implement the red part.
Actually, this is the approach that I use in some projects. I have a C++ facility that offers a function that allows to do the thing using only one single line[example: sleep_for<10_s>()] plus the ISR.

 

Let's review the code implementing the WDT on/off:

 

#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <util/atomic.h> //for ATOMIC_BLOCK

 

ISR(WDT_vect){} //step 0

ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {

    //turn on WDT without timed sequence [1] [step 1]
    asm("wdr");
    //enable the Interrupt mode using a prescaler value equal to 8s
    WDTCSR = (1<<WDIE) | (1<<WDP0) | (1<<WDP3);

 

    sei(); //step 2
    set_sleep_mode(SLEEP_MODE_PWR_DOWN); //step 3
    sleep_mode(); //step 4

 

    //turn off WDT using timed sequence [step 5]
    asm("wdr");
    MCUSR &= ~(1<WDRF);
    WDTCSR |= (1<<WDCE) | (1<<WDE);
    WDTCSR = 0x00;
}

 

[1] It seems to me that we don't need a timed sequenc to turn on the WDT on ATtiny84.

 

I don't have an ATtiny84 to test the code, but it works on the chip more close that I have here: ATtiny85.

 

>I may also need waiting times of 10 seconds and its multiples. like 30 sec and like 40 sec.maybe i shouldn't use wdt?
Right, no problem, you only need a software counter to be incremented by the ISR plus a loop outside the code above that contemplates the steps 1 to 4 that is responsible to check if you reach the requested timeout. You need to select a prescaler value that you can count to 10s, like the prescaler values of 2s. So in this case your software counter counts from 1 to 5.

 

>Can the processor wake up from sleep mode with the software timer?
No if you're only using the software counter, we need to use it if something else, like we did above.

 

github.com/ricardocosme

avrIO: Operation of I/O port pins and I/O registers.

Last Edited: Tue. Apr 6, 2021 - 05:20 PM