2024-10-10 19:31:16 +05:30
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "tm4c123gh6pm.h"
|
2024-10-10 19:32:09 +05:30
|
|
|
|
|
|
|
void GPIO_PORT_F_init(void)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
|
NVIC_EN0_R |= 1 << 30;
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORTF_IS_R = 0x00; // Make it edge-sensitive
|
|
|
|
GPIO_PORTF_IBE_R = 0x00; // Trigger on one edge
|
2024-10-10 19:32:09 +05:30
|
|
|
GPIO_PORTF_IEV_R = 0x00; // Falling edge event
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORTF_IM_R |= 0x11; // Unmask interrupts for PF0 and PF4
|
2024-10-10 19:32:09 +05:30
|
|
|
}
|
2024-10-10 19:33:05 +05:30
|
|
|
|
2024-10-10 20:04:27 +05:30
|
|
|
void GPIO_PORT_B_init(void)
|
|
|
|
{
|
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
SYSCTL_RCGCGPIO_R |= 0x02; // Enable clock to GPIOB
|
|
|
|
SYSCTL_RCGCUART_R |= 0x02; // Enable UART1 clock
|
2024-10-10 20:04:27 +05:30
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORTB_DEN_R |= 0x03; // Enable PB0, PB1 as digital
|
|
|
|
GPIO_PORTB_AFSEL_R |= 0x03; // Enable alternate function on PB0, PB1
|
2024-10-10 20:04:27 +05:30
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R & 0xFFFFFF00) | 0x00000011; // Set PB0, PB1 for UART
|
2024-10-10 20:04:27 +05:30
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
UART1_CTL_R &= ~0x01; // Disable UART1 during setup
|
|
|
|
UART1_IBRD_R = 104; // Set integer part of baud rate (for 9600 baud at 16 MHz clock)
|
|
|
|
UART1_FBRD_R = 11; // Set fractional part of baud rate
|
|
|
|
UART1_LCRH_R = 0x62; // 8-bit, odd parity, 1 stop bit
|
|
|
|
UART1_CC_R = 0x00; // Use system clock
|
2024-10-10 20:04:27 +05:30
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
UART1_CTL_R |= 0x301; // Enable UART1, RX, and TX
|
2024-10-10 20:04:27 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-10-10 19:33:05 +05:30
|
|
|
void UART1_WRITE(char data)
|
|
|
|
{
|
2024-10-10 20:27:33 +05:30
|
|
|
while (UART1_FR_R & 0x20); // Wait until TX FIFO is not full
|
|
|
|
UART1_DR_R = data; // Write data to UART data register
|
2024-10-10 19:33:05 +05:30
|
|
|
}
|
2024-10-10 19:53:36 +05:30
|
|
|
|
|
|
|
char UART1_READ(void)
|
|
|
|
{
|
2024-10-10 20:27:33 +05:30
|
|
|
while (UART1_FR_R & 0x10); // Wait until RX FIFO is not empty
|
|
|
|
return (char)UART1_DR_R; // Return received data
|
2024-10-10 19:53:36 +05:30
|
|
|
}
|
|
|
|
|
2024-10-10 20:09:42 +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
|
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
#define CLOCK_HZ 16000000 // CLOCK FREQUENCY
|
2024-10-10 20:09:42 +05:30
|
|
|
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // SYSTICK RELOAD VALUE
|
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
|
|
|
|
void systick_setting(void) // SYSTICK SETUP FUNCTION
|
2024-10-10 20:09:42 +05:30
|
|
|
{
|
2024-10-10 20:27:33 +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-10-10 20:09:42 +05:30
|
|
|
}
|
|
|
|
|
2024-10-10 19:53:36 +05:30
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
void delay(int us) // DEFINING DELAY FUNCTION
|
2024-10-10 20:13:18 +05:30
|
|
|
{
|
2024-10-10 20:27:33 +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
|
2024-10-10 20:13:18 +05:30
|
|
|
}
|
|
|
|
|
2024-10-10 20:16:49 +05:30
|
|
|
void STATUS_LED(char received_data)
|
|
|
|
{
|
|
|
|
if (received_data == 0xAA)
|
|
|
|
{
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORTF_DATA_R |= 0x08; // Turn on Green LED (PF3)
|
|
|
|
GPIO_PORTF_DATA_R &= ~0x04; // Turn off Blue LED (PF2)
|
2024-10-10 20:16:49 +05:30
|
|
|
}
|
|
|
|
else if (received_data == 0xF0)
|
|
|
|
{
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORTF_DATA_R |= 0x04; // Turn on Blue LED (PF2)
|
|
|
|
GPIO_PORTF_DATA_R &= ~0x08; // Turn off Green LED (PF3)
|
2024-10-10 20:16:49 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GPIO_PORTF_DATA_R |= 0x02;
|
|
|
|
}
|
2024-10-10 20:13:18 +05:30
|
|
|
|
2024-10-10 20:16:49 +05:30
|
|
|
}
|
2024-10-10 20:13:18 +05:30
|
|
|
|
2024-10-10 19:53:36 +05:30
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
void GPIOF_interruptHandler(void) // Interrupt handler for GPIO Port F
|
2024-10-10 19:34:52 +05:30
|
|
|
{
|
2024-10-10 20:27:33 +05:30
|
|
|
if (GPIO_PORTF_RIS_R & 0x10) // Check if PF4 caused the interrupt
|
2024-10-10 19:34:52 +05:30
|
|
|
{
|
|
|
|
UART1_WRITE(0xF0);
|
|
|
|
STATUS_LED(UART1_READ());
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORTF_ICR_R = 0x10; // Clear interrupt flag for PF4
|
2024-10-10 19:34:52 +05:30
|
|
|
}
|
|
|
|
|
2024-10-10 20:27:33 +05:30
|
|
|
if (GPIO_PORTF_RIS_R & 0x01) // Check if PF0 caused the interrupt
|
2024-10-10 19:34:52 +05:30
|
|
|
{
|
|
|
|
UART1_WRITE(0xAA);
|
|
|
|
STATUS_LED(UART1_READ());
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORTF_ICR_R = 0x01; // Clear interrupt flag for PF0
|
2024-10-10 19:34:52 +05:30
|
|
|
}
|
|
|
|
}
|
2024-10-10 20:27:33 +05:30
|
|
|
|
|
|
|
|
|
|
|
int main(void) // MAIN FUNCTION
|
2024-10-10 19:36:05 +05:30
|
|
|
{
|
2024-10-10 20:27:33 +05:30
|
|
|
GPIO_PORT_F_init(); // GPIO INITIALISATION FUNCTION
|
2024-10-10 20:09:42 +05:30
|
|
|
GPIO_PORT_B_init();
|
2024-10-10 20:27:33 +05:30
|
|
|
systick_setting(); // SYSTICK SETUP
|
2024-10-10 19:36:05 +05:30
|
|
|
while (1)
|
|
|
|
{
|
2024-10-10 20:16:49 +05:30
|
|
|
STATUS_LED(UART1_READ());
|
2024-10-10 19:36:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|