1 /* 2 此文件主要学习如何配置UART0,怎样单字符输入和输出, 怎样进行字符串的输入和输出; 3 */ 4 #include <stdarg.h> 5 #include "def.h" 6 7 #include "system.h" 8 #include "sysc.h" 9 #include "uart.h" 10 11 #define Inp32(ADDR) *((volatile u32 *)ADDR) 12 #define Outp32(ADDR,data) *((volatile u32 *)ADDR)=data 13 #define UART0 ((volatile UART_REGS*)0X7F005000) 14 15 void Uart_Port_Init(int channal); 16 void Uart_Init(u32 baud, int channal); 17 void Uart_Putc(char ch); 18 void Delay(int time); 19 void Uart_Puts(char *str); 20 void Uart_Printf(char *fmt,...); 21 char* Uart_Gets(char *); 22 char Uart_Getc(void); 23 char Uart_Getch(void); 24 char str[256]; 25 26 void main(void) 27 { 28 SYSC_GetClkInform(); 29 Uart_Init(115200, 0); 30 31 Uart_Puts("注意:按 ECS再按回车键 退出测试!! \n"); 32 Uart_Printf("首先输出字符串:The world is currut be 5\n"); 33 Uart_Printf("The world is currut be %d \n",5); 34 35 while(str[0] != 0x1b) 36 { 37 Uart_Gets(str); 38 Uart_Printf("你输入的是: %s \n",str); 39 } 40 41 Uart_Puts("你已经推出了程序,别做无谓针扎了....\n"); 42 while(1); 43 } 44 45 //延时函数; 46 void Delay(int time) 47 { 48 int i; 49 for(;time > 0; time--) 50 for(i=0;i < 3000; i++); 51 } 52 53 //端口初始化; 54 void Uart_Port_Init(int channal) 55 { 56 if(channal == 0) 57 { 58 u32 uConValue; 59 #define rGPACON 0X7F008000 60 uConValue = Inp32(rGPACON); 61 uConValue = (uConValue & ~0xff) | 0x22; //配置为UART0功能; 62 Outp32(rGPACON,uConValue); 63 64 #define rGPAPUD 0X7F008008 65 uConValue = Inp32(rGPAPUD); 66 uConValue &= ~0xf; //低4位配置为0000,上下拉电阻除能; 67 Outp32(rGPAPUD,uConValue); 68 69 } 70 } 71 72 //uart初始化; 73 void Uart_Init(u32 baud, int channal) 74 { 75 u32 pclk; 76 u32 uConValue; 77 78 //if(pclk == 0) 79 pclk = g_PCLK; 80 81 Uart_Port_Init(channal); 82 //配置UART0寄存器; 83 if(channal == 0) 84 { 85 UART0->rUlCon = 0x3;//配置为8位数据,1位停止位,0位校验位; 86 87 UART0->rUCon = 0x805;//配置为中断或轮询模式,使用pclk作为波特率; 88 89 UART0->rUfCon = 0x0;//配置为非FIFO模式; 90 91 UART0->rUmCon = 0x0;//配置为非中断,非流控模式; 92 93 uConValue = (int)(pclk/16./baud) - 1; 94 UART0->rUbrDiv = uConValue;//配置波特率; 95 96 Delay(100); 97 } 98 } 99 100 //单字符输出; 101 void Uart_Putc(char ch) 102 { 103 if(ch == '\n') 104 { 105 while(!((UART0->rUtrStat) & 0x2)); 106 UART0->rUtxh = '\r'; 107 } 108 while(!((UART0->rUtrStat) & 0x2)); 109 UART0->rUtxh = ch; 110 } 111 112 //字符串输出 113 void Uart_Puts(char *str) 114 { 115 while(*str) 116 { 117 Uart_Putc(*str++); 118 } 119 } 120 121 //格式化输出; 122 void Uart_Printf(char *fmt,...) 123 { 124 char str[256]; 125 va_list ap; 126 127 va_start(ap,fmt); //va_start() 与 va_end() 需要对称 128 vsprintf(str,fmt,ap); 129 Uart_Puts(str); 130 va_end(ap); 131 } 132 133 //单字符阻塞型输入; 134 char Uart_Getc(void) 135 { 136 while(!((UART0->rUtrStat) & 0x1)); 137 return (UART0->rUrxh); 138 } 139 140 //无阻塞型字符输入; 141 char Uart_Getch(void) 142 { 143 //if((UART0->rUtrStat) & 0x1) 144 return(UART0->rUrxh); 145 } 146 147 //字符寸输入; 148 char* Uart_Gets(char *str) 149 { 150 char *ptr = str; 151 char c; 152 153 while((c = Uart_Getc()) != '\r') 154 { 155 if(c == '\b') 156 { 157 if(ptr < str) 158 { 159 Uart_Putc('\b'); 160 Uart_Putc(' '); 161 Uart_Putc('\b'); 162 str--; 163 } 164 } 165 else 166 { 167 *str++ = c; 168 Uart_Putc(c); 169 } 170 } 171 Uart_Putc(c); 172 Uart_Putc('\n'); 173 *str = '\0'; 174 175 return (ptr); 176 }