Skip to content

Commit

Permalink
v1.81 - fixed timer 2 setting incorrectly restored after sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketscream committed Jan 21, 2020
1 parent 5445e1c commit 2f9b2e5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 48 deletions.
79 changes: 33 additions & 46 deletions LowPower.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
* LowPower Library
* Version: 1.80
* Date: 04-10-2018
* Version: 1.81
* Date: 21-01-2020
* Author: Lim Phang Moh
* Company: Rocket Scream Electronics
* Website: www.rocketscream.com
Expand All @@ -13,6 +13,8 @@
*
* Revision Description
* ======== ===========
* 1.81 Fixed timer 2 settings incorrectly restored after sleep.
* Contributed by rwared11.
* 1.80 Added support for ATmega88 and ATmega168P. PowerExtStandby()
* modified because not supported on Atmega88 / Atmega168
* Contributed by mrguen.
Expand Down Expand Up @@ -169,9 +171,8 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2,

if (timer2 == TIMER2_OFF)
{
if (TCCR2B & CS22) clockSource |= (1 << CS22);
if (TCCR2B & CS21) clockSource |= (1 << CS21);
if (TCCR2B & CS20) clockSource |= (1 << CS20);
// Store current setting
clockSource = TCCR2B;

// Remove the clock source to shutdown Timer2
TCCR2B &= ~(1 << CS22);
Expand Down Expand Up @@ -209,10 +210,9 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2,

if (timer2 == TIMER2_OFF)
{
if (clockSource & CS22) TCCR2B |= (1 << CS22);
if (clockSource & CS21) TCCR2B |= (1 << CS21);
if (clockSource & CS20) TCCR2B |= (1 << CS20);

// Restore previous setting
TCCR2B = clockSource;

power_timer2_enable();
}

Expand Down Expand Up @@ -394,9 +394,8 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2,

if (timer2 == TIMER2_OFF)
{
if (TCCR2B & CS22) clockSource |= (1 << CS22);
if (TCCR2B & CS21) clockSource |= (1 << CS21);
if (TCCR2B & CS20) clockSource |= (1 << CS20);
// Store current setting
clockSource = TCCR2B;

// Remove the clock source to shutdown Timer2
TCCR2B &= ~(1 << CS22);
Expand Down Expand Up @@ -435,9 +434,8 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2,

if (timer2 == TIMER2_OFF)
{
if (clockSource & CS22) TCCR2B |= (1 << CS22);
if (clockSource & CS21) TCCR2B |= (1 << CS21);
if (clockSource & CS20) TCCR2B |= (1 << CS20);
// Restore previous setting
TCCR2B = clockSource;

power_timer2_enable();
}
Expand Down Expand Up @@ -540,9 +538,8 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5,

if (timer2 == TIMER2_OFF)
{
if (TCCR2B & CS22) clockSource |= (1 << CS22);
if (TCCR2B & CS21) clockSource |= (1 << CS21);
if (TCCR2B & CS20) clockSource |= (1 << CS20);
// Store current setting
clockSource = TCCR2B;

// Remove the clock source to shutdown Timer2
TCCR2B &= ~(1 << CS22);
Expand Down Expand Up @@ -586,9 +583,8 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5,

if (timer2 == TIMER2_OFF)
{
if (clockSource & CS22) TCCR2B |= (1 << CS22);
if (clockSource & CS21) TCCR2B |= (1 << CS21);
if (clockSource & CS20) TCCR2B |= (1 << CS20);
// Restore previous setting
TCCR2B = clockSource;

power_timer2_enable();
}
Expand Down Expand Up @@ -688,9 +684,8 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5,

if (timer2 == TIMER2_OFF)
{
if (TCCR2B & CS22) clockSource |= (1 << CS22);
if (TCCR2B & CS21) clockSource |= (1 << CS21);
if (TCCR2B & CS20) clockSource |= (1 << CS20);
// Store current setting
clockSource = TCCR2B;

// Remove the clock source to shutdown Timer2
TCCR2B &= ~(1 << CS22);
Expand Down Expand Up @@ -732,9 +727,8 @@ void LowPowerClass::idle(period_t period, adc_t adc, timer5_t timer5,

if (timer2 == TIMER2_OFF)
{
if (clockSource & CS22) TCCR2B |= (1 << CS22);
if (clockSource & CS21) TCCR2B |= (1 << CS21);
if (clockSource & CS20) TCCR2B |= (1 << CS20);
// Restore previous setting
TCCR2B = clockSource;

power_timer2_enable();
}
Expand Down Expand Up @@ -793,9 +787,8 @@ void LowPowerClass::adcNoiseReduction(period_t period, adc_t adc,
#if !defined(__AVR_ATmega32U4__)
if (timer2 == TIMER2_OFF)
{
if (TCCR2B & CS22) clockSource |= (1 << CS22);
if (TCCR2B & CS21) clockSource |= (1 << CS21);
if (TCCR2B & CS20) clockSource |= (1 << CS20);
// Store current setting
clockSource = TCCR2B;

// Remove the clock source to shutdown Timer2
TCCR2B &= ~(1 << CS22);
Expand All @@ -819,10 +812,8 @@ void LowPowerClass::adcNoiseReduction(period_t period, adc_t adc,
#if !defined(__AVR_ATmega32U4__)
if (timer2 == TIMER2_OFF)
{
if (clockSource & CS22) TCCR2B |= (1 << CS22);
if (clockSource & CS21) TCCR2B |= (1 << CS21);
if (clockSource & CS20) TCCR2B |= (1 << CS20);

// Restore previous setting
TCCR2B = clockSource;
}
#endif
}
Expand Down Expand Up @@ -937,9 +928,8 @@ void LowPowerClass::powerSave(period_t period, adc_t adc, bod_t bod,
#if !defined(__AVR_ATmega32U4__)
if (timer2 == TIMER2_OFF)
{
if (TCCR2B & CS22) clockSource |= (1 << CS22);
if (TCCR2B & CS21) clockSource |= (1 << CS21);
if (TCCR2B & CS20) clockSource |= (1 << CS20);
// Store current setting
clockSource = TCCR2B;

// Remove the clock source to shutdown Timer2
TCCR2B &= ~(1 << CS22);
Expand Down Expand Up @@ -974,9 +964,8 @@ void LowPowerClass::powerSave(period_t period, adc_t adc, bod_t bod,
#if !defined(__AVR_ATmega32U4__)
if (timer2 == TIMER2_OFF)
{
if (clockSource & CS22) TCCR2B |= (1 << CS22);
if (clockSource & CS21) TCCR2B |= (1 << CS21);
if (clockSource & CS20) TCCR2B |= (1 << CS20);
// Restore previous setting
TCCR2B = clockSource;
}
#endif
}
Expand Down Expand Up @@ -1082,9 +1071,8 @@ void LowPowerClass::powerExtStandby(period_t period, adc_t adc, bod_t bod,
#if !defined(__AVR_ATmega32U4__)
if (timer2 == TIMER2_OFF)
{
if (TCCR2B & CS22) clockSource |= (1 << CS22);
if (TCCR2B & CS21) clockSource |= (1 << CS21);
if (TCCR2B & CS20) clockSource |= (1 << CS20);
// Store current setting
clockSource = TCCR2B;

// Remove the clock source to shutdown Timer2
TCCR2B &= ~(1 << CS22);
Expand Down Expand Up @@ -1123,9 +1111,8 @@ void LowPowerClass::powerExtStandby(period_t period, adc_t adc, bod_t bod,
#if !defined(__AVR_ATmega32U4__)
if (timer2 == TIMER2_OFF)
{
if (clockSource & CS22) TCCR2B |= (1 << CS22);
if (clockSource & CS21) TCCR2B |= (1 << CS21);
if (clockSource & CS20) TCCR2B |= (1 << CS20);
// Restore previous setting
TCCR2B = clockSource;
}
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
### Low-Power
Lightweight low power library for Arduino.

Version: 1.80
Version: 1.81

Date: 04-10-2018
Date: 21-01-2020

Devices Supported:
* ATMega88
Expand Down

0 comments on commit 2f9b2e5

Please sign in to comment.