ARM裸机程序开发21(2440串口:申嵌源码2440lib.c文件中关于UART的操作)



注意:使用前将CPU的引脚设为复用功能引脚。
1.初始化串口
设置波特率,FIFO,流控制寄存器、串口的数据格式和串口的控制寄存器
void Uart_Init(int pclk,int baud) 
{
int i;
if(pclk == 0)
pclk = PCLK; //使用系统的PCLK
rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable
rUFCON1 = 0x0; //UART channel 1 FIFO control register, FIFO disable
rUFCON2 = 0x0; //UART channel 2 FIFO control register, FIFO disable
rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable
rUMCON1 = 0x0; //UART chaneel 1 MODEM control register, AFC disable
//UART0
rULCON0 = 0x3; //Line control register : Normal,No parity,1 stop,8 bits
// [10] [9] [8] [7] [6] [5] [4] [3:2] [1:0]
// Clock Sel, Tx Int, Rx Int, Rx Time Out, Rx err, Loop-back, Send break, Transmit Mode, Receive Mode
// 0 1 0 , 0 1 0 0 , 01 01
// PCLK Level Pulse Disable Generate Normal Normal Interrupt or Polling
rUCON0 = 0x245; // Control register
rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 ); //Baud rate divisior register 0
//UART1
rULCON1 = 0x3;
rUCON1 = 0x245;
rUBRDIV1=( (int)(pclk/16./baud+0.5) -1 );
//UART2
rULCON2 = 0x3;
rUCON2 = 0x245;
rUBRDIV2=( (int)(pclk/16./baud+0.5) -1 ); 


for(i=0;i<100;i++); //稍做延时处理,保证已经正确执行
}
2.确定使用的那一个串口
通过赋值一个全局变量,之后程序通过判断全局变量的值进行选择对相应串口的操作
void Uart_Select(int ch)
{
whichUart = ch;
}
3.发送是否结束的函数,如果发送未结束也就是tx的移位寄存器中还有数据,就会一直循环等待,直至数据从发送移位寄存器中发送完成
void Uart_TxEmpty(int ch)
{
if(ch==0)
while(!(rUTRSTAT0 & 0x4)); //Wait until tx shifter is empty.


else if(ch==1)
while(!(rUTRSTAT1 & 0x4)); //Wait until tx shifter is empty.


else if(ch==2)
while(!(rUTRSTAT2 & 0x4)); //Wait until tx shifter is empty.
}
4.状态寄存器中的接受状态寄存器,若准备OK,则将buffer中的数据返回
char Uart_Getch(void)
{
if(whichUart==0)

while(!(rUTRSTAT0 & 0x1)); //Receive data ready
return RdURXH0();
}
else if(whichUart==1)

while(!(rUTRSTAT1 & 0x1)); //Receive data ready
return RdURXH1();
}
else if(whichUart==2)
{
while(!(rUTRSTAT2 & 0x1)); //Receive data ready
return RdURXH2();
}
return 0;
}


5.检测接收情况,如若接受成功将接受buffer中的数据返回,否则返回0
char Uart_GetKey(void)
{
if(whichUart==0)

if(rUTRSTAT0 & 0x1) //Receive data ready
return RdURXH0();
else
return 0;
}
else if(whichUart==1)
{
if(rUTRSTAT1 & 0x1) //Receive data ready
return RdURXH1();
else
return 0;
}
else if(whichUart==2)

if(rUTRSTAT2 & 0x1) //Receive data ready
return RdURXH2();
else
return 0;

return 0; 
}
6.返回值为string指向的字符串,通过传送地址进行处理。利用Getch函数接受每一个字符比针对特殊字符进行处理。在接受的同时还要在发送相应的字符用来在串口工具中进行回显
void Uart_GetString(char *string)
{
char *string2 = string;
char c;
while((c = Uart_Getch())!='\r')
{
if(c=='\b')
{
if( (int)string2 < (int)string )
{
Uart_Printf("\b \b");
string--;
}
}
else 
{
*string++ = c;
Uart_SendByte(c);
}
}
*string='\0';
Uart_SendByte('\n');
}
7.串口接受数字信息,分为10进制和16进制,并针对相应的数字进行处理。
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;
else
result = (result<<4) + string[i] - 'a' + 10;
}
else
result = (result<<4) + string[i] - '0';
}
result = minus ? (-1*result):result;
}
return result;
}
8.串口发送字节数据
void Uart_SendByte(int data)
{
if(whichUart==0)
{
if(data=='\n')
{
while(!(rUTRSTAT0 & 0x2));
Delay(10); //because the slow response of hyper_terminal 
WrUTXH0('\r');
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
Delay(10);
WrUTXH0(data);
}
else if(whichUart==1)
{
if(data=='\n')
{
while(!(rUTRSTAT1 & 0x2));
Delay(10); //because the slow response of hyper_terminal 
rUTXH1 = '\r';
}
while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty.
Delay(10);
rUTXH1 = data;

else if(whichUart==2)
{
if(data=='\n')
{
while(!(rUTRSTAT2 & 0x2));
Delay(10); //because the slow response of hyper_terminal 
rUTXH2 = '\r';
}
while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty.
Delay(10);
rUTXH2 = data;


9.串口发送字符串,通过字节数据的拼接完成字符串的发送
void Uart_SendString(char *pt)
{
while(*pt)
Uart_SendByte(*pt++);
}
10.格式化输出打印指令,和C语言的printf函数类似
void Uart_Printf(char *fmt,...)
{
va_list ap;
char string[256];




va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值