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中断方式发送和接收字符串
下一篇:JZ2440挂载nfs
推荐阅读最新更新时间:2024-11-02 11:35
设计资源 培训 开发板 精华推荐
- 使用 Cypress Semiconductor 的 S6AP413A9BGN1C000 的参考设计
- 涂鸦训练营万能遥控
- EVAL-AD7147EBZ,SPI 接口评估板允许用户评估用于单电极电容传感器的 AD7147 CapTouch 可编程控制器的特性
- MIC2549A-2YN 可编程限流高端开关的典型应用
- AKD5388A-A,用于 AK5388A 24 位、4 通道 Delta-Sigma ADC 的评估板
- 物联网关
- 热敏打印机V3.0
- LT6654AHLS8-5 电压基准的典型应用,具有升压输出电流和电流限制
- LT1317IS8 2V 至 3.3V 转换器的典型应用电路
- Si4122GM-EVB,Si4122G-BM PLL 频率合成器评估板
- 限时秒杀,100本专业书籍——TI 中国大学计划20周年
- 手机答题抽奖啦:Microchip高端千兆以太网专题知识
- 是德科技有奖直播:元宇宙测试系列研讨会之VR/AR 数字接口测试的挑战
- 【直播】恩智浦Thread/BLE双模技术讲座
- 有奖直播报名中|基于英飞凌AIROC™ CYW20829低功耗蓝牙芯片的无线组网解决方案
- 晒方案 赢大奖
- 任选下载有礼|《新概念模拟电路》全五册合集/《ADI 参考电路合集》
- 降碳行动:寻找身边8位单片机,抢楼赢ST最新M0开发板!
- Microchip 有奖直播|满足汽车电池管理系统 (BMS) 不断发展的安全标准
- 报名Keysight感恩月,天天抽示波器、直流电源、万用表……