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

📄 uart.c

📁 UART ADI example 简单的例子 练练手
💻 C
字号:
/****************************************Copyright (c)****************************************************
**                            Guangzhou ZHIYUAN electronics Co.,LTD.
**                                      
**                                 http://www.embedtools.com
**
**--------------File Info---------------------------------------------------------------------------------
** File name:               uart.c
** Latest modified Date:    2008-8-20
** Latest Version:          1.0
** Descriptions:            串口外设的操作函数
**
**--------------------------------------------------------------------------------------------------------
** Created by:              CaiWenqi
** Created date:            2008-8-20
** Version:                 1.0
** Descriptions:            The original version
**
**--------------------------------------------------------------------------------------------------------
** Modified by:             
** Modified date:           
** Version:                 
** Descriptions:            
**
*********************************************************************************************************/
#include    "config.h"

/*********************************************************************************************************
** Function name:       siciwrSetup
** Descriptions:        设置系统中断唤醒使能寄存器
** Input parameters:    uiPara: 见头文件,多个设置使用或操作然后传入此参数
**                      usEnable:   ENABLE  -- 使能
**                                  DISABLE -- 禁能
** Output parameters:   NONE
** Returned value:      NONE
*********************************************************************************************************/
void uartBaudSet (uint16 usBaud)
{
    uint16 divisor;                                                     /* 保存计算的除数值             */
    uint16 msel;       
    uint16 ssel;       

    msel = ((*pPLL_CTL) >> 9) & 0x003f;                                 /* 获取PLL中VCO的倍频数         */
    ssel = (*pPLL_DIV) & 0x000f;                                        /* 获取分频SSEL,SCLK = VCO/SSEL */

    divisor = ((msel * CLKIN)/(ssel * usBaud)) >> 4;                    /* 计算除数                     */
    
    if (*pPLL_CTL & 0x1) {                                              /* PLL入口时钟是否为CLKIN/2     */
        divisor = divisor >> 1; 
    } 
    
    *pUART_LCR |= (1 << 7);                                             /* 使能除数锁存                 */
    ssync();                                                            /* 命令立即生效                 */
    *pUART_DLL = divisor & 0x00ff;                                      /* 赋低八位                     */
    ssync();
    *pUART_DLH = (divisor >> 8);                                        /* 赋高八位                     */
    ssync();
    *pUART_LCR &= (~((uint16)1<<7));                                    /* 禁能除数锁存                 */ 
    ssync();
}

/*********************************************************************************************************
** Function name:       uartConfig
** Descriptions:        设置UART基本通讯参数
** Input parameters:    usBaud            :    设置的波特率
**                      usByteLength      :    数据长度,可取 5, 6 ,7 ,8 
**                      usStopbit         :    停止位宽度,可取 1, 2
**                      usParityCheck     :    PARITY_DIS -- 不进行校验
**                                             PARITY_ODD -- 进行奇校验
**                                             PARITY_EVEN-- 进行偶校验
** Output parameters:   NONE
** Returned value:      true -- 成功设置,false -- 设置失败
*********************************************************************************************************/
uint16 uartConfig (uint16 usBaud, 
                   uint16 usByteLength,
                   uint16 usStopbit,
                   uint16 usParityCheck)
{    
    uint16 usTmpLCR;
    uint16 usTmpRBR;
    usTmpLCR = 0;
    
    uartBaudSet(usBaud);                                                /* 设置波特率                   */
    
    switch (usByteLength) {                                             /* 设置数据长度                 */
    
    case 5:
        usTmpLCR = LEN_5BIT;
        break;
        
    case 6:
        usTmpLCR = LEN_6BIT;
        break;
        
    case 7:
        usTmpLCR = LEN_7BIT;
        break;
        
    case 8:
        usTmpLCR = LEN_8BIT;
        break;
        
    default:
        return false;
    }
    
    switch (usStopbit) {
        
    case 1:
         break;
         
    case 2:
        usTmpLCR |= STOP_2BIT;
        break;
        
    default:
        return false;
    }
    
    switch (usParityCheck) {                                            /* 设置奇偶校验                 */
    
    case PARITY_DIS:
        break;    
        
    case PARITY_ODD:
        usTmpLCR |= PARITY_EN;
        break;
    
    case PARITY_EVEN:
        usTmpLCR |= (PARITY_E | PARITY_EN);
        break;
    
    default:
        return false;
    }
    
    usTmpRBR = *pUART_RBR;                                              /* 清除可能的残留数据           */
    *pUART_LCR = usTmpLCR;                                              /* 回写UART_LCR寄存器,完成配置 */   
    ssync();
    *pUART_GCTL |= 1;                                                   /* 打开UART时钟                 */
    ssync(); 
    return true;
}





/*********************************************************************************************************
** Function name:       uartSendChar
** Descriptions:        发送单个字符
** Input parameters:    cChar : 发送字符
** Output parameters:   NONE
** Returned value:      NONE
*********************************************************************************************************/
void uartSendChar(char cChar)
{
	uint16 i;
    while ((*pUART_LSR & (1<<5)) == 0) {
        asm("nop;");
    }
    for (i = 0; i<0x0fff; i ++) {
        asm("nop;");
    }
    *pUART_THR = cChar;
	ssync();
}

/*********************************************************************************************************
** Function name:       uartSendStr
** Descriptions:        发送字符串
** Input parameters:    cStr : 字符串指针
** Output parameters:   NONE
** Returned value:      NONE
*********************************************************************************************************/
void uartSendStr(char *cStr)
{
	uint32 i;
	i = 0;
    while (cStr[i] != '\0') {
        uartSendChar(cStr[i++]);
    }
}

/*********************************************************************************************************
** Function name:       uartConfigDMA
** Descriptions:        设置DMA方式下的UART基本通讯参数
** Input parameters:    usBaud            :    设置的波特率
**                      usByteLength      :    数据长度,可取 5, 6 ,7 ,8 
**                      usStopbit         :    停止位宽度,可取 1, 2
**                      usParityCheck     :    PARITY_DIS -- 不进行校验
**                                             PARITY_ODD -- 进行奇校验
**                                             PARITY_EVEN-- 进行偶校验
** Output parameters:   NONE
** Returned value:      true -- 成功设置,false -- 设置失败
*********************************************************************************************************/
/*uint16 uartConfigDMA (uint16 usBaud, 
                      uint16 usByteLength,
                      uint16 usStopbit,
                      uint16 usParityCheck
                      uint16 usDMAx)
{
    uartConfig(usBaud, usByteLength, usStopbit, usParityCheck);
    
}*/
/*********************************************************************************************************
** Function name:       uartSendStrDMA
** Descriptions:        通过DMA发送字符串
** Input parameters:    cStr : 字符串指针
** Output parameters:   NONE
** Returned value:      NONE
*********************************************************************************************************/
/*void uartSendStrDMA(char *cStr)
{
	uint32 i;
	i = 0;
    while (cStr[i] != '\0') {
        uartSendChar(cStr[i++]);
    }
}*/




/*********************************************************************************************************
** Function name:       uartISR
** Descriptions:        设置UART基本通讯参数
** Input parameters:    usBaud            :    设置的波特率
**                      usByteLength      :    数据长度,可取 5, 6 ,7 ,8 
**                      usStopbit         :    停止位宽度,可取 1, 2
**                      usParityCheck     :    PARITY_DIS -- 不进行校验
**                                             PARITY_ODD -- 进行奇校验
**                                             PARITY_EVEN-- 进行偶校验
** Output parameters:   NONE
** Returned value:      true -- 成功设置,false -- 设置失败
*********************************************************************************************************/
void uartISR (void)
{
	uint16 usIIRv;
	uint16 usIIR;
	uint16 usLSR;
	char  ucRcv;
	
	usIIR = *pUART_IIR;                                                    /* 同时清除可能的THR中断请求 */
	usIIRv = usIIR & 0x60;
	
	if (!(usIIR & HAVE_INT)) {                                             /* 有中断产生                */
	    
	    if (usIIRv == HAVE_LSR_INT) {                                      /* 发生线状态中断            */
            usLSR = *pUART_LSR;                                            /* 同时清除线状态中断请求    */
	    }
	    
	    if (usIIRv == HAVE_RBR_INT) {                                      /* 接收数据可用中断          */
	        ucRcv = *pUART_RBR;                                            /* 同时清除中断请求          */
	        uartSendChar(ucRcv);
	    }
	    
	    if (usIIRv == HAVE_THRE_INT) {                                     /* 发送数据结束中断          */
	    
	    }
	}
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

⌨️ 快捷键说明

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