Add Comments

This commit is contained in:
Sanyog Nevase Nevase 2024-09-22 22:05:41 +05:30
parent edacdd8b63
commit 36b55d2f7b
1 changed files with 43 additions and 45 deletions

View File

@ -5,7 +5,7 @@
// Global Variables // Global Variables
volatile int duty = 50; // Initial duty cycle volatile int duty = 50; // Initial duty cycle
volatile bool buttonPressed = false; // Track button state volatile bool buttonPressed = false; // Button state
volatile uint32_t pressDuration = 0; // Track how long the button is pressed volatile uint32_t pressDuration = 0; // Track how long the button is pressed
const long int PWM_PERIOD = 1000; // 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 STCTRL *((volatile uint32_t *) 0xE000E010) // Control and status
@ -15,9 +15,9 @@ const long int PWM_PERIOD = 1000; // Period for 100kHz in microseconds (10ms)
#define COUNT_FLAG (1 << 16) // Bit 16 of CSR automatically set to 1 #define COUNT_FLAG (1 << 16) // Bit 16 of CSR automatically set to 1
#define ENABLE (1 << 0) // Bit 0 of CSR to enable the timer #define ENABLE (1 << 0) // Bit 0 of CSR to enable the timer
#define CLKINT (1 << 2) // Bit 2 of CSR to specify CPU clock #define CLKINT (1 << 2) // Bit 2 of CSR to specify CPU clock
#define CLOCK_HZ 16000000 // Clock frequency of EK-TM4C123GXL #define CLOCK_HZ 16000000 // Clock frequency
#define INTEN (1 << 1) // Bit 1 of CSR to enable interrupt #define INTEN (1 << 1) // Bit 1 of CSR to enable interrupt
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // SysTick reload value in microseconds based on clock frequency #define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // value in microseconds
void GPIO_PORT_F_init(void) { void GPIO_PORT_F_init(void) {
SYSCTL_RCGC2_R |= 0x00000020; // Enable clock to GPIOF SYSCTL_RCGC2_R |= 0x00000020; // Enable clock to GPIOF
@ -31,7 +31,7 @@ void GPIO_PORT_F_init(void) {
GPIO_PORTF_IS_R = 0x00; // Make it edge-sensitive GPIO_PORTF_IS_R = 0x00; // Make it edge-sensitive
GPIO_PORTF_IBE_R = 0x00; // Trigger on one edge GPIO_PORTF_IBE_R = 0x00; // Trigger on one edge
GPIO_PORTF_IEV_R = 0x00; // Falling edge event GPIO_PORTF_IEV_R = 0x00; // Falling edge event
GPIO_PORTF_IM_R |= 0x11; // Unmask interrupts for PF0 and PF4 GPIO_PORTF_IM_R |= 0x11; // Unmask interrupts for switch 1 and Switch 2
} }
void systick_setting(void) { void systick_setting(void) {
@ -53,19 +53,20 @@ void SystickHandler(void)
ontime++; ontime++;
if (ontime < duty) if (ontime < duty)
{ {
GPIO_PORTF_DATA_R |= 0x02; // LED should be ON GPIO_PORTF_DATA_R |= 0x02; // LED ON
} }
else else
{ {
GPIO_PORTF_DATA_R &= ~0x02; // LED should be OFF GPIO_PORTF_DATA_R &= ~0x02; // LED OFF
} }
if (ontime >= 100) if (ontime >= 100)
{ {
ontime = 0; // Reset counter after one period ontime = 0; // Reset counter after one period
} }
// Handle button press duration measurement
if (buttonPressed) { if (buttonPressed) //Press Duration Measurement
{
pressDuration++; pressDuration++;
} }
@ -77,7 +78,8 @@ void GPIOF_interruptHandler(void) {
//for (i=0; i < 100000 ; i++){ } //for (i=0; i < 100000 ; i++){ }
if (!(GPIO_PORTF_DATA_R & 0x01)) if (!(GPIO_PORTF_DATA_R & 0x01))
{ {
if (!buttonPressed) { // If button was not already pressed if (!buttonPressed) // If button was not pressed
{
buttonPressed = true; buttonPressed = true;
pressDuration = 0; // Reset press duration pressDuration = 0; // Reset press duration
} }
@ -86,23 +88,19 @@ void GPIOF_interruptHandler(void) {
buttonPressed = false; buttonPressed = false;
// Determine press duration // Determine press duration
if (pressDuration > 100) { // Long press (>1 sec if using 10 ms ticks) if (pressDuration > 100)
{ // Long press (>1 sec if using 10 ms ticks)
duty -= 5; // Decrease duty cycle duty -= 5; // Decrease duty cycle
if (duty < 0) if (duty < 0)
{duty = 0; {duty = 0;
// Ensure duty cycle doesn't gor below 0%
} }
} else if (pressDuration < 100){ // Short press } else if (pressDuration < 100){ // Short press
duty += 5; // decrease duty cycle duty += 5; // decrease duty cycle
if (duty > 100) if (duty > 100)
{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 GPIO_PORTF_ICR_R = 0x01; // Clear interrupt flag
} }
} }