查看: 7720|回复: 8

【有奖征文】LM4F Launchpad上手系列3-UART

  [复制链接]
  • TA的每日心情
    奋斗
    2013-2-28 11:51
  • 签到天数: 49 天

    连续签到: 1 天

    [LV.5]常住居民I

    发表于 2012-12-18 13:02:01 | 显示全部楼层 |阅读模式
    分享到:
    最近事情太多,已经有将近一个礼拜没有摆弄LM4F Launchpad了,
    今天中午抽空,再一次开启CCS,摆弄摆弄,这次我们主要研究LM4F的UART
    及UART中断接收方式的使用。

    我们依旧使用CCS作为开发平台,首先创建工程,如下图所示:
    1.png
    创建完工程后,配置Include目录及导入lib文件,这在之前的帖子中已经有详述,
    需要的朋友可以查看一下我以前的帖子,有问题可以站内发信给我。

    工程创建完后,创建一个uart.c文件,如下图所示:
    2.png

    好了,至此我们可以正式开始今天的实验了。

    首先键入我们要用到的头文件:
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/debug.h"
    #include "driverlib/fpu.h"
    #include "driverlib/gpio.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/rom.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"

    由于我们需要使用中断,因而需要在startup_css.c中配置好uart0的中断函数
    4.png
    如上图所示,我们的uart0中断子程序名称为UARTIntHandler,同时
    在startup_css.c文件顶部加入:extern void UARTIntHandler(void);
    用以表明该函数将在其他地方声明,在本次实验中,我们将该函数定义在uart.c中。

    切换回uart.c,加入uart0中断处理程序
    //*****************************************************************************
    //
    // The UART interrupt handler.
    //
    //*****************************************************************************
    void
    UARTIntHandler(void)
    {
        unsigned long ulStatus;

        //
        // Get the interrrupt status.
        //
        ulStatus = ROM_UARTIntStatus(UART0_BASE, true);

        //
        // Clear the asserted interrupts.
        //
        ROM_UARTIntClear(UART0_BASE, ulStatus);

        //
        // Loop while there are characters in the receive FIFO.
        //
        while(ROM_UARTCharsAvail(UART0_BASE))
        {
            //
            // Read the next character from the UART and write it back to the UART.
            //
            ROM_UARTCharPutNonBlocking(UART0_BASE,
                                       ROM_UARTCharGetNonBlocking(UART0_BASE));

            //
            // Blink the LED to show a character transfer is occuring.
            //
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);

            //
            // Delay for 1 millisecond.  Each SysCtlDelay is about 3 clocks.
            //
            SysCtlDelay(SysCtlClockGet() / (1000 * 3));

            //
            // Turn off the LED
            //
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

        }
    }

    串口发送程序:
    //*****************************************************************************
    //
    // Send a string to the UART.
    //
    //*****************************************************************************
    void
    UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
    {
        //
        // Loop while there are more characters to send.
        //
        while(ulCount--)
        {
            //
            // Write the next character to the UART.
            //
            ROM_UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
        }
    }

    主程序:
    //*****************************************************************************
    //
    // This example demonstrates how to send a string of data to the UART.
    //
    //*****************************************************************************
    int
    main(void)
    {
        //
        // Enable lazy stacking for interrupt handlers.  This allows floating-point
        // instructions to be used within interrupt handlers, but at the expense of
        // extra stack usage.
        //
        ROM_FPUEnable();
        ROM_FPULazyStackingEnable();

        //
        // Set the clocking to run directly from the crystal.
        //
        ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                           SYSCTL_XTAL_16MHZ);

        //
        // Enable the GPIO port that is used for the on-board LED.
        //
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

        //
        // Enable the GPIO pins for the LED (PF2).
        //
        ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);

        //
        // Enable the peripherals used by this example.
        //
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

        //
        // Enable processor interrupts.
        //
        ROM_IntMasterEnable();

        //
        // Set GPIO A0 and A1 as UART pins.
        //
        GPIOPinConfigure(GPIO_PA0_U0RX);
        GPIOPinConfigure(GPIO_PA1_U0TX);
        ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

        //
        // Configure the UART for 115,200, 8-N-1 operation.
        //
        ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
                                (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                                 UART_CONFIG_PAR_NONE));

        //
        // Enable the UART interrupt.
        //
        ROM_IntEnable(INT_UART0);
        ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

        //
        // Prompt for text to be entered.
        //
        UARTSend((unsigned char *)"\033[2JEnter text: ", 16);

        //
        // Loop forever echoing data through the UART.
        //
        while(1)
        {
        }
    }

    编译、下载到LM4F Launchpad后,打开串口调试助手,
    设置波特率115200,8bit数据位,无校验,1位停止位
    然后Reset我们的LM4F Launchpad,在串口调试助手上可以看到“Enter text”字样

    我们随意在发送框键入字符,发送后,在接收窗口可以看到我们所发送的字符串。
    3.png

    本帖旨在引导大家进行开发的思路,更为细节的函数这里就不多赘述,大家在实际
    开发过程中遇到问题的话,可以跟帖询问,我看到会回复的。


    回复

    使用道具 举报

  • TA的每日心情

    2015-2-14 09:02
  • 签到天数: 656 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2012-12-18 13:05:53 | 显示全部楼层
    支持一下,写的不错
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2014-7-16 09:10
  • 签到天数: 361 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2012-12-18 13:06:00 | 显示全部楼层
    我第一个,抢沙发
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    2020-3-19 13:22
  • 签到天数: 805 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2012-12-18 13:06:59 | 显示全部楼层
    支持一下           
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2020-9-28 10:10
  • 签到天数: 1018 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2012-12-18 13:12:45 | 显示全部楼层
    支持支持!!!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2016-6-12 21:59
  • 签到天数: 647 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2012-12-18 13:14:41 | 显示全部楼层
    如果有个机会玩LM4F Launchpad,一定要比你玩的好~{:soso_e130:}
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2013-5-5 12:36
  • 签到天数: 33 天

    连续签到: 1 天

    [LV.5]常住居民I

    发表于 2012-12-18 14:08:18 | 显示全部楼层
    顶一个
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2013-11-30 17:53
  • 签到天数: 193 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2013-3-8 08:12:25 | 显示全部楼层
    本帖最后由 repo 于 2013-3-8 08:13 编辑

    编译时出现这样的提示是什么情况?
    repo.png
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2013-9-20 15:53:01 | 显示全部楼层
    repo 发表于 2013-3-8 08:12
    编译时出现这样的提示是什么情况?

    打开工程属性(alt+enter),  Build----->ARM Compiler---->Advanced Options---->Predefined Symbols
    在Pre-define NAME里面分别加入如下内容:
    PART_LM4F120H5QR
    TARGET_IS_BLIZZARD_RA2
    PART_TM4C1233H6PM
    TARGET_IS_PART_TM4C1233H6PM
    TARGET_IS_BLIZZARD_RA1
    ccs="ccs"
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 00:26 , Processed in 0.197242 second(s), 33 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.