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);
}

 

posted @ 2013-01-08 00:01  zpehome  阅读(337)  评论(0编辑  收藏  举报