📄 lm3s_uart.c
字号:
#include "LM3S_UART.H"
#include "LM3S1138_PinMap.H"
#include <hw_types.h>
#include <hw_memmap.h>
#include <hw_sysctl.h>
#include <hw_gpio.h>
#include <hw_uart.h>
#include <sysctl.h>
#include <gpio.h>
#include <uart.h>
#include <ctype.h>
#include <hw_ints.h>
#include <hw_types.h>
#include <hw_memmap.h>
#include <hw_ints.h>
#include <interrupt.h>
#include <sysctl.h>
#include <gpio.h>
#include <timer.h>
#include <interrupt.h>
#include <sysctl.h>
#include <hw_ints.h>
#include <hw_timer.h>
#define SysCtlPeriEnable SysCtlPeripheralEnable
/***************************************************************************************************
功能:UART0收发初始化
参数:无
返回:无
***************************************************************************************************/
void UART0_Init(void)
{
SysCtlPeriEnable(SYSCTL_PERIPH_UART0); // 使能UART0模块
SysCtlPeriEnable(U0RX_PERIPH); // 使能RX所在的GPIO端口
GPIOPinTypeUART(U0RX_PORT , U0RX_PIN); // 配置RX所在管脚为UART功能
SysCtlPeriEnable(U0TX_PERIPH); // 使能TX所在的GPIO端口
GPIOPinTypeUART(U0TX_PORT , U0TX_PIN); // 配置TX所在管脚为UART功能
UARTConfigSet(UART0_BASE , 9600 , UART_CONFIG_WLEN_8 | // 配置UART,波特率9600,数据位8
UART_CONFIG_STOP_ONE | // 停止位1
UART_CONFIG_PAR_NONE); // 无校验
UARTEnable(UART0_BASE); // 使能UART端口
}
/***************************************************************************************************
功能:通过UART0发送1个字符
参数:c是要发送的字符
返回:无
***************************************************************************************************/
void UART0_Putc(const unsigned char c)
{
UARTCharPut(UART0_BASE , c);
}
/***************************************************************************************************
功能:通过UART0发送字符串
参数:s是要发送的字符串
返回:无
***************************************************************************************************/
void UART0_Puts(const unsigned char *s)
{
char c;
for (;;)
{
c = *(s++);
if (c == '\0')
{
break;
}
UART0_Putc(c);
}
}
/***************************************************************************************************
功能:通过UART接收1个字符
参数:无
返回:接收到的字符
***************************************************************************************************/
char UART0_Getc(void)//没有调用
{
char c;
c = UARTCharGet(UART0_BASE);
return(c);
}
/***************************************************************************************************
功能:通过UART0接收字符串(要求输入的字符串以"\r\n"结尾)
参数:s 保存接收数据的缓冲区(只接收可打印字符,即ASCII码32~127)
size 接收缓冲区限制长度
返回:实际接收到的有效字符数量(字符串长度)
***************************************************************************************************/
int UART0_Gets(char *s , const int size)//没有调用
{
char c;
int n = 0;
for (;;)
{
c = UART0_Getc(); // 接收1个字符
if ( c == '\r' ) // 如果遇到回车符,则跳出
{
break;
}
if ( n < size ) // 如果小于长度限制
{
if ( isprint(c) ) // 如果接收到的是可打印字符
{
*(s++) = c; // 保存接收到的字符到缓冲区
n++;
}
}
}
*s = '\0'; // 添加字符串结束标志
return(n); // 返回有效的字符数
}
/***************************************************************************************************
功能:UART1收发初始化
参数:无
返回:无
***************************************************************************************************/
void UART1_Init(void)
{
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOD); /* 使能UART1所在的GPIOA端口 */
SysCtlPeriEnable(SYSCTL_PERIPH_UART1); /* 使能UART1模块 */
GPIOPinTypeUART(GPIO_PORTD_BASE , /* 配置PD2和PD3为UART功能 */
GPIO_PIN_2 | GPIO_PIN_3);
UARTConfigSet(UART1_BASE ,
9600 , /* 波特率:9600 */
UART_CONFIG_WLEN_8 | /* 数据位:8 */
UART_CONFIG_STOP_ONE | /* 停止位:1 */
UART_CONFIG_PAR_NONE); /* 校验位:无 */
UARTFIFOLevelSet(UART1_BASE , /* 设置发送和接收的FIFO深度 */
UART_FIFO_TX1_8,
UART_FIFO_RX7_8);
UARTIntEnable(UART1_BASE , /* 使能接收中断和接收超时中断 */
UART_INT_RX | UART_INT_RT);
IntPrioritySet(INT_UART1 , 4 << 5);
IntEnable(INT_UART1); /* 使能UART1总中断 */
IntMasterEnable(); /* 使能处理器中断 */
UARTEnable(UART1_BASE); /* 使能UART1端口 */
}
/***************************************************************************************************
功能:通过UART1发送1个字符
参数:c是要发送的字符
返回:无
***************************************************************************************************/
void UART1_Putc(const unsigned char c)
{
UARTCharPut(UART1_BASE , c);
}
/***************************************************************************************************
功能:通过UART1送字符串
参数:s是要发送的字符串
返回:无
***************************************************************************************************/
void UART1_Puts(const unsigned char *s)
{
char c = 0,i;
for (i=0;i<3;i++)
{
c = *(s++);
UART1_Putc(c);
}
}
/***************************************************************************************************
功能:通过UART1接收1个字符
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -