TQ2440之串口

 宏定义:

 #define WrUTXH0(ch) (*(volatile unsigned char *)0x50000020) = ch //串口发送缓冲寄存器
 #define RdURXH0()   (*(volatile unsigned char *)0x50000024)      //串口接受缓冲寄存器 
 #define WrUTXH1(ch) (*(volatile unsigned char *)0x50004020) = ch
 #define RdURXH1()   (*(volatile unsigned char *)0x50004024)
 #define WrUTXH2(ch) (*(volatile unsigned char *)0x50008020) = ch
 #define RdURXH2()   (*(volatile unsigned char *)0x50008024)

 串口初始化:

 void Uart_Init(int pclk,int baud)
 {    
   int i;   
    CLKCON |= (1<<10); //开启串口0时钟 
    GPHCON = 0x0000a0;//设置UART0、UART1、UART2端口 
    GPHUP = 0x7fff;//上拉失能 
   UFCON0 = 0x0;   //UART channel 0 FIFO control register, FIFO disable    
    UMCON0 = 0x0;   //UART chaneel 0 MODEM control register, AFC disable    
//UART0    
    ULCON0 = 0x3;  //正常模式、一位停止位、无校验位、正常模式     
    UCON0  = 0x245;   //(接收和发送)中断请求或查询模式、产生接收错误状态中断,禁止接收超时中断、中断模式:接收-边沿;发送-电平;时钟PCLK        
   UBRDIV0 = (int)(PCLK/(16*baud))-1;   //Baud rate divisior register 0    
   for(i=0;i<100;i++);
}

 

 串口选择:

1 void Uart_Select(int ch)//串口选择
2 {    
3   whichUart = ch;
4 }

 串口发送一个字节:

 1 void Uart_SendByte(unsigned char data)//发送一个字节
 2 { 
 3   if(whichUart==0) 
 4   { 
 5     if(data=='\n') 
 6     {   
 7       while(!(UTRSTAT0 & 0x2));   
 8       WrUTXH0('\r');  
 9     } 
10     while(!(UTRSTAT0 & 0x2));   //等待发送缓冲区为空 
11     WrUTXH0(data);
12   } 
13   else if(whichUart==1) 
14   { 
15     if(data=='\n') 
16     {  
17       while(!(UTRSTAT1 & 0x2));   
18       rUTXH1 = '\r';  
19     }  
20     while(!(UTRSTAT1 & 0x2));   //Wait until THR is empty.  
21     rUTXH1 = data;
22   }    
23   else if(whichUart==2) 
24   { 
25     if(data=='\n') 
26     {   
27       while(!(UTRSTAT2 & 0x2));    
28       rUTXH2 = '\r';  
29     }  
30     while(!(UTRSTAT2 & 0x2));   //Wait until THR is empty.   
31     rUTXH2 = data; 
32   }      
33} 

串口发送字符串:

void Uart_SendString(char *pt)
{
    while(*pt)
        Uart_SendByte(*pt++);
}

void uart_send_byte(char data)
{
    while (!(UTRSTAT0 & TXD0READY));
        UTXH0 = data;
}
void uart_send_string(char *string)
{
    while(*string)
    {
        uart_send_byte(*string++);
    }
}

#include <stdarg.h> 
#include <stdio.h> 
void Uart_Printf(char *fmt,...)
{
    va_list ap;
    char string[256];
    
    va_start(ap,fmt);
    vsprintf(string,fmt,ap);
    uart_send_string(string);
    va_end(ap);
}
在操作系统任务中使用此函数输出浮点数时会输出0.0000,是因为任务堆栈对齐问题,堆栈定义时进行对齐处理即可。如下所示:
__align(8) OS_STK Task2Stk [Task2StkLengh];

 主程序:

int main()
{
    SystemClockInit();
    Uart_Init(115200);
    Uart_Select(0);
    Uart_SendString("pppppppppppzzzzzzzzzzzzzzzz\r\n");    
    while(1);
}

 

转载于:https://www.cnblogs.com/zpehome/archive/2013/01/08/2823447.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值