Group21_Lab06/Group21_Lab06_Task2/main.c

116 lines
4.6 KiB
C
Raw Normal View History

#include <stdint.h>
#include <stdbool.h>
#include "tm4c123gh6pm.h"
// Global Variables
2024-09-22 22:05:41 +05:30
volatile int duty = 50; // Initial duty cycle
volatile bool buttonPressed = false; // Button state
volatile uint32_t pressDuration = 0; // Track how long the button is pressed
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
2024-09-22 22:05:41 +05:30
#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 CLKINT (1 << 2) // Bit 2 of CSR to specify CPU clock
#define CLOCK_HZ 16000000 // Clock frequency
#define INTEN (1 << 1) // Bit 1 of CSR to enable interrupt
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // value in microseconds
void GPIO_PORT_F_init(void) {
2024-09-22 22:05:41 +05:30
SYSCTL_RCGC2_R |= 0x00000020; // Enable clock to GPIOF
GPIO_PORTF_LOCK_R = 0x4C4F434B; // Unlock commit register
GPIO_PORTF_CR_R = 0x1F; // Make PORTF0 configurable
GPIO_PORTF_DEN_R = 0x1F; // Set PORTF digital enable
GPIO_PORTF_DIR_R = 0x0E; // Set PF0, PF4 as input and PF1, PF2, PF3 as output
GPIO_PORTF_PUR_R = 0x11; // PORTF PF0 and PF4 is pulled up
2024-09-22 22:05:41 +05:30
NVIC_EN0_R |= 1 << 30; // Enable interrupt for PORTF
GPIO_PORTF_IS_R = 0x00; // Make it edge-sensitive
GPIO_PORTF_IBE_R = 0x00; // Trigger on one edge
GPIO_PORTF_IEV_R = 0x00; // Falling edge event
GPIO_PORTF_IM_R |= 0x11; // Unmask interrupts for switch 1 and Switch 2
}
void systick_setting(void) {
STRELOAD = SYSTICK_RELOAD_VALUE(PWM_PERIOD/100); // Set reload value for 10 µs intervals (100 kHz)
STCTRL |= (CLKINT | ENABLE | INTEN); // Set internal clock, enable the timer
STCURRENT = 0; // Clear STCURRENT value
}
void delay(int us) // DEFINING DELAY FUNCTION
{
STRELOAD = SYSTICK_RELOAD_VALUE(us); // RELOAD VALUE FOR REQUIRED DELAY
STCURRENT = 0; // Clear STCURRENT
STCTRL |= (1 << 0) | (1 << 2); // Enable SysTick
while ((STCTRL & (1 << 16)) == 0); // Wait until flag is set
STCTRL &= 0x0; // Stop the timer
}
void SystickHandler(void)
{ // SysTick interrupt handler
2024-09-22 21:05:33 +05:30
static int ontime = 0;
ontime++;
if (ontime < duty)
{
2024-09-22 22:05:41 +05:30
GPIO_PORTF_DATA_R |= 0x02; // LED ON
}
else
{
2024-09-22 22:05:41 +05:30
GPIO_PORTF_DATA_R &= ~0x02; // LED OFF
}
if (ontime >= 100)
{
2024-09-22 22:05:41 +05:30
ontime = 0; // Reset counter after one period
}
2024-09-22 22:05:41 +05:30
if (buttonPressed) //Press Duration Measurement
{
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))
{
2024-09-22 22:05:41 +05:30
if (!buttonPressed) // If button was not pressed
{
buttonPressed = true;
2024-09-22 22:05:41 +05:30
pressDuration = 0; // Reset press duration
}
2024-09-22 22:05:41 +05:30
} else { // Button released
if (buttonPressed) { // Only if button was previously pressed
buttonPressed = false;
2024-09-22 22:05:41 +05:30
// Determine press duration
if (pressDuration > 100)
{ // Long press (>1 sec if using 10 ms ticks)
duty -= 5; // Decrease duty cycle
2024-09-22 22:05:41 +05:30
if (duty < 0)
{duty = 0;
}
} else if (pressDuration < 100){ // Short press
duty += 5; // decrease duty cycle
2024-09-22 22:05:41 +05:30
if (duty > 100)
{duty = 100;
}
}
}
GPIO_PORTF_ICR_R = 0x01; // Clear interrupt flag
}
}
2024-09-22 20:56:54 +05:30
int main(void) {
GPIO_PORT_F_init(); // GPIO initialization function
systick_setting(); // SysTick setup
2024-09-22 20:56:54 +05:30
while (1) {
// Main loop does nothing; everything is handled in interrupts
}
}