0% found this document useful (0 votes)
21 views2 pages

Char Put Init

Uploaded by

Đinh Thế Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Char Put Init

Uploaded by

Đinh Thế Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdint.

h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/systick.h"
#include "driverlib/uart.h"

// Function Prototypes
void UARTIntHandler(void);

// Global Variables
volatile int clock = 0;

int main() {
// Set system clock (50MHz from external 16MHz crystal)
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
SYSCTL_OSC_MAIN);

// Enable UART0 and GPIOA


SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

// Configure UART pins


GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

// UART Configuration
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 9600,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));

// Enable UART interrupts


IntEnable(INT_UART0);
UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

// Enable GPIOF for LED control


SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); // Blue LED (PF2)

// Turn off LED initially


GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x00);

// Enable global interrupts


IntMasterEnable();

while (1) {
// Main loop does nothing; interrupts handle the tasks
}
}

// ------------------------ UART Interrupt Handler ------------------------


void UARTIntHandler(void) {
uint32_t status = UARTIntStatus(UART0_BASE, true); // Get interrupt status
UARTIntClear(UART0_BASE, status); // Clear handled interrupts

while (UARTCharsAvail(UART0_BASE)) { // Check if data is available


char c = UARTCharGetNonBlocking(UART0_BASE); // Read received character

// Echo received character back


UARTCharPutNonBlocking(UART0_BASE, c);

// Check if the received character is '1'


if (c == '1') {
// Toggle Blue LED (PF2)
uint8_t ledState = GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2);
if (ledState) {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x00); // Turn LED OFF
} else {
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2); // Turn LED
ON
}
}
}
}

You might also like