-
UART初始化函数
void Uart_Init(int pclk,int baud) { int i; rGPHCON|=0xa0; //GPH2,GPH3 as TXD0,RXD0 rGPHUP = 0x0; //GPH2,GPH3内部上拉 if(pclk == 0) pclk = PCLK; rUFCON0 = 0x0; //禁止3个通道的FIFO控制寄存器 rUFCON1 = 0x0; rUFCON2 = 0x0; rUMCON0 = 0x0; rUMCON1 = 0x0; rUMCON2 = 0x0; //初始化3个通道的MODEM控制寄存器,禁止AFC //Line control register 0: Normal,No parity,1 stop,8 bits. rULCON0=0x3; // Control register 0 rUCON0 = 0x5; //Baud rate divisior register 0 rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 ); }
-
接收1字节数据
//接收一字节数据 char Uart_Getch(void) { while(!(rUTRSTAT0 & 0x1)); //等待就绪 //读错误状态寄存器UERSTATn if ((rUERSTAT0 & 0x1)|| (rUERSTAT0 & 0x4)) return -1; return RdURXH0; //0x50000027 }
-
接收1个字符串
//接收一个字符串 void Uart_GetString(char *string) { char *string2 = string; char c; while((c = Uart_Getch())!=‘\r’) //回车符 *string++ = c; }
-
接收1个字符串,转换为数字
int Uart_GetIntNum(void) { char str[30]; char *string = str; int base = 10; //进制 int minus = 0; //是否负数 int result = 0; //转换的结果 int lastIndex; //字符串长度 int i; Uart_GetString(string); if(string[0]=='-') { minus = 1; string++; } if(string[0]=='0' && (string[1]=='x' || string[1]=='X')) { base = 16; string += 2; } lastIndex = strlen(string) - 1; if(lastIndex<0) return -1; if(string[lastIndex]=='h' || string[lastIndex]=='H' ) { base = 16; string[lastIndex] = 0; lastIndex--; } if(base==10) { result = atoi(string); result = minus ? (-1*result):result; } else { for(i=0;i<=lastIndex;i++) { if(isalpha(string[i])) { if(isupper(string[i])) result = (result<<4) + string[i] - ‘A’ + 10;//之前的结果乘16,再加当前数字 else result = (result<<4) + string[i] - 'a' + 10; } else { result = (result<<4) + string[i] - '0'; } result = minus ? (-1*result):result; } return result; }
-
发送一字节
//发送一字节 //#define WrUTXH0(ch) ( * (volatile unsigned char * )0x50000023)=(unsigned char)(ch) void Uart_SendByte(int data) { while(!(rUTRSTAT0 & 0x4)); Delay(10); //because the slow response of hyper_terminal WrUTXH0(data); }
-
送一个字符串
//发送一个字符串 void Uart_SendString(char *pt) { while(*pt) Uart_SendByte(*pt++); }
-
2410addr.h 定义UART各个寄存器的头文件
//2410addr.h 定义UART各个寄存器的头文件 #define rULCON0 ( * (volatile unsigned * )0x50000000) //UART 0 Line control #define rUCON0 ( * (volatile unsigned * )0x50000004) //UART 0 control #define rUFCON0 ( * (volatile unsigned * )0x50000008) //UART 0 FIFO control #define rUMCON0 ( * (volatile unsigned * )0x5000000c) //UART 0 Modem control #define rUTRSTAT0 ( * (volatile unsigned * )0x50000010) //UART 0 Tx/Rx status #define rUERSTAT0 ( * (volatile unsigned * )0x50000014) //UART 0 Rx error status #define rUFSTAT0 ( * (volatile unsigned * )0x50000018) //UART 0 FIFO status #define rUMSTAT0 ( * (volatile unsigned * )0x5000001c) //UART 0 Modem status #define rUBRDIV0 ( * (volatile unsigned * )0x50000028) //UART 0 Baud rate diviaor #define rULCON1 ( * (volatile unsigned * )0x50004000) //UART 1 Line control #define rUCON1 ( * (volatile unsigned * )0x50004004) //UART 1 Control #define rUFCON1 ( * (volatile unsigned * )0x50004008) //UART 1 FIFO control #define rUMCON1 ( * (volatile unsigned * )0x5000400c) //UART 1 Modem control #define rUTRSTAT1 ( * (volatile unsigned * )0x50004010) //UART 1 Tx/Rx status #define rUERSTAT1 ( * (volatile unsigned * )0x50004014) //UART 1 Rx error status #define rUFSTAT1 ( * (volatile unsigned * )0x50004018) //UART 1 FIFO status #define rUMSTAT1 ( * (volatile unsigned * )0x5000401c) //UART 1 Modem status #define rUBRDIV1 ( * (volatile unsigned * )0x50004028) //UART 1 Baud rate divisor #define rULCON2 ( * (volatile unsigned * )0x50008000) //UART 2 Line control #define rUCON2 ( * (volatile unsigned * )0x50008004) //UART 2 Control #define rUFCON2 ( * (volatile unsigned * )0x50008008) //UART 2 FIFO control #define rUMCON2 ( * (volatile unsigned * )0x5000800c) //UART 2 Modem control #define rUTRSTAT2 ( * (volatile unsigned * )0x50008010) //UART 2 Tx/Rx status #define rUERSTAT2 ( * (volatile unsigned * )0x50008014) //UART 2 Rx error status #define rUFSTAT2 ( * (volatile unsigned * )0x50008018) //UART 2 FIFO status #define rUMSTAT2 ( * (volatile unsigned * )0x5000801c) //UART 2 Modem status #define rUBRDIV2 ( * (volatile unsigned * )0x50008028) //UART 2 Baud rate divisor #if def_BIG_ENDIAN #define rUTXH0 ( * (volatile unsigned char * )0x50000023) //UART 0 Transmission Hold #define rURXH0 ( * (volatile unsigned char * )0x50000027) //UART 0 Receive buffer #define rUTXH1 ( * (volatile unsigned char * )0x50004023) //UART 1 Transmission Hold #define rURXH1 ( * (volatile unsigned char * )0x50004027) //UART 1 Receive buffer #define rUTXH2 ( * (volatile unsigned char * )0x50008023) //UART 2 Transmission Hold #define rURXH2 ( * (volatile unsigned char * )0x50008027) //UART 2 Receive buffer #define WrUTXH0(ch) ( * (volatile unsigned char * )0x50000023)=(unsigned char)(ch) #define RdURXH0 ( * (volatile unsigned char * )0x50000027) #define WrUTXH1(ch) ( * (volatile unsigned char * )0x50004023)=(unsigned char)(ch) #define RdURXH1() ( * (volatile unsigned char * )0x50004027) #define WrUTXH20(ch) ( * (volatile unsigned char * )0x50008023)=(unsigned char)(ch) #define RdURXH2() ( * (volatile unsigned char * )0x50008027) #define UTXH0 (0x50000020+3) //Byte_access address by DMA #define URXH0 (0x50000024+3) #define UTXH1 (0x50004020+3) #define URXH1 (0x50004024+3) #define UTXH2 (0x50008020+3 ) #define URXH2 (0x50008024+3) #else//Little Endian #define rUTXH0 ( * (volatile unsigned char * )0x50000020)//UART 0 Transmission Hold #define rURXH0 ( * (volatile unsigned char * )0x50000024)//UART 0 Receive buffer #define rUTXH1 ( * (volatile unsigned char * )0x50004020)//UART 1 Transmission Hold #define rURXH1 ( * (volatile unsigned char * )0x50004024)//UART 1 Receive buffer #define rUTXH2 ( * (volatile unsigned char * )0x50000820)//UART 2 Transmission Hold #define rURXH2 ( * (volatile unsigned char * )0x50008024)//UART 2 Receive buffer #define WrUTXH0(ch) ( * (volatile unsigned char * )0x50000020)=(unsigned char)(ch) #define RdURXH0 ( * (volatile unsigned char * )0x50000024) #define WrUTXH1(ch) ( * (volatile unsigned char * )0x50004020)=(unsigned char)(ch) #define RdURXH1() ( * (volatile unsigned char * )0x50004024) #define WrUTXH2(ch) ( * (volatile unsigned char * )0x50008020)=(unsigned char)(ch) #define RdURXH2() ( * (volatile unsigned char * )0x50008024) #define UTXH0 (0x50000020+3) //Byte_access address by DMA #define URXH0 (0x50000024+3) #define UTXH1 (0x50004020+3) #define URXH1 (0x50004024+3) #define UTXH2 (0x50008020+3 ) #define URXH2 (0x50008024+3) #endif
S3C2440——使用URAT0查询方式发送和接收字符串
最新推荐文章于 2023-04-24 20:12:08 发布