try to use timer interrupt
This commit is contained in:
		
							
								
								
									
										64
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								main.c
									
									
									
									
									
								
							| @ -1,42 +1,56 @@ | ||||
| /** | ||||
|  * @file main.c | ||||
|  * @brief Bike rear light implementation for ATTINY202. | ||||
|  * | ||||
|  * @brief Bike rear light implementation for ATTINY202 with low-power standby. | ||||
|  */ | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include <stdbool.h> | ||||
| #include <avr/io.h> | ||||
| #include <util/delay.h> | ||||
| #include <avr/sleep.h> | ||||
| #include <avr/interrupt.h> | ||||
|  | ||||
| #define BUTTON_PIN_MASK 0x01 // PA0 TODO: using RESET/UPDI pin | ||||
| #define PA1_SET_MASK 0x02    ///< LED 1–2 | ||||
| #define PA2_SET_MASK 0x04    ///< LED 3–6 | ||||
| #define PA3_SET_MASK 0x08    ///< LED 7–8 | ||||
| #define PA6_SET_MASK 0x40    ///< Green LED pin | ||||
| #define PA7_SET_MASK 0x80    ///< Red LED | ||||
| #define BUTTON_PIN_MASK 0x01 // PA0 | ||||
| #define PA1_SET_MASK 0x02    // LED 1–2 | ||||
| #define PA2_SET_MASK 0x04    // LED 3–6 | ||||
| #define PA3_SET_MASK 0x08    // LED 7–8 | ||||
| #define PA6_SET_MASK 0x40    // Green LED | ||||
| #define PA7_SET_MASK 0x80    // Red LED | ||||
|  | ||||
| #define MAIN_LOOP_SLEEP 10U                 // Main loop delay in ms | ||||
| #define MAIN_LOOP_SLEEP 50U                 // Loop period in ms | ||||
| #define BUTTON_LONG_PRESS_DURATION_MS 1000U // Long press threshold | ||||
| #define BUTTON_IGNORE_DURATION_MS 1000U     // Time button ignored after long press | ||||
| #define BUTTON_IGNORE_DURATION_MS 1000U     // Ignore after long press | ||||
|  | ||||
| /** Convert milliseconds to system ticks */ | ||||
| #define MS_TO_TICKS(ms) ((ms) / MAIN_LOOP_SLEEP) | ||||
|  | ||||
| /** Global flags */ | ||||
| volatile bool bLedEnabled = true; | ||||
| volatile bool bLedEnabled = false; | ||||
| volatile bool bBtnPressed = false; | ||||
|  | ||||
| // Forward declarations | ||||
| void blinkLed(bool resetCounters); | ||||
| static inline void leds_off(void); | ||||
| static inline void leds_on(void); | ||||
| static void battery_level_indicator(void); | ||||
| static void handleSwitch(void); | ||||
| void blinkLed(bool resetCounters); | ||||
|  | ||||
| /** | ||||
|  * @brief Main entry point | ||||
|  */ | ||||
| /* --- Timer init: Use RTC PIT for periodic wake-up --- */ | ||||
| void init_timer(void) | ||||
| { | ||||
|     RTC.CLKSEL = RTC_CLKSEL_INT1K_gc; // 1 kHz ULP clock for RTC | ||||
|     while (RTC.STATUS > 0) | ||||
|     { | ||||
|     } // Wait for sync | ||||
|  | ||||
|     RTC.PITINTCTRL = RTC_PI_bm;        // Enable PIT interrupt | ||||
|     RTC.PITCTRLA = RTC_PERIOD_CYC64_gc // ≈64 ms wake-up (~50 ms) | ||||
|                    | RTC_PITEN_bm;     // Enable PIT | ||||
| } | ||||
|  | ||||
| ISR(RTC_PIT_vect) | ||||
| { | ||||
|     RTC.PITINTFLAGS = RTC_PI_bm; // Clear interrupt flag | ||||
| } | ||||
|  | ||||
| /* --- MAIN --- */ | ||||
| int main(void) | ||||
| { | ||||
|     // Configure LED pins as outputs | ||||
| @ -52,7 +66,13 @@ int main(void) | ||||
|  | ||||
|     bool bLedEnabledOld = bLedEnabled; | ||||
|  | ||||
|     while (true) | ||||
|     cli(); | ||||
|     init_timer(); | ||||
|     sei(); | ||||
|  | ||||
|     set_sleep_mode(SLEEP_MODE_STANDBY); | ||||
|  | ||||
|     while (1) | ||||
|     { | ||||
|         handleSwitch(); // Check switch state | ||||
|  | ||||
| @ -88,7 +108,9 @@ int main(void) | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         _delay_ms(MAIN_LOOP_SLEEP); | ||||
|         sleep_enable(); | ||||
|         sleep_cpu(); // Sleep until PIT wakes | ||||
|         sleep_disable(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -279,7 +301,7 @@ void blinkLed(bool resetCounters) | ||||
|         if (counter >= T250) | ||||
|         { | ||||
|             counter = 0; | ||||
|             state = 0; // restart normal sequence | ||||
|             state = 0; | ||||
|         } | ||||
|         break; | ||||
|     } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user