这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » LPC2xxx,FIFO LPC2xxx的串口 FIFO中断方式的程序运行几个月

共2条 1/1 1 跳转至

LPC2xxx,FIFO LPC2xxx的串口 FIFO中断方式的程序运行几个月了,该共享了

院士
2006-09-17 18:14:16     打赏
LPC2xxx,FIFO LPC2xxx的串口 FIFO中断方式的程序运行几个月了,该共享了



关键词: LPC2xxx     串口     中断     方式     程序     运行     几个    

院士
2006-12-22 22:43:00     打赏
2楼
问    完整的C语言下载 http://www.shawyoo.com 下载中心 一栏


/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  Copyright (c)  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$                                                肖遥电子(广州)研发中心
$                                        Shawyoo Electronics (GuangZhou) R&D Center
$                           http://www.shawyoo.com         Email: autoele@qq.com
$                                 QQ群:9069471/9069433/9524253        All rights reserved.      
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  Copyright (c)  $$$$$$$$$$$$$$$$$$$$$$$$$$$ */
/*  
                                      文件描述
    文件名:      UART0.c
    功能描述:串口0底层驱动函数
    编译环境:Keil for ARM uVision3
    目标CPU:  LPC2000
    作者:     
    创建时间:
    建议编辑环境:UltraEdit 11.00a+  制表符宽度为三个字符,程序可读性更好。
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*/                      
#include        "RTOS.h"

#define        UART0_GLOBALS

#include    "UART0.h"

#define        TEST_UART0            0



/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名称: Uart0_SendChar
   功能描述: 以查询方式从串口0发送一字节数据。此函数基本不用
   入口参数: 待发送的数据
   出口参数: 无
   全局变量:  无
   创建时间:
   修改时间:
    注:          调用本函数前需关串口中断
*/     
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 以上原形声明的函数供本函数调用
void       Uart0_SendChar(uchar data)
{
    while((U0LSR & 0x00000040) == 0);    //
    U0THR = data;    
}


/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名称:PutString
   功能描述:从串口0发送一块数据(字符串,要用结束符'\0')
   入口参数:待发送的字符串数据指针
   出口参数:发送成功与否标志,成功为0,不成功为非0
   全局变量:
   创建时间:
   修改时间:
*/     
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 以上原形声明的函数供本函数调用
uchar         PutString(uchar  *p)
{
    uchar        i,Len;
    //RS485_TRANSMIT_ENABLE;          
    
    NOP();    NOP();    //return 0;
   if(Txd.Sending)
       return 1;                 // 串口忙,发送失败,需要等待
    Txd.Length = strlen(p);
    if(Txd.Length==0)
        return 1;
    TxdP = p;
    Txd.Sending = 1;
    if(Txd.Length>FIFO_LENGTH)         
        Len = FIFO_LENGTH;
    else                         
        Len = Txd.Length;
    for(i=0;i<Len;i++)    {    U0THR = *TxdP;        TxdP++;    }
    Txd.Length -= Len;
    return 0;
}   // End of <PutString> Function



/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名称:PutBuffer
   功能描述:从串口0发送一块数据(16进制)
   入口参数:初始检验和,数据长度,待发送的数据指针
   出口参数:发送成功与否标志,成功为0,不成功为非0
   全局变量:
   创建时间:
   修改时间:2005-8-10 增加是否重新计算校验和 if(!InitSum)
*/     
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 以上原形声明的函数供本函数调用
uchar         PutBuffer(uchar InitSum,uint16 L,uchar *p)
{
    uint16   i,Len;
    NOP();    NOP();
    if(Txd.Sending)
       return 1;         // 串口忙,发送失败,需要等待
   Txd.Length = L;
    TxdP = p;
    if(!InitSum)            // 如果调用本函数前已计算校验和,则这里不再计算
    {
        Txd.CheckSum = 0;
        for(i=0;i<Txd.Length;i++)
        {
           Txd.CheckSum ^= *p;
           p++;
        }
        *p = Txd.CheckSum;        // Add ChechSum at the end of the buffer
        Txd.Length += 1;               
    }
    Txd.Sending = 1;
    if(Txd.Length>FIFO_LENGTH)         
        Len = FIFO_LENGTH;
    else                         
        Len = Txd.Length;
    for(i=0;i<Len;i++)    {    U0THR = *TxdP;        TxdP++;    }
    Txd.Length -= Len;
    return 0;
}  // End of <PutBuffer> Function



/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名称:UART0_INT
   功能描述:UART0中断函数,实现UART0数据链路层数据通讯
   入口参数:无
   出口参数:无
   全局变量:
   创建时间:
   修改时间:
*/     
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 以上原形声明的函数供本函数调用
void            UART0_INT(void)   __irq      // UART0 interrupt function
{
   uchar       IIR,rc;
   uchar       i,FifoLen;
    uint16    Len;
   NOP();   NOP();    
    IIR = U0IIR;
    switch (IIR & 0x0e)          
    {
         case 0x02:            // Transmit interrupt $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$      
             NOP();    NOP();
            if(Txd.Length)
            {
                 if(Txd.Length>FIFO_LENGTH)         
                    Len = FIFO_LENGTH;
                else                         
                    Len = Txd.Length;
                for(i=0;i<Len;i++)    {    U0THR = *TxdP;        TxdP++;    }
                Txd.Length -= Len;
                Txd.Sending = TRUE;
             }
            else
            {
                 ClearTransmitFlag();            
                UserEvtFlag.UART0 = UART0_TRANSMIT_OK_EVT_FLAG;
                isr_evt_set(UART0_TRANSMIT_OK_EVT_FLAG,TaskID.UART0);                
            }
        break;    //      
        case 0x0c:        // 接收数据有效,超时。注:这里不能用break语句            
            Rec.OverTime = 1;
          case 0x04:     // 接收数据有效 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
            NOP();    NOP();
            FifoLen = 0;
           while((U0LSR&0x01) == 1)         //  最多14字节
           {
               rc = U0RBR;
               //////////////////////////////////// 接收数据
               NOP();    NOP();               
               if(Rec.Counter<UART0_REC_LENGTH)        // 不能超出缓冲区长度
               {                   
                   Rec.Buffer[Rec.Counter++] = rc;                                          
                   Rec.CheckSum ^= rc;
                    FifoLen ++;
               }               
           }    
           ///////////////////////////////////// 判断是否为帧头
           if(Rec.Receiving==0)
           {
               if((Rec.Buffer[0]==0xaa)&&(Rec.Buffer[1]==0x75))
               {
                   Rec.Command = Rec.Buffer[2];
                   Rec.Length = Rec.Buffer[3];
                   Rec.Length <<= 8;
                   Rec.Length |= (0x00ff&((uint)Rec.Buffer[4]));                   
                   for(i=0;i<8;i++) { Rec.Buffer[i] = Rec.Buffer[i+6];}
                   Rec.Counter = FifoLen-6;
                   Rec.Receiving = TRUE;
               }
               else
                   Rec.Error = TRUE;
           }
           /////////////////////////////////// 判断是否有超时,超时则为帧尾
           if((Rec.OverTime)||(Rec.Counter>=Rec.Length))
           {
               Rec.CheckSum ^= Rec.Buffer[Rec.Counter-1];               
               if(Rec.CheckSum!=Rec.Buffer[Rec.Counter-1])
                   Rec.Error = TRUE;                               
                UserEvtFlag.UART0 = UART0_RECEIVE_OK_EVT_FLAG;
                isr_evt_set(UART0_RECEIVE_OK_EVT_FLAG,TaskID.UART0);   //
           }
              NOP();    NOP();              
       
        break;
        default: break;
    }    // End of Switch                    
    VICVectAddr = 0;    
    NOP();   NOP();
    NOP();    NOP();
}  // End of UART0 Interrupt function




/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名称:ClearReceiveFlag
   功能描述:清除接收相关标志
   入口参数:清接收缓冲区标志
   出口参数:无
   全局变量: 无
   创建时间:
   修改时间:
*/     
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 以上原形声明的函数供本函数调用
void            ClearReceiveFlag(uint16 c)
{     
    NOP();    NOP();      
    Rec.CheckSum = Rec.Error = 0;
    Rec.Command = Rec.OverTime = 0;
    Rec.Counter  = Rec.Length   = Rec.Receiving = 0;            
    if(c)
    {    
        for(c=0;c<UART0_REC_LENGTH;c++)    { Rec.Buffer[c] = 0; }
    }
} // End of Function                 




/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名称:ClearTransmitFlag
   功能描述:清除发送缓冲区及相关标志
   入口参数:无
   出口参数:无
   全局变量:
   创建时间:
   修改时间:
*/     
void            Delay_mS(unsigned char  c);
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 以上原形声明的函数供本函数调用
void            ClearTransmitFlag(void)
{
    
    NOP();    NOP();         
    Txd.CheckSum = Txd.Sending = 0;    
    Txd.Length = 0;    
} // End of Function                  




/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    函数名称:UART0_Init
   功能描述:初始化UART0
   入口参数:无
   出口参数:无
   全局变量:
   创建时间:
   修改时间:
*/     
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 以上原形声明的函数供本函数调用
void            UART0_Init(void)
{
    uint16  Fdiv;        
    ///////////////  UART0缓冲区及相关标志清零
    NOP();    NOP();    
    ////////////////////////////////  UART0 Initialize    
    PINSEL0 |= 0x00000005;              // Enable RxD0 and TxD0  
   U0LCR = 0x83;                       
    Fdiv = (Fpclk / 16) / UART0_BPS;    // set baudrate
   U0DLM = Fdiv / 256;                            
    U0DLL = Fdiv % 256;        
    U0LCR = ((U0_DATA_BIT)|(U0_STOP_BIT<<2)|(U0_CHECK_BIT_EN<<3)|(U0_CHECK_BIT<<4));
   U0IER = 0x03;                       // Enable RDA and THRE interrupts
    U0FCR = 0xc7;                       // Eable FIFO
   VICVectAddr1  = (uint32)UART0_INT;
   VICVectCntl1  = (IRQ_ENABLE|UART0_INT_VECT);
   VICIntEnable  |= (1<<UART0_INT_VECT);                //
   U0THR = 'A';                // 保证函数<Uart0_SendChar>正常
   NOP();    NOP();
}  // End of UART0 initialize function




/*
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$  End of File $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
*/ 1: 杰作应该顶一下 2: 完整的C语言下载 http://www.shawyoo.com 下载中心 下载不了!!! 3: 顶一下  确实下不了。 4: 又是很久没上网了,这下可以下载了 5: 顶一下!!! 6: 欣赏一下支持楼主的无私 7: 欣赏一下 8: http://www.shawyoo.com不太稳定只上去过一次... 9: 会自己写的可能都共享了吧?还有谁有原创的LPC21XX串口程序?都贡献出来让大家看看嘛!

共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]