打印

LM3S811使用心得之三 串口通信

[复制链接]
6839|27
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
nwx8899|  楼主 | 2011-11-28 15:15 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 nwx8899 于 2011-12-28 20:14 编辑

LM3S811使用心得之三   串口通信

利用 串口调试助手 实现LM3S811PC机之间通信。

一  以下首先是一些关于LM3S811  UART模块的总结


1、
函数:UARTConfigSetExpClk( )
功能:UART配置(要求提供明确的时钟速率)
原型:void UARTConfigSetExpClk(unsigned long ulBase,unsigned long ulUARTClk,
unsigned long ulBaud,unsigned long ulConfig)
参数: ulBase:UART端口的基址,取值UART0_BASE、UART1_BASE或UART2_BASE
ulUARTClk:提供给UART模块的时钟速率,即系统时钟频率
ulBaud:期望设定的波特率
ulConfig:UART端口的数据格式,取下列各组数值之间的“或运算”组合形式:
2、
函数:UARTConfigSet( )
功能:UART配置(自动获取时钟速率)
原型:#define UARTConfigSet(a, b, c) UARTConfigSetExpClk(a, SysCtlClockGet( ), b, c)
参数:详见UARTConfigSetExpClk函数描述
返回:无
说明:本宏函数常常用来代替函数UARTConfigSetExpClk( ),在调用之前应当先调用SysCtlClockSet( )函数设置系统时钟(不要使用误差很大的内部振荡器IOSC、IOSC/4、INT30等)
3、
函数:UARTEnable( )
功能:使能指定UART端口的发送和接收操作
原型:void UARTEnable(unsigned long ulBase)
参数:ulBase:UART端口的基址,取值UART0_BASE、UART1_BASE
4、函数:UARTCharPut( )

功能:发送1个字符到指定的UART端口(等待)

原型:void UARTCharPut(unsigned long ulBase, unsigned char ucData)

参数:ulBase:UART端口的基址,取值UART0_BASE、UART1_BASE或UART2_BASE

        ulData:要发送的字符

返回:无(在未发送完毕前不会返回)

5、函数:UARTCharGet( )

功能:从指定的UART端口接收1个字符(等待)

原型:long UARTCharGet(unsigned long ulBase)

参数:ulBase:UART端口的基址,取值UART0_BASE、UART1_BASE或UART2_BASE

返回:读取到的字符,并自动转换为long型(在未收到字符之前会一直等待)

6、函数:UARTCharPutNonBlocking( )

功能:发送1个字符到指定的UART端口(不等待)

原型:tBoolean UARTCharPutNonBlocking(unsigned long ulBase, unsigned char ucData)

参数:ulBase:UART端口的基址,取值UART0_BASE、UART1_BASE或UART2_BASE

ulData:要发送的字符

返回:如果发送FIFO里有可用空间,则将数据放入发送FIFO,并立即返回true

      如果发送FIFO里没有可用空间,则立即返回false(发送失败)

说明:通常,在调用本函数之前应当先调用UARTSpaceAvail( )确认发送FIFO里有可用空间





二  任务
以下程序主要实现的目的是:首先LM3S811PC发送“请输入密码”,如果在串口调试助手上正确输入程序预先设置的密码(nwx8899),则串口调试助手将再次收到“您是我们的会员,欢迎光临!”的字样;否则将受到:”对不起,密码错误!“


[attach]8 7417[/a ttach]


三 代码




#include "hw_types.h"
#include "hw_memmap.h"
#include "gpio.h"
#include "uart.h"


//防止JTAG失效,上电或者复位时按下板上的USER按键,进去此函数,板上LED一直闪烁。
void JtagWait(void)
{

unsigned long i;

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);


// 使能KEY、LED所在的PC端口


GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_4);

// 设置KEY所在管脚PC4为输入


GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_5 );
// 设置LED所在管脚PC5为输出


if (GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4) == 0x00)
// 若复位或上电时按下KEY,则进入


{




while(1)
//死循环,以等待JTAG连接,LED闪烁



{



for(i=0;i<200000;i++);



GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,GPIO_PIN_5);
//点亮LED




for(i=0;i<200000;i++);



GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,~GPIO_PIN_5);
//熄灭LED



}

}

SysCtlPeripheralDisable(SYSCTL_PERIPH_GPIOC);


// 禁止KEY所在的GPIO端口

}

// Send  strings to the UART0.
void Uart0SendStrings(unsigned char *pStr)
{
    while((*pStr!='\0'))
    {


//Sends the character ucData to the transmit FIFO for the specified port.


//If there is no space available in the transmit FIFO, this


//function will wait until there is space available before return-ing.


UARTCharPut(UART0_BASE, *(pStr++));

//发送字符,若FIFO满,自动等待



//Writes the character ucData to the transmit FIFO for the speci?ed port.


//This function does not block, so if there is no space available,


//then a false is returned, and the application will have to retry the function later.


//UARTCharPutNonBlocking(UART0_BASE, *(pStr++));
//发送字符,若FIFO满,返回false

    }
}

//主函数
int main(void)
{

char cThisChar;

unsigned char arr[20];

unsigned char ans[] = "nwx8899\0";

int i=0;

int DoorFlag = 1;

//int i = 0;

// Set GPIO A0 and A1 as UART pins.
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

// Configure the UART for 115,200, 8-N-1 operation.
    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 9600,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

//发送一串字符

//while(1)

//{

do
    {
        //
        // Read a character using the blocking read function.  This function
        // will not return until a character is available.
        //
        cThisChar = UARTCharGet(UART0_BASE);


arr[i++] = cThisChar;

        //
        // Write the same character using the blocking write function.  This
        // function will not return until there was space in the FIFO and
        // the character is written.
        //
        UARTCharPut(UART0_BASE, cThisChar);

    //
    // Stay in the loop until either a CR or LF is received.
    //
    } while((cThisChar != '\n') && (cThisChar != '\r'));

arr = '\0';

Uart0SendStrings("Result:\n");

DoorFlag = 1;

for(i=0;i<7;++i)

{


if(arr != ans)



DoorFlag = 0;

}

if(DoorFlag == 1)


Uart0SendStrings("您是我们的会员,欢迎光临!\n");

else


Uart0SendStrings("对不起,密码错误!\n");


//UARTCharPut(UART0_BASE, arr);

//Uart0SendStrings(arr);

//Uart0SendStrings(ans);

//}

//GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_5,GPIO_PIN_5);
//点亮LED


//while(1);

}









串口调试助手.zip

352.73 KB

评分
参与人数 1威望 +1 收起 理由
fengzhongfeiwu + 1

相关帖子

沙发
tianm| | 2011-11-29 09:00 | 只看该作者
写得很详细 不错

使用特权

评论回复
板凳
nwx8899|  楼主 | 2011-11-29 13:03 | 只看该作者
3# tianm 感谢斑竹支持

使用特权

评论回复
地板
568581185| | 2011-12-8 22:05 | 只看该作者
学习了
高人!!!

使用特权

评论回复
5
fengzhongfeiwu| | 2011-12-9 11:46 | 只看该作者
试过了,不错

使用特权

评论回复
6
nwx8899|  楼主 | 2011-12-9 14:24 | 只看该作者
5# 568581185 :handshake

使用特权

评论回复
7
nwx8899|  楼主 | 2011-12-9 20:28 | 只看该作者
2# jingshuai

通用异步收发器(UART).pdf

629.69 KB

使用特权

评论回复
8
nwx8899|  楼主 | 2011-12-9 20:37 | 只看该作者
本帖最后由 nwx8899 于 2011-12-9 21:02 编辑

真真的灌水

使用特权

评论回复
9
u880| | 2011-12-11 21:53 | 只看该作者
这个很好,值得顶一顶

使用特权

评论回复
10
nwx8899|  楼主 | 2011-12-12 15:01 | 只看该作者
10# u880 3q

使用特权

评论回复
11
liugp25| | 2011-12-13 14:53 | 只看该作者
不愧为酷帖 总结的很到位

使用特权

评论回复
12
acer4736| | 2011-12-13 16:13 | 只看该作者
这个真好,讲的真不错,谢啦

使用特权

评论回复
13
nwx8899|  楼主 | 2011-12-13 16:34 | 只看该作者
:) :) 12# liugp25

使用特权

评论回复
14
nwx8899|  楼主 | 2011-12-13 20:16 | 只看该作者
3q  :lol 13# acer4736

使用特权

评论回复
15
fengzhongfeiwu| | 2011-12-14 11:48 | 只看该作者
:lol 9# nwx8899

使用特权

评论回复
16
nwx8899|  楼主 | 2011-12-14 14:14 | 只看该作者

使用特权

评论回复
17
xlhtracy| | 2011-12-14 16:19 | 只看该作者
总结的挺好的

使用特权

评论回复
18
nwx8899|  楼主 | 2011-12-14 20:05 | 只看该作者
18# xlhtracy    你的签名

使用特权

评论回复
19
414779421| | 2011-12-19 11:43 | 只看该作者
继续顶楼主

使用特权

评论回复
20
wangjinlili| | 2011-12-19 12:00 | 只看该作者
楼主,你这个系列的我只看到了3和4,别的在哪里呢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名: 宁静致远

61

主题

798

帖子

2

粉丝