/****************************************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: T he main() function example template ** **------------------------------------------------------------------------------------------------------ ** Created by: Chenmingji ** Created date: 2004-09-16 ** Version: 1.0 ** Descriptions: The original version ** **------------------------------------------------------------------------------------------------------ ** Modified by: Chenxibing ** Modified date: 2005-01-17 ** Version: ** Descriptions: UART0通讯实验,查询方式。 ** ********************************************************************************************************/ #include "config.h" /* ********************************************************************************************************* ** 函数名称 :DelayNS() ** 函数功能 :长软件延时。 ** 入口参数 :dly 延时参数,值越大,延时越久 ** 出口参数 :无 ********************************************************************************************************* */ void DelayNS (uint32 dly) { uint32 i; for ( ; dly>0; dly--) for (i=0; i<50000; i++); } #define UART_BPS 115200 // 串口通讯波特率 /* ********************************************************************************************************* ** 函数名称 :UART0_Init() ** 函数功能 :串口初始化,设置为8位数据位,1位停止位,无奇偶校验,波特率115200。 ** 入口参数 :无 ** 出口参数 :无 ********************************************************************************************************* */ void UART0_Init (void) { uint16 Fdiv; U0LCR = 0x83; // DLAB=1,允许设置波特率 Fdiv = (Fpclk / 16) / UART_BPS; // 设置波特率 U0DLM = Fdiv / 256; U0DLL = Fdiv % 256; U0LCR = 0x03; } /* ********************************************************************************************************* ** 函数名称 :UART0_GetByte() ** 函数功能 :从串口接收1字节数据,使用查询方式接收。 ** 入口参数 :无 ** 出口参数 :接收到的数据 ********************************************************************************************************* */ uint8 UART0_GetByte (void) { 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 == '\0') break; // 遇到结束符,退出 UART0_SendByte(*str++); // 发送数据 } } /* ********************************************************************************************************* ** 函数名称 :main() ** 函数功能 :从串口UART0接收字符串"Hello EasyARM2131!",并发送回上位机显示。 ** 调试说明 :需要PC串口显示终端软件如EasyARM.exe。 ********************************************************************************************************* */ int main (void) { uint8 snd[32]; PINSEL0 = 0x00000005; // 设置I/O连接到UART0 UART0_Init(); // 串口初始化 UART0_GetStr(snd,18); // 从串口接收字符串 DelayNS(10); UART0_SendStr(snd); // 向串口发送字符串 DelayNS(10); while (1); return 0; } /********************************************************************************************************* ** End Of File ********************************************************************************************************/