?? uartgetput.c
字號:
#include "uartGetPut.h"
#include <hw_types.h>
#include <hw_memmap.h>
#include <sysctl.h>
#include <gpio.h>
#include <uart.h>
#include <ctype.h>
// UART初始化
void uartInit(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART2); // 使能UART模塊
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG); // 使能RX/TX所在的GPIO端口
// 配置RX/TX所在管腳為UART收發功能
GPIOPinTypeUART(GPIO_PORTG_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// 配置UART端口,波特率9600,數據位8,停止位1,無校驗
UARTConfigSet(UART2_BASE, 9600, UART_CONFIG_WLEN_8 |
UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE);
UARTEnable(UART2_BASE); // 使能UART端口
}
// 通過UART發送一個字符
void uartPutc(const char c)
{
UARTCharPut(UART2_BASE, c);
}
// 通過UART發送字符串
void uartPuts(const char *s)
{
while (*s != '\0') uartPutc(*(s++));
}
// 通過UART接收一個字符
char uartGetc(void)
{
return(UARTCharGet(UART2_BASE));
}
// 功能:通過UART接收字符串,回顯,退格<Backspace>修改,回車<Enter>結束
// 參數:*s保存接收數據的緩沖區,只接收可打印字符(ASCII碼32~127)
// size是緩沖區*s的總長度,要求size >= 2(包括末尾'\0',建議用sizeof()來獲取)
// 返回:接收到的有效字符數目
int uartGets(char *s, int size)
{
char c;
int n = 0;
*s = '\0';
if (size < 2) return(0);
size--;
for (;;)
{
c = uartGetc(); // 接收1個字符
uartPutc(c); // 回顯輸入的字符
if (c == '\b') // 遇退格<Backspace>修改
{
if (n > 0)
{
*(--s) = '\0';
n--;
uartPuts(" \b"); // 顯示空格和退格<Backspace>
}
}
if (c == '\r') // 遇回車<Enter>結束
{
uartPuts("\r\n"); // 顯示回車換行<CR><LF>
break;
}
if (n < size) // 如果小于長度限制
{
if (isprint(c)) // 如果接收到的是可打印字符
{
*(s++) = c; // 保存接收到的字符到緩沖區
*s = '\0';
n++;
}
}
}
return(n); // 返回接收到的有效字符數目
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -