From c394652d008662c9f0a4bc579186cdc617e3ba4f Mon Sep 17 00:00:00 2001 From: Sanyog Ramesh Nevase Date: Sun, 22 Sep 2024 20:56:04 +0530 Subject: [PATCH] Add switch press logic Add GPIO PortF Handler Add Button Press measurement --- Group21_Lab06_Task2/main.c | 51 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/Group21_Lab06_Task2/main.c b/Group21_Lab06_Task2/main.c index 0d8d1b0..11d47b0 100644 --- a/Group21_Lab06_Task2/main.c +++ b/Group21_Lab06_Task2/main.c @@ -1,11 +1,13 @@ #include #include #include "tm4c123gh6pm.h" + + // Global Variables volatile int duty = 50; // Initial duty cycle volatile bool buttonPressed = false; // Track button state volatile uint32_t pressDuration = 0; // Track how long the button is pressed -const long int PWM_PERIOD = 1000000; // Period for 100kHz in microseconds (10ms) +const long int PWM_PERIOD = 1000; // Period for 100kHz in microseconds (10ms) #define STCTRL *((volatile uint32_t *) 0xE000E010) // Control and status #define STRELOAD *((volatile uint32_t *) 0xE000E014) // Reload value #define STCURRENT *((volatile uint32_t *) 0xE000E018) // Current value @@ -61,4 +63,49 @@ void SystickHandler(void) { ontime = 0; // Reset counter after one period } -} \ No newline at end of file +// Handle button press duration measurement + if (buttonPressed) { + pressDuration++; + } + +} + +void GPIOF_interruptHandler(void) { + // Check if PF0 (SW1) is pressed (interrupt) + //long int i = 0; + //for (i=0; i < 100000 ; i++){ } + if (!(GPIO_PORTF_DATA_R & 0x01)) + { + if (!buttonPressed) { // If button was not already pressed + buttonPressed = true; + pressDuration = 0; // Reset press duration + } + } else { // Button released + if (buttonPressed) { // Only if button was previously pressed + buttonPressed = false; + + // Determine press duration + if (pressDuration > 100) { // Long press (>1 sec if using 10 ms ticks) + duty -= 5; // Decrease duty cycle + if (duty < 0) + {duty = 0; + // Ensure duty cycle doesn't gor below 0% + + } + } else if (pressDuration < 100){ // Short press + duty += 5; // decrease duty cycle + if (duty > 100) + {duty = 100; + // Ensure duty cycle doesn't exceed 100% + } + } + } + + // Clear the interrupt flag for PF0 (Switch 1) + GPIO_PORTF_ICR_R = 0x01; // Clear interrupt flag + } +} + + + +