lpc2210之UART0、UART1基于UCOS的全双工通讯

#i nclude "config.h"
#i nclude "stdlib.h"

#define TaskStkLengh 64   //Define the Task0 stack length 定义用户任务0的堆栈长度
 
OS_STK TaskStk [TaskStkLengh];  //Define the Task0 stack 定义用户任务0的堆栈

void  Task0(void *pdata);   //Task0 任务0



typedef  struct  UartModeA
{  uint8 databA;         // 字长度,5/6/7/8
   uint8 stopbA;         // 停止位,1/2
   uint8 parityA;     // 奇偶校验位,0为无校验,1奇数校验,2为偶数校验
}  UARTMODEA;


typedef  struct  UartModeB
{  uint8 databB;         // 字长度,5/6/7/8
   uint8 stopbB;         // 停止位,1/2
   uint8 parityB;     // 奇偶校验位,0为无校验,1奇数校验,2为偶数校验
}  UARTMODEB;

uint8  rcv_bufA[8];         // UART0数据接收缓冲区
uint8  rcv_bufB[8];         // UART1数据接收缓冲区

volatile uint8  rcv_newA;      // 接收新数据标志
volatile uint8  rcv_newB;      // 接收新数据标志

void   __irq IRQ_UART0(void)
{
 uint8  i;
    
   if( 0x04==(U0IIR&0x0F) ) rcv_newA = 1;// 设置接收到新的数据标志
   for(i=0; i<8; i++)
   { rcv_bufA[i] = U0RBR;               // 读取FIFO的数据,并清除中断标志
   }
  
   VICVectAddr = 0x00;               // 中断处理结束
}

void   __irq IRQ_UART1(void)

uint8  i;
    
   if( 0x04==(U1IIR&0x0F) ) rcv_newB = 1;// 设置接收到新的数据标志
   for(i=0; i<8; i++)
   { rcv_bufB[i] = U1RBR;               // 读取FIFO的数据,并清除中断标志
   }
  
   VICVectAddr = 0x00;               // 中断处理结束
}              



void  SendByteA(uint8 dataA)

U0THR = dataA;                       // 发送数据
}

void  SendByteB(uint8 dataB)
{
 U1THR = dataB;                       // 发送数据
}



void  ISendBufA(void)
{  uint8  i;
 
   for(i=0; i<8; i++) SendByteA(rcv_bufA[i]);
   while( (U0LSR&0x20)==0 );          // 等待数据发送
}

void  ISendBufB(void)
{  uint8  i;
 
   for(i=0; i<8; i++) SendByteB(rcv_bufB[i]);
   while( (U1LSR&0x20)==0 );          // 等待数据发送
}              
                
       

uint8  UART0_Ini(uint32 baudA, UARTMODEA set)
{  uint32  bakA;
  
  
   if( (0==baudA)||(baudA>115200) ) return(0);
   if( (set.databA<5)||(set.databA>8) ) return(0);
   if( (0==set.stopbA)||(set.stopbA>2) ) return(0);
   if( set.parityA>4 ) return(0);

  
   U0LCR = 0x80;                        // DLAB位置1
   bakA = (Fpclk>>4)/baudA;
   U0DLM = bakA>>8;
   U0DLL = bakA&0xff;
  
  
   bakA = set.databA-5;                   // 设置字长度
   if(2==set.stopbA) bakA |= 0x04;        // 判断是否为2位停止位 
  
   if(0!=set.parityA) {set.parityA = set.parityA-1; bakA |= 0x08;}
   bakA |= set.parityA<<4;               // 设置奇偶校验
     
   U0LCR = bakA;
  
   return(1);
}

uint8  UART1_Ini(uint32 baudB, UARTMODEB set)
{  uint32  bakB;
  
  
   if( (0==baudB)||(baudB>115200) ) return(0);
   if( (set.databB<5)||(set.databB>8) ) return(0);
   if( (0==set.stopbB)||(set.stopbB>2) ) return(0);
   if( set.parityB>4 ) return(0);

  
   U1LCR = 0x80;                        // DLAB位置1
   bakB = (Fpclk>>4)/baudB;
   U1DLM = bakB>>8;
   U1DLL = bakB&0xff;
  
  
   bakB = set.databB-5;                   // 设置字长度
   if(2==set.stopbB) bakB |= 0x04;        // 判断是否为2位停止位 
  
   if(0!=set.parityB) {set.parityB = set.parityB-1; bakB |= 0x08;}
   bakB |= set.parityB<<4;               // 设置奇偶校验
     
   U1LCR = bakB;
  
   return(1);
}
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 
//主函数

        int main (void)
{
 OSInit ();                          
 OSTaskCreate (Task0,(void *)0, &TaskStk[TaskStkLengh - 1], 2);  
 OSStart ();
 return 0;               
}

  void Task0 (void *pdata)
{
uint8     rcv_counterA;
uint8     rcv_counterB;  
  
   UARTMODEA  uart0_set;
   UARTMODEB  uart1_set;    
  
   PINSEL0 = 0x00050005;                // 设置I/O连接到UART0
   PINSEL1 = 0x00000000;               
   /
   VICIntSelect = 0x00000000;           // 设置所有通道为IRQ中断
   VICVectCntl0 = 0x26;                 // UART0中断通道分配到IRQ slot 0,即优先级最高
   VICVectAddr0 = (int)IRQ_UART0;       // 设置UART0向量地址
   VICIntEnable = 0x000006C0;           // 使能UART0中断
   /
   VICVectCntl1 = 0x27;                 // UART0中断通道分配到IRQ slot 0,即优先级最高
   VICVectAddr1 = (int)IRQ_UART1;       // 设置UART1向量地址
   while(1)                             // 等待中断
   {
   
    //if(1==rcv_newB)
    if(rcv_newB==1)
     { 
     rcv_newB = 0;
       
        ISendBufB();                     // 将接收到的数据发送回主机
     }
   
     //if(1==rcv_newA)
     if(rcv_newA==1)
     { 
     rcv_newA = 0;
       
        ISendBufA();                     // 将接收到的数据发送回主机
     }
   }
//   return(0);
 
}
  
/*********************************************************************************************************
**                            End Of File
****************************************

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值