LPC1768之UART0查询方式收发数据
来源:互联网 发布:大公教育知满天 编辑:程序博客网 时间:2024/05/18 02:15
*********************************************************************************************************/#include "LPC17xx.h" /* LPC17xx外设寄存器 *//******************************************************************************************************** 变量与宏定义*********************************************************************************************************/extern uint32_t SystemFrequency; /* Clock Variable 时钟变量 */#define UART_BPS 9600 /* 串口通信波特率 */ /*********************************************************************************************************** Function name: delayNS** Descriptions: 延时函数** input parameters: ulDly: 延时值** output parameters: 无** Returned value: 无*********************************************************************************************************/void delayNS (uint32_t ulDly){ uint32_t i; for (; ulDly > 0; ulDly--) { for (i = 0; i < 5000; i++); }}/*********************************************************************************************************** Function name: uartInit** Descriptions: 串口初始化,设置为8位数据位,1位停止位,无奇偶校验,波特率为9600** input parameters: 无** output parameters: 无** Returned value: 无*********************************************************************************************************/void uart0Init (void){ uint16_t usFdiv; LPC_PINCON->PINSEL0 |= (0x01 << 4)|(0x01 << 6); /* 设置P0.2(TXD0)为串口,P0.3(RXD0)为串口 */ LPC_SC->PCONP = LPC_SC->PCONP|0x08; /* 打开串口0功能 */ LPC_UART0->LCR = 0x83; /* 设置串口数据格式,8位字符长度,1个停止位,无校验, 使能访问除数锁存器 ,设定波特率 */ usFdiv = (SystemFrequency/4 / 16) / UART_BPS; /* 设置波特率 */ LPC_UART0->DLM = usFdiv / 256; /*除数高8位,没有小数点情况 */ LPC_UART0->DLL = usFdiv % 256; /*除数低8位,没有小数点情况 */ LPC_UART0->LCR = 0x03; /* 锁定波特率 */ LPC_UART0->FCR = 0x06; /* 复位TxFIFO和RXFIFIO */}/*********************************************************************************************************** Function name: uart0GetByte** Descriptions: 从串口接收1字节数据,使用查询方式接收** input parameters: 无** output parameters: 无** Returned value: ucRcvData: 接收到的数据*********************************************************************************************************/uint8_t uart0GetByte (void) //查询法{ uint8_t ucRcvData; while ((LPC_UART0->LSR & 0x01) == 0); //UnLSR[0] 该位是否置一,决定能否从FIFO中读取数据 //UnLSR[0]为0 :RBR为空 ; UnLSR[0]为1:RBR中包含有效数据,从FIFO中读走所有数据后,恢复为0 ucRcvData = LPC_UART0->RBR; //UnRBR为接收缓存寄存器 /* 读取数据 */ return (ucRcvData); }/*********************************************************************************************************** Function name: uart0GetStr** Descriptions: 串口接收字符串** input parameters: puiStr: 指向接收数据数组的指针** ulNum: 接收数据的个数** output parameters: 无** Returned value: 无*********************************************************************************************************/void uart0GetStr (uint8_t *puiStr, uint32_t ulNum){ for (; ulNum > 0; ulNum--) { *puiStr++ = uart0GetByte (); } }/*********************************************************************************************************** Function name: uart0SendByte** Descriptions: 向串口发送子节数据,并等待数据发送完成,使用查询方式** input parameters: ucDat: 要发送的数据** output parameters: 无** Returned value: 无*********************************************************************************************************/void uart0SendByte (uint8_t ucDat){ LPC_UART0->THR = ucDat; //UnTHR为发送缓存寄存器 /* 写入数据 * / while ((LPC_UART0->LSR & 0x40) == 0); // UnLSR[6] 为0 时,表明UnTHR包含数据; } // UnLSR[6]为1时,则UnTHR已空 /* 等待数据发送完毕*//*********************************************************************************************************** Function name: uart0SendStr** Descriptions: 向串口发送字符串** input parameters: puiStr: 要发送的字符 串指针** output parameters: 无** Returned value: 无*********************************************************************************************************/void uart0SendStr (uint8_t const *puiStr, uint32_t len){ while (len -- > 0) { uart0SendByte (*puiStr++); }}/*********************************************************************************************************** Function name: main** Descriptions: 串口查询方式接收发送数据。** 全速运行程序用串口调试软件发送字符串,并在接收窗口查看返回数据,波特率为9600,** 上位机一次需要发送20个字符。** input parameters: 无** output parameters: 无** Returned value: 无*********************************************************************************************************/int main (void){ uint8_t ucBuf[30]; SystemInit(); /* 初始化目标板 */ uart0Init (); /* 串口初始化 */ while (1){ uart0GetStr(ucBuf, 20); /* 从串口接收字符串 */ delayNS(1); uart0SendStr(ucBuf, 20); /* 向串口发送字符串 */ delayNS(1); }}/********************************************************************************************************* End Of File*********************************************************************************************************/
0 0
- LPC1768之UART0查询方式收发数据
- LPC1768之串口UART0
- LPC1768之串口UART0
- LPC1768之串口UART0
- LPC1768 UART0
- UART0串口编程系列 串口编程(UART0)之中断方式(一)
- UART0串口编程系列 串口编程(UART0)之中断方式(二)
- UART0串口编程系列(一)之轮训方式
- UART0串口编程系列(二)之中断方式
- S3C2440之UART0
- LPC1768之外部中断
- LPC1768之定时器TIMER0
- winsocket 之收发数据出问题
- lwip之数据收发流程
- lwip之数据收发流程
- STM32F407ZET6 USART DMA方式收发数据
- lpc1768
- S3C2440裸奔之uart0使用
- 【POJ】3155 Hard Life 最大密度子图
- How many prime numbers(水题)
- c语言基础知识总结
- javascript学习(三)——常用方法(2)
- QT 下使用jrtplib
- LPC1768之UART0查询方式收发数据
- HDU 1963 Investment (完全背包)
- Frame Relay(帧中继)的理解
- 如何使得CTreeCtrl 失去焦点后,选中项的颜色仍然保持不变,怎么实现?高亮
- leetcode总结无止境系列之链表
- Dual Palindromes 双重回文数
- 通过jQuery源码学习javascript(三)
- MacTex写中文
- 复习图---Delay Constrained Maximum Capacity Path(SPFA+二分)