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

📄 uart_v1.c

📁 基于arm—LPC2103的串口通讯程序
💻 C
字号:

#include "lpc21xx.h"
#define 	Fpclk		11059200/4
#define		UART_BPS    9600                                         /*  串口通信波特率              */
#define		LN 			30
volatile   	char RcvNew;                                            /*  串口接收新数据的标志        */
char RcvBuf[LN]={0};                                       /*  串口接收数据缓冲区          */
int	RcvNum;                                                     /*  串口接收数据的个数          */

/*********************************************************************************************************
** Function name:		DelayNS
** Descriptions:		延时函数
** input parameters:    uiDly   值越大,延时时间越长
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void DelayNS (int uiDly)
{
    int i;
    
    for (; uiDly > 0; uiDly--){
        for(i = 0; i < 50000; i++);
    }
}

enum OptCodeType {PUSH,POP,CLR,CHECK};
int SentFIFO(int OptCode,char OptNum)
{	  
	#define FIFO_Size 32
	static char FIFO[FIFO_Size],ptIN,ptOUT;
	switch(OptCode)
	{
		case PUSH :
		if(ptIN==(ptOUT+1)) return 0 ;
		else FIFO[(ptIN++)%FIFO_Size]=OptNum;	
		return	1; 

		
		case POP :
		if(ptIN==ptOUT) return(0);
		else return(FIFO[(ptOUT++)%FIFO_Size]);

		
		case CLR :
		ptOUT=ptIN;
		return 1;

		
		case CHECK:
		if(ptOUT>ptIN) return(ptOUT-ptIN);
		else return (ptIN-ptOUT);

		default:
		return 0;
	}
	
}

/*********************************************************************************************************
** Function name:		UART0_IRQ
** Descriptions:		串口中断服务函数
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void __irq UART0_IRQ (void)
{
    int i;   
    while ((U0IIR & 0x01) == 0)
	{ 								                                        /*  判断是否有中断挂起          */
        switch (U0IIR & 0x0E)
		{                                       						   /*  判断中断标志                */
        
            case 0x04: 
			{                                                			 /*  接收数据中断                */
               RcvNew = 1;                                          /*  置接收新数据标志            */
                for (i= 0; i < 8; i++)                										 /*  连续接收8个字节             */
                	if(RcvNum<LN)
						RcvBuf[RcvNum++] = U0RBR;
                	
			}
            break;
            
            case 0x0C: 			                                               /*  字符超时中断                */
                RcvNew = 1;
                while ((U0LSR & 0x01) == 0x01)
				{                        								 /*  判断数据是否接收完毕        */ 
                 if(RcvNum<LN)				RcvBuf[RcvNum++] = U0RBR;
                						
                } 	
                break;

			case 0x02:
				
                
            default:
                break;
        }

    }
   
   VICVectAddr = 0x00;
   
}

/*********************************************************************************************************
** Function name:		UARTInit
** Descriptions:		串口初始化,设置为8位数据位,1位停止位,无奇偶校验,波特率为115200
** input parameters:    uiDly   值越大,延时时间越长
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void UARTInit (void)
{
    int uiFdiv; 
    U0LCR  = 0x83;                                                      /*  允许设置波特率              */
    uiFdiv = (Fpclk / 16) / UART_BPS;                                   /*  设置波特率*/
    U0DLM  = uiFdiv / 256;
    U0DLL  = uiFdiv % 256; 
    U0LCR  = 0x03;                                                      /*  锁定波特率                  */
}

/*********************************************************************************************************
** Function name:		UART0SendByte
** Descriptions:		向串口发送子节数据,并等待数据发送完成,使用查询方式
** input parameters:    uiDat   要发送的数据
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void UART0SendByte (char uiDat)
{
    U0THR = uiDat;                                                      /*  写入数据                    */
    while ((U0LSR & 0x20) == 0);                                        /*  等待数据发送完毕            */
}

/*********************************************************************************************************
** Function name:		UART0SendStr
** Descriptions:		向串口发送字符串
** input parameters:    uiStr   要发送的字符串指针
**                      uiRcvNum   要发送的数据个数
** output parameters:   无
** Returned value:      无
***************************************************************************************8****************/
void UART0SendStr(char const *uiStr)
{
while( *uiStr)                  			/*  发送指定个字节数据           */
        UART0SendByte (*uiStr++);
	//	UART0SendByte ('\r');
//		UART0SendByte ('\n');

}

/*********************************************************************************************************
** Function name:		main
** Descriptions:		跳线JP6短接,打开串口调试软件,串口0中断方式通信
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
int main (void)
{
    int i;
    PINSEL0 = PINSEL0 & (~0x0F);                                        
    PINSEL0 = PINSEL0 | 0x05;                                           /*  设置I/O连接到UART           */
    
    RcvNew = 0;
    UARTInit ();                                                        /*  串口初始化                  */
    U0FCR = 0x81;                                                       /*  使能FIFO,设置8个字节触发点 */
    U0IER = 0x01;                                                       /*  使能接收中断                */
    
   // IRQEnable ();
    
    VICIntSelect = 0x00000000;                                          /*  设置所有中断为向量中断      */
    VICVectCntl0 = 0x20 | 0x06;                                         /*  设置串口中断为最高优先级    */
    VICVectAddr0 = (int)UART0_IRQ;                           		    /*  设置向量地址                */
    VICIntEnable = 1 << 0x06;                                           /*  使能串口中断                */
    
	UART0SendStr ("==================Hello! ZhiHui Chengjiang======================\n");
	UART0SendStr ("ARM: I'm ready now,sent me the massage!\n");
    while (1){
        if (RcvNew == 1)
		{                                          				 /*  判断是否有新数据            */
            RcvNew = 0;                                          /*  清除标志                    */
            UART0SendStr (RcvBuf);                           	/*  向串口发送数据              */
        	for(i=0;i<LN;i++)
				RcvBuf[i]=0;
			RcvNum=0;
		}
    }
  return 0;
}
/*********************************************************************************************************
                           End Of File
**********************************************************************************************************/

⌨️ 快捷键说明

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