Add comments
This commit is contained in:
parent
73455674fa
commit
b5e4501940
97
main.c
97
main.c
|
@ -12,122 +12,113 @@ void GPIO_PORT_F_init(void)
|
||||||
GPIO_PORTF_PUR_R = 0x11; // PORTF PF0 and PF4 IS PULLED UP
|
GPIO_PORTF_PUR_R = 0x11; // PORTF PF0 and PF4 IS PULLED UP
|
||||||
|
|
||||||
NVIC_EN0_R |= 1 << 30;
|
NVIC_EN0_R |= 1 << 30;
|
||||||
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 PF0 and PF4
|
||||||
}
|
}
|
||||||
|
|
||||||
void GPIO_PORT_B_init(void)
|
void GPIO_PORT_B_init(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
SYSCTL_RCGCGPIO_R |= 0x02; // Enable clock to GPIOB
|
SYSCTL_RCGCGPIO_R |= 0x02; // Enable clock to GPIOB
|
||||||
SYSCTL_RCGCUART_R |= 0x02; // Enable UART1 clock
|
SYSCTL_RCGCUART_R |= 0x02; // Enable UART1 clock
|
||||||
|
|
||||||
GPIO_PORTB_DEN_R |= 0x03; // Enable PB0, PB1 as digital
|
GPIO_PORTB_DEN_R |= 0x03; // Enable PB0, PB1 as digital
|
||||||
GPIO_PORTB_AFSEL_R |= 0x03; // Enable alternate function on PB0, PB1
|
GPIO_PORTB_AFSEL_R |= 0x03; // Enable alternate function on PB0, PB1
|
||||||
|
|
||||||
GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R & 0xFFFFFF00) | 0x00000011; // Set PB0, PB1 for UART
|
GPIO_PORTB_PCTL_R = (GPIO_PORTB_PCTL_R & 0xFFFFFF00) | 0x00000011; // Set PB0, PB1 for UART
|
||||||
|
|
||||||
UART1_CTL_R &= ~0x01; // Disable UART1 during setup
|
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_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_FBRD_R = 11; // Set fractional part of baud rate
|
||||||
UART1_LCRH_R = 0x62; // 8-bit, odd parity, 1 stop bit
|
UART1_LCRH_R = 0x62; // 8-bit, odd parity, 1 stop bit
|
||||||
UART1_CC_R = 0x00; // Use system clock
|
UART1_CC_R = 0x00; // Use system clock
|
||||||
|
|
||||||
UART1_CTL_R |= 0x301; // Enable UART1, RX, and TX
|
UART1_CTL_R |= 0x301; // Enable UART1, RX, and TX
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UART1_WRITE(char data)
|
void UART1_WRITE(char data)
|
||||||
{
|
{
|
||||||
while (UART1_FR_R & 0x20); // Wait until TX FIFO is not full
|
while (UART1_FR_R & 0x20); // Wait until TX FIFO is not full
|
||||||
UART1_DR_R = data; // Write data to UART data register
|
UART1_DR_R = data; // Write data to UART data register
|
||||||
}
|
}
|
||||||
|
|
||||||
char UART1_READ(void)
|
char UART1_READ(void)
|
||||||
{
|
{
|
||||||
while (UART1_FR_R & 0x10); // Wait until RX FIFO is not empty
|
while (UART1_FR_R & 0x10); // Wait until RX FIFO is not empty
|
||||||
return (char)UART1_DR_R; // Return received data
|
return (char)UART1_DR_R; // Return received data
|
||||||
}
|
}
|
||||||
|
|
||||||
#define STCTRL *((volatile uint32_t *) 0xE000E010) // control and status
|
#define STCTRL *((volatile uint32_t *) 0xE000E010) // control and status
|
||||||
#define STRELOAD *((volatile uint32_t *) 0xE000E014) // reload value
|
#define STRELOAD *((volatile uint32_t *) 0xE000E014) // reload value
|
||||||
#define STCURRENT *((volatile uint32_t *) 0xE000E018) // current value
|
#define STCURRENT *((volatile uint32_t *) 0xE000E018) // current value
|
||||||
|
|
||||||
#define CLOCK_HZ 16000000 // CLOCK FREQUENCY OF EK-TM4C123GXL
|
#define CLOCK_HZ 16000000 // CLOCK FREQUENCY
|
||||||
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // SYSTICK RELOAD VALUE
|
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // SYSTICK RELOAD VALUE
|
||||||
|
|
||||||
void systick_setting(void) // SYSTICK SETUP FUNCTION
|
|
||||||
|
void systick_setting(void) // SYSTICK SETUP FUNCTION
|
||||||
{
|
{
|
||||||
STRELOAD = SYSTICK_RELOAD_VALUE(1000); // RELOAD VALUE FOR 1ms
|
STRELOAD = SYSTICK_RELOAD_VALUE(1000); // RELOAD VALUE FOR 1ms
|
||||||
STCTRL |= (1 << 0) | (1 << 2); // Enable SysTick with system clock
|
STCTRL |= (1 << 0) | (1 << 2); // Enable SysTick with system clock
|
||||||
STCURRENT = 0; // Clear current value
|
STCURRENT = 0; // Clear current value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void delay(int us) //DEFINING DELAY FUNCTION
|
void delay(int us) // DEFINING DELAY FUNCTION
|
||||||
{
|
{
|
||||||
STRELOAD = SYSTICK_RELOAD_VALUE(us); // RELOAD VALUE FOR REQUIRED DELAY
|
STRELOAD = SYSTICK_RELOAD_VALUE(us); // RELOAD VALUE FOR REQUIRED DELAY
|
||||||
STCURRENT = 0; // Clear STCURRENT
|
STCURRENT = 0; // Clear STCURRENT
|
||||||
STCTRL |= (1 << 0) | (1 << 2); // Enable SysTick
|
STCTRL |= (1 << 0) | (1 << 2); // Enable SysTick
|
||||||
while ((STCTRL & (1 << 16)) == 0); // Wait until flag is set
|
while ((STCTRL & (1 << 16)) == 0); // Wait until flag is set
|
||||||
STCTRL &= 0x0; // Stop the timer
|
STCTRL &= 0x0; // Stop the timer
|
||||||
}
|
}
|
||||||
|
|
||||||
void STATUS_LED(char received_data)
|
void STATUS_LED(char received_data)
|
||||||
{
|
{
|
||||||
if (received_data == 0xAA)
|
if (received_data == 0xAA)
|
||||||
{
|
{
|
||||||
GPIO_PORTF_DATA_R |= 0x08; // Turn on Green LED (PF3)
|
GPIO_PORTF_DATA_R |= 0x08; // Turn on Green LED (PF3)
|
||||||
GPIO_PORTF_DATA_R &= ~0x04; // Turn off Blue LED (PF2)
|
GPIO_PORTF_DATA_R &= ~0x04; // Turn off Blue LED (PF2)
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (received_data == 0xF0)
|
else if (received_data == 0xF0)
|
||||||
{
|
{
|
||||||
GPIO_PORTF_DATA_R |= 0x04; // Turn on Blue LED (PF2)
|
GPIO_PORTF_DATA_R |= 0x04; // Turn on Blue LED (PF2)
|
||||||
GPIO_PORTF_DATA_R &= ~0x08; // Turn off Green LED (PF3)
|
GPIO_PORTF_DATA_R &= ~0x08; // Turn off Green LED (PF3)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
GPIO_PORTF_DATA_R |= 0x02;
|
GPIO_PORTF_DATA_R |= 0x02;
|
||||||
}
|
}
|
||||||
//delay(500000);
|
|
||||||
//GPIO_PORTF_DATA_R &= 0x00; // Turn on Green LED (PF3)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void GPIOF_interruptHandler(void) // Interrupt handler for GPIO Port F
|
void GPIOF_interruptHandler(void) // Interrupt handler for GPIO Port F
|
||||||
{
|
{
|
||||||
//delay(200000); // Debounce delay
|
if (GPIO_PORTF_RIS_R & 0x10) // Check if PF4 caused the interrupt
|
||||||
// Check PF4 (Switch 2)
|
|
||||||
if (GPIO_PORTF_RIS_R & 0x10) // Check if PF4 caused the interrupt
|
|
||||||
{
|
{
|
||||||
//GPIO_PORTF_DATA_R |= 0x02; // Toggle RED LED (PF1)
|
|
||||||
//delay(500000);
|
|
||||||
UART1_WRITE(0xF0);
|
UART1_WRITE(0xF0);
|
||||||
STATUS_LED(UART1_READ());
|
STATUS_LED(UART1_READ());
|
||||||
//GPIO_PORTF_DATA_R = 0x00; // Toggle RED LED (PF1)
|
GPIO_PORTF_ICR_R = 0x10; // Clear interrupt flag for PF4
|
||||||
GPIO_PORTF_ICR_R = 0x10; // Clear interrupt flag for PF4
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check PF0 (Switch 1)
|
if (GPIO_PORTF_RIS_R & 0x01) // Check if PF0 caused the interrupt
|
||||||
if (GPIO_PORTF_RIS_R & 0x01) // Check if PF0 caused the interrupt
|
|
||||||
{
|
{
|
||||||
//GPIO_PORTF_DATA_R |= 0x04; // Toggle RED LED (PF1)
|
|
||||||
//delay(500000);
|
|
||||||
UART1_WRITE(0xAA);
|
UART1_WRITE(0xAA);
|
||||||
//GPIO_PORTF_DATA_R = 0x00;
|
|
||||||
STATUS_LED(UART1_READ());
|
STATUS_LED(UART1_READ());
|
||||||
GPIO_PORTF_ICR_R = 0x01; // Clear interrupt flag for PF0
|
GPIO_PORTF_ICR_R = 0x01; // Clear interrupt flag for PF0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int main(void) // MAIN FUNCTION
|
|
||||||
|
|
||||||
|
int main(void) // MAIN FUNCTION
|
||||||
{
|
{
|
||||||
GPIO_PORT_F_init(); // GPIO INITIALISATION FUNCTION
|
GPIO_PORT_F_init(); // GPIO INITIALISATION FUNCTION
|
||||||
GPIO_PORT_B_init();
|
GPIO_PORT_B_init();
|
||||||
systick_setting(); // SYSTICK SETUP
|
systick_setting(); // SYSTICK SETUP
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
STATUS_LED(UART1_READ());
|
STATUS_LED(UART1_READ());
|
||||||
|
|
Loading…
Reference in New Issue