⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tl16c752.txt

📁 TL16C752 串口源程序 串口源程序 串口源程序 串口源程序 串口源程序 串口源程序
💻 TXT
字号:
#include "uart.h"

/***************************************************************************/
/*  ======== UART_rset ========           */
/*  Set a UART register,设置寄存器             */
/***************************************************************************/
void UART_rset(Int16 regnum,Int16 regval)
{
    Int16 regindex, lcr;
    
    /* Register index is determined by lower 3 bits and the target UART */
    
    //regindex = regnum & 0x7;
//    if (hUart == 1)
//        regindex += 8;
    /*是否为高位的寄存器*/
    /* If regnum between 0x08 and 0x0F, set bit 7 of LCR to access register */
    //if ((regnum & 0x18) == 0x8)
    if((regnum >= 0x10) && (regnum <= 0x1e))
    {
        lcr = rget(TL_UART_LCR);
        waitusec(1);
        rset(TL_UART_LCR, lcr | 0x80);
        waitusec(1);
        rset(regnum, regval);
        waitusec(1);
        rset(TL_UART_LCR, lcr);
        waitusec(1);
    }
    else
    {
        
        rset(regnum, regval);
        waitusec(1);
    }
}

/**********************************************************************/
/*  ======== UART_rget ========         */
/*  Get the value of a UART register,读出寄存器的值       */
/**********************************************************************/
Int16 UART_rget(Int16 regnum)
{
    Int16 regindex, returnval, lcr;
    
    /* Register index is determined by lower 3 bits and the target UART */
    //regindex = regnum & 0x7;
//    if (hUart == 1)
//        regindex += 8;
    
    /* If regnum between 0x08 and 0x0F, set bit 7 of LCR to access register */
    //if ((regnum & 0x18) == 0x8)
    if((regnum >= 0x10) && (regnum <= 0x1e))
    {
        lcr = rget(TL_UART_LCR);
        rset(TL_UART_LCR, lcr | 0x80);
        returnval = rget(regnum);
        rset(TL_UART_LCR, lcr);
    }
    else
    {
        returnval = rget(regnum);
    }
    
    return returnval;
}

/**************************************************************************/
/*  ======== UART_open ========                                 */
/*  Initialize UART and return handle           */
/* 描述:打开相应的串口,并进行相应的配置          */
/* 输入参数:                    */
/*  devid: 选择UART的通路             */
/*  baudrate: 选择正确的波待率            */
/*     config:   其它配置其它的参数组           */
/**************************************************************************/
void UART_open(Int16 baudrate,UART_Config *config)
{
//    UART_Handle hUart;
    //Int16 dl;
    Int8 dl,test;
    /* Assign handle */
//    hUart = devid;
    
    /* Set registers based on config structure */

    /*设置MCR*/
    UART_rset(TL_UART_MCR,config -> regs[3]);
    //test = UART_rget(TL_UART_MCR);
    /*将EFR寄存器的第4位关闭*/
    dl = UART_rget(TL_UART_EFR);
    UART_rset(TL_UART_EFR, dl & 0xEF);
    //test = UART_rget(TL_UART_EFR);
    
    /*设置串口的MCR*/
    UART_rset(TL_UART_MCR, config -> regs[3]);
    //test = UART_rget(TL_UART_MCR);
    
    /*设置串口的IER*/
    UART_rset(TL_UART_IER, config -> regs[0]);
    //test = UART_rget(TL_UART_IER);
    /*设置串口的FCR*/
    UART_rset(TL_UART_FCR, config -> regs[1]);
    //test = UART_rget(TL_UART_FCR); 
    /*设置串口的LCR*/
    UART_rset(TL_UART_LCR, config -> regs[2]);
    //test = UART_rget(TL_UART_LCR);

    /* Set up baud divisor clock,设置波待率 */
    dl = UART_rget(TL_UART_MCR);
    if((dl & 0x80)==0x80)
    {
     baudrate = (baudrate/4); 
    }
    //UART_rset(TL_UART_DLL, baudrate & 0xff);
    UART_rset(TL_UART_DLL, 0x50);
    dl = UART_rget(TL_UART_DLL);
    //UART_rset(TL_UART_DLH, (baudrate >> 8) & 0xff);
    UART_rset(TL_UART_DLH, 0x00);
    dl = UART_rget(TL_UART_DLH);
    /* Clear any outstanding receive characters,清空接收寄存器 */
    UART_rget(TL_UART_RBR);
    
//    return hUart;
}

/*********************************************************************/
/*  ======== UART_getChar ========        */
/*  Get one character of data from the UART,读取数据        */
/*********************************************************************/
Int16 UART_getChar(void)
{
    Int16 status;
    
    while(1)
    {
        status = UART_rget(TL_UART_LSR);
        if ((status & 1) != 0)  // DR
            break;
    }
    
    return 
     UART_rget(TL_UART_RBR);
}

/*********************************************************************/
/*  ======== UART_putChar ========            */
/*  Send one character of data to the UART,发送一个数据        */
/*********************************************************************/
void UART_putChar(Uint16 data)
{
    Int16 status;
    
    while(1)
    {
        status = UART_rget(TL_UART_LSR);
        if ((status & 0x20) != 0)  // THRE
            break;
    }
//    SEEDDM642_waitusec(3);    
    UART_rset(TL_UART_THR, data);

}

void UART_puts(const char *s)
{
 while (*s) {
  UART_putChar(*s++);
 }
}

/* Spin in a delay loop for delay iterations */
void wait(Uint32 delay)
{
    volatile Uint32 i, n;
    
    n = 0;
    for (i = 0; i < delay; i++)
    {
        n = n + 1;
    }
}

/* Spin in a delay loop for delay microseconds */
void waitusec(Uint32 delay)
{
    wait(delay * 21);
}

Uint8 rget(Int16 regnum)
{
    Uint8 *pdata;
    
    /* Return lower 8 bits of register */
    pdata = (Uint8 *)(UART_BASE + regnum);
    return (*pdata & 0xff);
}

void rset(Int16 regnum, Uint8 regval)
{
    Uint8 *pdata;
    
    /* Write lower 8 bits of register */
    pdata = (Uint8 *)(UART_BASE + regnum);
    *pdata = (regval & 0xff);
}

//本代码Blackfin示例

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -