2024-09-20 21:50:30 +05:30
|
|
|
/*
|
|
|
|
Problem Statement:
|
|
|
|
Write a program where the press of a switch is detected by a GPIO interrupt. On each button press the interrupt handler should toggle the state of RED LED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2024-09-20 19:51:56 +05:30
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2024-09-20 19:57:15 +05:30
|
|
|
#include "tm4c123gh6pm.h"
|
|
|
|
// SysTick memory-mapped registers
|
2024-09-20 21:38:29 +05:30
|
|
|
#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
|
|
|
|
#define CLOCK_HZ 16000000 // CLOCK FREQUENCY OF EK-TM4C123GXL
|
2024-09-20 20:18:04 +05:30
|
|
|
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // SYSTICK RELOAD VALUE
|
|
|
|
|
|
|
|
void GPIO_PORT_F_init(void)
|
|
|
|
{
|
2024-09-20 21:38:29 +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 and PF3 as output
|
|
|
|
GPIO_PORTF_PUR_R = 0x11; // PORTF PF0 and PF4 IS PULLED UP
|
2024-09-20 20:18:04 +05:30
|
|
|
|
|
|
|
NVIC_EN0_R |= 1 << 30;
|
2024-09-20 21:50:30 +05:30
|
|
|
GPIO_PORTF_IS_R = 0x00; // INTERRUPT SENSE EDGE SENSITIVE
|
|
|
|
GPIO_PORTF_IBE_R = 0x00; // INTERRUPT GENERATION IS CONTROLLED BY THE GPIO INTERRUPT EVENT
|
|
|
|
GPIO_PORTF_IEV_R = 0x00; // INTERRUPT EVENT FALLING EDGE
|
|
|
|
GPIO_PORTF_IM_R |= 0x11; // INTERRUPT MASK ENABLE FOR SWITCH1 AND SWITCH2
|
2024-09-20 20:18:04 +05:30
|
|
|
}
|
2024-09-20 21:32:19 +05:30
|
|
|
void systick_setting(void) // SYSTICK SETUP FUNCTION
|
2024-09-20 20:20:35 +05:30
|
|
|
{
|
2024-09-20 21:32:19 +05:30
|
|
|
STRELOAD = SYSTICK_RELOAD_VALUE(1000); // RELOAD VALUE FOR 1ms
|
|
|
|
STCTRL |= (1 << 0) | (1 << 2); // Enable SysTick with system clock
|
|
|
|
STCURRENT = 0; // Clear current value
|
2024-09-20 20:20:35 +05:30
|
|
|
}
|
2024-09-20 20:21:11 +05:30
|
|
|
|
2024-09-20 21:50:30 +05:30
|
|
|
void delay(int us) // DEFINING DELAY FUNCTION
|
2024-09-20 20:21:11 +05:30
|
|
|
{
|
2024-09-20 21:32:19 +05:30
|
|
|
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 GPIOF_interruptHandler(void) // Interrupt handler for GPIO Port F
|
|
|
|
{
|
|
|
|
delay(200000); // DEBOUNCE DELAY
|
|
|
|
if (GPIO_PORTF_RIS_R & 0x10) // CHECK IF SWITCH 1 CAUSED INTERRUPT
|
|
|
|
{
|
2024-09-20 21:50:30 +05:30
|
|
|
GPIO_PORTF_DATA_R ^= 0x02; // TOGGLE RED LED
|
|
|
|
GPIO_PORTF_ICR_R = 0x10; // CLEAR INTERRUPT FLAG FOR SWITCH 1
|
2024-09-20 21:32:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (GPIO_PORTF_RIS_R & 0x01) // CHECK IF SWITCH 2 CAUSED INTERRUPT
|
|
|
|
{
|
2024-09-20 21:50:30 +05:30
|
|
|
GPIO_PORTF_DATA_R ^= 0x02; // TOGGLE RED LED
|
|
|
|
GPIO_PORTF_ICR_R = 0x01; // CLEAR INTERRUPT FLAG FOR SWITCH 1
|
2024-09-20 21:32:19 +05:30
|
|
|
}
|
2024-09-20 20:21:11 +05:30
|
|
|
}
|
2024-09-20 21:38:29 +05:30
|
|
|
|
|
|
|
int main(void) // MAIN FUNCTION
|
|
|
|
{
|
|
|
|
GPIO_PORT_F_init(); // GPIO INITIALISATION FUNCTION
|
|
|
|
systick_setting(); // SYSTICK SETUP
|
|
|
|
while (1)
|
|
|
|
{ // DO NOTHING
|
|
|
|
}
|
|
|
|
}
|