Group21_Lab08/main.c

85 lines
3.2 KiB
C

#include <stdint.h>
#include <stdbool.h>
#include "tm4c123gh6pm.h"
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;
GPIO_PORTF_IS_R = 0x00; // EDGE SENSITIVE
GPIO_PORTF_IBE_R = 0x00; // ONE EDGE
GPIO_PORTF_IEV_R = 0x00; // INTERRUPT EVENT FALLING
GPIO_PORTF_IM_R |= 0x11; // UNMASK INTERRUPT
}
void GPIO_PORT_B_init(void)
{
SYSCTL_RCGCGPIO_R |= 0x02; // ENABLE CLOCK FOR GPIOB
SYSCTL_RCGCUART_R |= 0x02; // ENABLE CLOCK FOR UART1
GPIO_PORTB_DEN_R |= 0x03; // DIGITAL ENABLE FOR PB0 AND 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 FUNCTIONALITY
UART1_CTL_R &= ~0x01; // DISABLE UART1 DURING SETUP
UART1_IBRD_R = 104; // SET INTEGER PART OF BAUD RED FOR 9600 BAUD FORMULA:
UART1_FBRD_R = 11; // SET FRACTIONAL PART OF BAUD RATE
UART1_LCRH_R = 0x62; // SET 8-BIT, ODD PARITY, STOP BIT-1
UART1_CC_R = 0x00; // USE SYSTEM CLOCK
UART1_CTL_R |= 0x301; // ENABLE UART1, RX AND TX
}
void UART1_WRITE(char data) // UART1 WRITE DATA
{
while (UART1_FR_R & 0x20); // WAIT UNTIL TX FIFO IS NOT FULL
UART1_DR_R = data; // WRITE DATA TO UART DATA REGISTER
}
char UART1_READ(void) // UART1 READ DATA
{
while (UART1_FR_R & 0x10); // WAIT UNTIL RX FIFO IS NOT EMPTY
return (char)UART1_DR_R; // RETURN RECEIVED DATA
}
void UART1_RXTX_to_DISPLAY(char RX_DATA)
{
if (RX_DATA == 0x52)
{
GPIO_PORTF_DATA_R |= 0x02; // IF RECEIVED CHARACTER IS R TURN ON RED LED
}
else if (RX_DATA == 0x42)
{
GPIO_PORTF_DATA_R |= 0x04; // IF RECEIVED CHARACTER IS B TURN ON BLUE LED
}
else if (RX_DATA == 0x47)
{
GPIO_PORTF_DATA_R |= 0x08; // IF RECEIVED CHARACTER IS G TURN ON GREEN LED
}
else
{
GPIO_PORTF_DATA_R &= 0x00; // RED LED FOR JUNK DATA
}
UART1_WRITE(RX_DATA); // SEND RECEIVED DATA TO DISPLAY
}
int main(void) // MAIN FUNCTION
{
GPIO_PORT_F_init(); // GPIO PORT F INITIALISATION FUNCTION
GPIO_PORT_B_init(); // GPIO PORT B INITIALISATION FUNCTION
while (1)
{
UART1_RXTX_to_DISPLAY(UART1_READ()); // RECEIVE DATA, SEND RECEIVED DATA TO DISPLAY OR GLOW LED
}
}