新闻中心

EEPW首页>嵌入式系统>设计应用> UART---查询方式(EasyARM2131)

UART---查询方式(EasyARM2131)

作者: 时间:2016-11-10 来源:网络 收藏
/****************************************Copyright (c)********************

**--------------File Info-------------------------------------------------
** File name:main.c
** Last modified Date: 2011-04-10
** Last Version:1.0
** Descriptions:The main() function example template
**
**-----------------------------------------------------------------------
** Created by:lxliu
** Created date:2011-04-10
** Version:1.0
** Descriptions:The original version
**
**-----------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
************************************************************************/
#include "config.h"

本文引用地址://m.amcfsurvey.com/article/201611/317409.htm

/***********************************************************************
** 函数名称:DelayNS()
** 函数功能:长软件延时
** 入口参数:dly延时控制值,值越大,延时越长
** 出口参数:无
***********************************************************************/

void DelayNS(uint32 dly)
{
uint32 i;
for(;dly>0;dly--)
for(i=0;i<50000;i++);
}

#defineUART_BPS 115200 //串口通信波特率
/***********************************************************************
** 函数名称:UART0_Init()
** 函数功能:串口初始化,设置为8位数据位,1位停止位,
** 无奇偶校验,波特率为115200
** 入口参数:无
** 出口参数:无
***********************************************************************/
void UART0_Init()
{
uint16 Fdiv;

U0LCR = 0x83;
Fdiv = (Fpclk/16)/UART_BPS;
U0DLM = Fdiv/256;
U0DLL = Fdiv%256;
U0LCR = 0x03;
}


/***********************************************************************
** 函数名称:UART0_GetByte()
** 函数功能:从串口接收一个字节数据,使用查询方式接收
** 入口参数:无
** 出口参数:接收到的数据
***********************************************************************/

uint8 UART0_GetByte()
{
uint8 rcv_dat;

while((U0LSR & 0x01) == 0);
rcv_dat = U0RBR;

return(rcv_dat);
}

/***********************************************************************
** 函数名称:UART0_GetStr()
** 函数功能:串口接收
** 入口参数:s 指向接收数据数组的指针
** n 接收的个数
** 出口参数:无
***********************************************************************/
void UART0_GetStr(uint8 *s, uint32 n)
{
for (;n>0;n--)
{
*s++ = UART0_GetByte();
}

}

/***********************************************************************
** 函数名称:UART0_SendByte()
** 函数功能:向串口发送字节数据,并等待发送完毕,使用查询方式
** 入口参数:dat 要发送的数据
** 出口参数:无
***********************************************************************/
void UART0_SendByte(uint8 dat)
{
U0THR = dat;
while((U0LSR & 0x40) == 0);

}

/***********************************************************************
** 函数名称:UART0_SendStr()
** 函数功能:向串口发送一字符串
** 入口参数:str 要发送的字符串的指针
** 出口参数:无
***********************************************************************/
void UART0_SendStr(uint8 const *str)
{
while(1)
{
if(*str == )
break;
UART0_SendByte(*str++);
}
}

/***********************************************************************
** 函数名称:main()
** 函数功能:从串口UART0接收字符串"HelloEasyARM2131!",并发送回上位机显示
** 调试说明:需要PC串口显示终端软件,如EasyARM.exe
***********************************************************************/
int main (void)
{
uint8 snd[32];

PINSEL0 = 0x00000005;
UART0_Init();
UART0_GetStr(snd,18);
DelayNS(10);
UART0_SendStr(snd);
DelayNS(10);

while(1);
return 0;
}
/**********************************************************************
** End Of File
**********************************************************************/



评论


技术专区

关闭