⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 LM3S系列UART例程:以FIFO中断方式接收
💻 C
字号:
#include  "systemInit.h"
#include  <uart.h>
#include  <ctype.h>
#include  <string.h>


#define  UARTCharGetNB      UARTCharGetNonBlocking


//  UART初始化
void uartInit(void)
{
    SysCtlPeriEnable(SYSCTL_PERIPH_UART2);                  //  使能UART模块
    SysCtlPeriEnable(SYSCTL_PERIPH_GPIOG);                  //  使能RX/TX所在的GPIO端口

    GPIOPinTypeUART(GPIO_PORTG_BASE,                        //  配置RX/TX所在管脚为
                    GPIO_PIN_0 | GPIO_PIN_1);               //  UART收发功能

    UARTConfigSet(UART2_BASE,                               //  配置UART端口
                  9600,                                     //  波特率:9600
                  UART_CONFIG_WLEN_8 |                      //  数据位:8
                  UART_CONFIG_STOP_ONE |                    //  停止位:1
                  UART_CONFIG_PAR_NONE);                    //  校验位:无

    UARTFIFOLevelSet(UART2_BASE,                            //  设置发送和接收FIFO深度
                     UART_FIFO_TX4_8,                       //  发送FIFO为2/8深度(4B)
                     UART_FIFO_RX6_8);                      //  接收FIFO为6/8深度(12B)

    UARTIntEnable(UART2_BASE, UART_INT_RX | UART_INT_RT);   //  使能接收和接收超时中断
    IntEnable(INT_UART2);                                   //  使能UART总中断
    IntMasterEnable();                                      //  使能处理器中断

    UARTEnable(UART2_BASE);                                 //  使能UART端口
}


//  通过UART发送字符串
void uartPuts(const char *s)
{
    while (*s != '\0')
    {
        UARTCharPut(UART2_BASE, *s++);
    }
}


//  定义接收缓冲区
#define  MAX_SIZE   40                                      //  缓冲区最大限制长度
char RxBuf[1 + MAX_SIZE];                                   //  接收缓冲区
int BufP = 0;                                               //  缓冲区位置变量
tBoolean RxEndFlag = false;                                 //  接收结束标志


//  以FIFO中断方式接收一个字符串,不回显,返回实际接收到的有效字符数
int uartFIFOGets(char *s, int size)
{
    int n;

    while (!RxEndFlag);
    n = BufP;
    BufP = 0;
    RxEndFlag = false;
    strncpy(s, RxBuf, size);
    s[MAX_SIZE] = '\0';

    return(n);
}


//  主函数(程序入口)
int main(void)
{
    char s[1 + MAX_SIZE];

    jtagWait();                                             //  防止JTAG失效,重要!
    clockInit();                                            //  时钟初始化:晶振,6MHz
    uartInit();                                             //  UART初始化

    for (;;)
    {
        if (uartFIFOGets(s, MAX_SIZE) > 0)
        {
            uartPuts(s);
            uartPuts("\r\n");
        }
    }
}


//  UART2中断服务函数
void UART2_ISR(void)
{
    char c;
    unsigned long ulStatus;

    ulStatus = UARTIntStatus(UART2_BASE, true);             //  读取当前中断状态
    UARTIntClear(UART2_BASE, ulStatus);                     //  清除中断状态

    if ((ulStatus & UART_INT_RX) || (ulStatus & UART_INT_RT))   //  若是接收中断或者
    {                                                           //      接收超时中断
        for (;;)
        {
            if (!UARTCharsAvail(UART2_BASE)) break;         //  若接收FIFO里无字符则跳出
            c = UARTCharGetNB(UART2_BASE);                  //  从接收FIFO里读取字符

            if (c == '\r')
            {
                UARTCharPut(UART2_BASE, '\r');              //  回显回车换行<CR><LF>
                UARTCharPut(UART2_BASE, '\n');
                RxEndFlag = true;                           //  接收结束标志置位
                break;
            }

            if (isprint(c))                                 //  若是可打印字符
            {
                if (BufP < MAX_SIZE)
                {
                    UARTCharPut(UART2_BASE, c);             //  回显
                    RxBuf[BufP++] = c;
                    RxBuf[BufP] = '\0';
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -