#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
void uart0_init(uint32_t ui32Baud);
void uart_sendstring(char *str);
void uart_recestring(void);
int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
uart0_init(115200);
uart_sendstring("123321456654\r\n");
uart_recestring();
while(1);
}
void uart0_init(uint32_t ui32Baud)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); // 使能串口0
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // 使能GPIOA
GPIOPinConfigure(GPIO_PA0_U0RX); // 将PA0复用为串口0 RX
GPIOPinConfigure(GPIO_PA1_U0TX); // 将PA1复用为串口0 TX
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // 将GPIO配置为串口功能
UARTConfigSetExpClk(UART0_BASE,
SysCtlClockGet(), // 获取时钟频率
ui32Baud, // 波特率 : ui32Baud
(UART_CONFIG_WLEN_8 | // 字节宽 : 8
UART_CONFIG_STOP_ONE | // 停止位 : 1
UART_CONFIG_PAR_NONE)); // 校验位 : 无
}
void uart_sendstring(char *str)
{
unsigned int k=0;
do
{
UARTCharPut(UART0_BASE, *(str + k));
k++;
} while(*(str + k)!='\0');
}
void uart_recestring(void)
{
char cThisChar;
do
{
cThisChar = UARTCharGet(UART0_BASE); // 接收一个字节
UARTCharPut(UART0_BASE, cThisChar); // 将接收到的字节发送出去
}
while((cThisChar != '\n') && (cThisChar != '\r'));
}