From f30fb96f972c6f5909109d6482bdeb8a2b2d4668 Mon Sep 17 00:00:00 2001 From: Kurt LaVacque Date: Thu, 18 Jan 2024 23:31:15 -0600 Subject: [PATCH 1/5] Added button toggle for sleep --- main.cpp | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 190776a7..dc2cb78f 100644 --- a/main.cpp +++ b/main.cpp @@ -4,13 +4,36 @@ #include #include // Include this for sei() +volatile bool sleepEnabled = false; // Flag to control sleep mode -int main() -{ +// Interrupt Service Routine for Pin Change Interrupt +ISR(PCINT0_vect) { + // Toggle sleep mode on button press + if (bit_is_clear(PINB, PB2)) { // Check if button is pressed (low state) + sleepEnabled = !sleepEnabled; // Toggle the flag + } +} + +int main() { + // ... [rest of your setup code] + + // Button setup + DDRB &= ~(1 << PB2); // Set PB2 as input + PORTB |= (1 << PB2); // Enable pull-up resistor on PB2 + + // Configure PB0 as output for the LED + DDRB |= (1 << PB0); // Set PB0 as output + + // Enable Pin Change Interrupt for PB2 + GIMSK |= (1 << PCIE); // Enable Pin Change Interrupts + PCMSK |= (1 << PCINT2); // Enable interrupt for PB2 + + sei(); // Enable global interrupts - while (1) - { - // Disable the Watchdog Timer + while (1) { + // Check the sleep flag + if (sleepEnabled) { + // Disable the Watchdog Timer MCUSR &= ~(1 << WDRF); wdt_disable(); // Configure all I/O pins as input with pull-up enabled @@ -42,6 +65,10 @@ int main() // Disable sleep mode sleep_disable(); + } else { + // Set PB0 (LED) to high when not sleeping + PORTB |= (1 << PB0); + } } return 0; } From 7ea0e6f677c8bfea5fe4e4c44590061e4d527133 Mon Sep 17 00:00:00 2001 From: Kurt LaVacque Date: Thu, 18 Jan 2024 23:43:30 -0600 Subject: [PATCH 2/5] Update button setup and interrupt configuration --- main.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index dc2cb78f..483f2415 100644 --- a/main.cpp +++ b/main.cpp @@ -9,31 +9,31 @@ volatile bool sleepEnabled = false; // Flag to control sleep mode // Interrupt Service Routine for Pin Change Interrupt ISR(PCINT0_vect) { // Toggle sleep mode on button press - if (bit_is_clear(PINB, PB2)) { // Check if button is pressed (low state) + if ((PINB & (1 << 3)) != 0) { // Check if button is pressed (low state) sleepEnabled = !sleepEnabled; // Toggle the flag } } int main() { - // ... [rest of your setup code] - // Button setup - DDRB &= ~(1 << PB2); // Set PB2 as input - PORTB |= (1 << PB2); // Enable pull-up resistor on PB2 + DDRB &= ~(1 << PB3); // Set PB3 as input + PORTB |= (1 << PB3); // Enable pull-up resistor on PB3 // Configure PB0 as output for the LED DDRB |= (1 << PB0); // Set PB0 as output - // Enable Pin Change Interrupt for PB2 + // Enable Pin Change Interrupt for PB3 GIMSK |= (1 << PCIE); // Enable Pin Change Interrupts - PCMSK |= (1 << PCINT2); // Enable interrupt for PB2 + PCMSK |= (1 << PCINT3); // Enable interrupt for PB3 sei(); // Enable global interrupts while (1) { // Check the sleep flag if (sleepEnabled) { - // Disable the Watchdog Timer + // Turn off LED + PORTB &= ~(1 << PB0); + // Disable the Watchdog Timer MCUSR &= ~(1 << WDRF); wdt_disable(); // Configure all I/O pins as input with pull-up enabled @@ -52,6 +52,8 @@ int main() { // Activate PRR (Power Reduction Register) PRR |= (1 << PRTIM1) | (1 << PRTIM0) | (1 << PRUSI) | (1 << PRADC); + + // Disable the BOD sleep_bod_disable(); From cad25b8b0277dbe789fe68289583974955425193 Mon Sep 17 00:00:00 2001 From: Kurt LaVacque Date: Thu, 18 Jan 2024 23:51:35 -0600 Subject: [PATCH 3/5] Fix Pin Change Interrupt and LED configuration --- main.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 483f2415..8c22d3db 100644 --- a/main.cpp +++ b/main.cpp @@ -9,9 +9,9 @@ volatile bool sleepEnabled = false; // Flag to control sleep mode // Interrupt Service Routine for Pin Change Interrupt ISR(PCINT0_vect) { // Toggle sleep mode on button press - if ((PINB & (1 << 3)) != 0) { // Check if button is pressed (low state) - sleepEnabled = !sleepEnabled; // Toggle the flag - } + // if ((PINB & (1 << 3)) != 0) { // Check if button is pressed (low state) + sleepEnabled = !sleepEnabled; // Toggle the flag + // } } int main() { @@ -52,8 +52,6 @@ int main() { // Activate PRR (Power Reduction Register) PRR |= (1 << PRTIM1) | (1 << PRTIM0) | (1 << PRUSI) | (1 << PRADC); - - // Disable the BOD sleep_bod_disable(); @@ -68,6 +66,12 @@ int main() { // Disable sleep mode sleep_disable(); } else { + // Re-enable Pin Change Interrupt for PB3 + GIMSK |= (1 << PCIE); // Enable Pin Change Interrupts + PCMSK |= (1 << PCINT3); // Enable interrupt for PB3 + + // Reconfigure PB0 as output for the LED + DDRB |= (1 << PB0); // Set PB0 as output // Set PB0 (LED) to high when not sleeping PORTB |= (1 << PB0); } From f30a3be62b934de729b88de2684243f2fcc04cc5 Mon Sep 17 00:00:00 2001 From: Kurt LaVacque Date: Thu, 18 Jan 2024 23:55:54 -0600 Subject: [PATCH 4/5] Trying this --- main.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index 8c22d3db..2ee745dc 100644 --- a/main.cpp +++ b/main.cpp @@ -8,10 +8,7 @@ volatile bool sleepEnabled = false; // Flag to control sleep mode // Interrupt Service Routine for Pin Change Interrupt ISR(PCINT0_vect) { - // Toggle sleep mode on button press - // if ((PINB & (1 << 3)) != 0) { // Check if button is pressed (low state) - sleepEnabled = !sleepEnabled; // Toggle the flag - // } + } int main() { @@ -29,8 +26,6 @@ int main() { sei(); // Enable global interrupts while (1) { - // Check the sleep flag - if (sleepEnabled) { // Turn off LED PORTB &= ~(1 << PB0); // Disable the Watchdog Timer @@ -65,16 +60,14 @@ int main() { // Disable sleep mode sleep_disable(); - } else { // Re-enable Pin Change Interrupt for PB3 GIMSK |= (1 << PCIE); // Enable Pin Change Interrupts PCMSK |= (1 << PCINT3); // Enable interrupt for PB3 // Reconfigure PB0 as output for the LED DDRB |= (1 << PB0); // Set PB0 as output - // Set PB0 (LED) to high when not sleeping - PORTB |= (1 << PB0); - } + // Set PB0 (LED) to high when not sleeping + PORTB |= (1 << PB0); } return 0; } From 343203987d9e3d64be44553238121658ddbb2681 Mon Sep 17 00:00:00 2001 From: Kurt LaVacque Date: Thu, 18 Jan 2024 23:58:39 -0600 Subject: [PATCH 5/5] Starts in sleep and button wakes it --- main.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 2ee745dc..4dd4827a 100644 --- a/main.cpp +++ b/main.cpp @@ -25,8 +25,7 @@ int main() { sei(); // Enable global interrupts - while (1) { - // Turn off LED + // Turn off LED PORTB &= ~(1 << PB0); // Disable the Watchdog Timer MCUSR &= ~(1 << WDRF); @@ -60,6 +59,9 @@ int main() { // Disable sleep mode sleep_disable(); + + while (1) { + // Re-enable Pin Change Interrupt for PB3 GIMSK |= (1 << PCIE); // Enable Pin Change Interrupts PCMSK |= (1 << PCINT3); // Enable interrupt for PB3