#include "2440addr.h" #include <stdarg.h> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <ctype.h> #define TXD0READY (1<<2) #define RXD0READY (1) #define UART_CLK 50000000 // UART0的时钟源设为PCLK #define UART_BAUD_RATE 115200 // 波特率 #define UART_BRD ((UART_CLK / (UART_BAUD_RATE * 16)) - 1) /* * 初始化UART0 * 115200,8N1,无流控 */ void Uart0_Init(void) { rGPHCON |= 0xa0; // GPH2,GPH3用作TXD0,RXD0 rGPHUP = 0x0c; // GPH2,GPH3内部上拉 rULCON0 = 0x03; // 8N1(8个数据位,无较验,1个停止位) rUCON0 = 0x05; // 查询方式,UART时钟源为PCLK rUFCON0 = 0x00; // 不使用FIFO rUMCON0 = 0x00; // 不使用流控 rUBRDIV0 = UART_BRD; // 波特率为115200 } /* * 发送一个字符 */ void Send_Byte(unsigned char c) { /* 等待,直到发送缓冲区中的数据已经全部发送出去 */ while (!(rUTRSTAT0 & TXD0READY)); /* 向UTXH0寄存器中写入数据,UART即自动将它发送出去 */ rUTXH0 = c; } /* * 接收字符 */ unsigned char Get_Byte(void) { /* 等待,直到接收缓冲区中的有数据 */ while (!(rUTRSTAT0 & RXD0READY)); /* 直接读取URXH0寄存器,即可获得接收到的数据 */ return rURXH0; } /* * 判断一个字符是否数字 */ int isDigit(unsigned char c) { if (c >= '0' && c <= '9') return 1; else return 0; } /* * 判断一个字符是否英文字母 */ int isLetter(unsigned char c) { if (c >= 'a' && c <= 'z') return 1; else if (c >= 'A' && c <= 'Z') return 1; else return 0; } void Uart0_SendString(char *pt) { while(*pt) { Send_Byte(*pt++); } } void Uart_Printf(char *fmt,...) { va_list ap; char string[256]; va_start(ap,fmt); vsprintf(string,fmt,ap); Uart0_SendString(string); va_end(ap); }