Add switch press logic

Add GPIO PortF Handler
Add Button Press measurement
This commit is contained in:
Sanyog Nevase Nevase 2024-09-22 20:56:04 +05:30
parent 3577fe4128
commit c394652d00
1 changed files with 49 additions and 2 deletions

View File

@ -1,11 +1,13 @@
#include <stdint.h>
#include <stdbool.h>
#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
}
}
// 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
}
}