📄 uart.c
字号:
/****************************************Copyright (c)***************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info--------------------------------------------------------------------------------
** File name: main.c
** Last modified Date: 2004-09-16
** Last Version: 1.0
** Descriptions: The main() function example template
**
**-------------------------------------------------------------------------------------------------------
** Created by: Chen Mingji
** Created date: 2004-09-16
** Version: 1.0
** Descriptions: The original version
**
**-------------------------------------------------------------------------------------------------------
** Modified by: Li Baihua
** Modified date: 2008-04-02
** Version: 1.1
** Descriptions: 串口0通信-查询方式实验
**
*********************************************************************************************************/
#include "config.h"
#include "string.h"
# define UART_BPS 115200 /* 串口通信波特率 */
/*********************************************************************************************************
** Function name: UARTInit
** Descriptions: 串口初始化,设置为8位数据位,1位停止位,无奇偶校验,波特率为9600
** input parameters: uiDly 值越大,延时时间越长
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void UARTInit (void)
{
uint16 uiFdiv;
PINSEL0 = PINSEL0 & (~0x0F);
PINSEL0 = PINSEL0 | 0x05; /* 设置I/O连接到UART */
U0LCR = 0x83; /* 允许设置波特率 */
uiFdiv = (11059200*5 / 16) / UART_BPS; /* 设置波特率*/
U0DLM = uiFdiv / 256;
U0DLL = uiFdiv % 256;
U0LCR = 0x03; /* 锁定波特率 */
}
/*********************************************************************************************************
** Function name: UART0GetByte
** Descriptions: 从串口接收1字节数据,使用查询方式接收
** input parameters: 无
** output parameters: 无
** Returned value: uiRcvData 接收到的数据
*********************************************************************************************************/
uint8 UART0GetByte (void)
{
uint8 uiRcvData;
while ((U0LSR & 0x01) == 0); /* 等待接收标志置位 */
uiRcvData = U0RBR; /* 读取数据 */
return (uiRcvData);
}
/*********************************************************************************************************
** Function name: UART0GetStr
** Descriptions: 串口接收字符串
** input parameters: uiStr 指向接收数据数组的指针
** uiNum 接收数据的个数
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void UART0GetStr(uint8 *uiStr, uint32 uiNum)
{
for (; uiNum > 0; uiNum--){
*uiStr++ = UART0GetByte ();
}
}
/*********************************************************************************************************
** Function name: UART0SendByte
** Descriptions: 向串口发送子节数据,并等待数据发送完成,使用查询方式
** input parameters: uiDat 要发送的数据
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void UART0SendByte (uint8 uiDat)
{
U0THR = uiDat; /* 写入数据 */
while ((U0LSR & 0x40) == 0); /* 等待数据发送完毕 */
}
/*********************************************************************************************************
** Function name: UART0SendStr
** Descriptions: 向串口发送字符串
** input parameters: uiStr 要发送的字符串指针
** output parameters: 无
** Returned value: 无
*********************************************************************************************************/
void UART0SendStr(uint8 const *uiStr)
{
while (1){
if (*uiStr == '\0')break; /* 遇到结束符,退出 */
UART0SendByte (*uiStr++);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -