16V554 的测试代码

   //------------------------------------------------------------------------   

  1. #include   "AT16C554.H"   
  2. //--------------------------------------------------------------------------------------------------   
  3. code        Uint    PORTAddrTABLE[PortNumber]={STARTADDR_SERIAL1,   
  4.                                                STARTADDR_SERIAL2,   
  5.                                                STARTADDR_SERIAL3,   
  6.                                                STARTADDR_SERIAL4};          //串口端口地址映射表   
  7.    
  8.    
  9. xdata       TCommHandle     CommDevice[PortNumber];   
  10. sbit        reset_16c554 =  P1^4;                                           //ST16C554 复位控制线   
  11. //---------------------------------------------------------------------------------------------------   
  12. //  Define Private Faction   
  13. //-----------------------------------------------------------------------------------------------------   
  14.    
  15. bool ReadPortRecBuf(uchar Port,Uchar *Byte);   
  16. bool WritePortSendBuf(uchar Port,Uchar Byte);   
  17. void Port_open(Uchar PortID,Uint Baud);   
  18.    
  19. //-------------------------------------------------------------------------   
  20. //  Define Public Faction   
  21. //-------------------------------------------------------------------------   
  22.    
  23. bool Init_16c554(void);   
  24. void Port_open(Uchar PortID,Uint Baud);   
  25. //void Port_open(Uchar PortID,int Baud,Uchar DataBitNum,Uchar Parit,Uchar Stop);   
  26. void Port_close(Uchar PortID);   
  27. int  Port_Receive(Uchar PortID,void *Buffer,int Count);   
  28. int  Port_Send(Uchar PortID,void *Buffer,int Count);   
  29.    
  30.    
  31.    
  32. //---------------------------------------------------------------------------------------------   
  33. //  Public Faction Code   
  34. //---------------------------------------------------------------------------------------------   
  35.    
  36.    
  37.    
  38.    
  39. //---------------------------------------------------------------------------------------------   
  40. //  初始化ST16C554   
  41. //---------------------------------------------------------------------------------------------   
  42. bool Init_16c554(void)   
  43. {   
  44. uchar xdata *ptr;   
  45. uchar i=Init_16c554_Count;   
  46. bool Result=false;   
  47.    
  48. while(--i)                                                  // 复位10次不成功,则返回出错   
  49.     {   
  50.       _nop_();   
  51.       reset_16c554 = FREE_RESET;   
  52.       ms_delay(5);   
  53.       reset_16c554 = RESET;                                 // 复位16C554   
  54.       ms_delay(10);                                         // 延时1毫秒   
  55.       reset_16c554 = FREE_RESET;   
  56.       ms_delay(10);   
  57.       ptr = STARTADDR_SERIAL1+SPR;                          // 通道1临时寄存器   
  58.       *ptr = 0xaa;                                          // 写测试值到临时寄存器   
  59.       _nop_();   
  60.       _nop_();   
  61.       if(*ptr == 0xaa)    
  62.         {   
  63.            Result=true;   
  64.            break;   
  65.         };//end if   
  66.     };//end while   
  67. for(i=0;i<PortNumber;i++) CommDevice[i].RAMAddress_Map=PORTAddrTABLE[i];   
  68.    
  69. return Result;   
  70. }   
  71. //---------------------------------------------------------------------------------------------   
  72. //    打开串口       
  73. //---------------------------------------------------------------------------------------------   
  74. void Port_open(Uchar PortID,Uint Baud)   
  75. {   
  76. uchar xdata *ptr;   
  77.    
  78. ptr = CommDevice[PortID].RAMAddress_Map;                                                // 取端口的基地址   
  79. *(ptr+LCR) = LCR_DIV_EN;                                                                // 允许发送波特率   
  80. *(ptr+DLM) = (uchar)(Baud>>8);   
  81. *(ptr+DLL) = (uchar)Baud;                                                               // 设置默任波特率为19200   
  82. *(ptr+LCR) = LCR_WORD_LEN8|LCR_STOP_LEN1;                                               // 设置字长为8,1个停止位,无校验位   
  83. *(ptr+FCR) = FIFO_EN|FIFO_TRI_LEVEL_1|FIFO_CLEAR_RCVR|FIFO_CLEAR_XMIT|FIFO_DMA_MODE;    // 设置FIFO 控制寄存器   
  84. *(ptr+IER) = IER_TRANSMITHOLD_INT|IER_RECEIVEHOLD_INT;                                  // 设置中断屏蔽寄存器   
  85. *(ptr+MCR) = MCR_INTX_EN;                                                               // 允许中断引脚输出   
  86.    
  87. if(PortID==0)   EX0 = true;                         // 开外部中断0(通道1中断)   
  88. if(PortID==1)   EX1 = true;                         // 开外部中断1(通道2中断)   
  89. if(PortID==2)   EX2 = true;                         // 开外部中断2(通道3中断)   
  90. if(PortID==3)   EX3 = true;                         // 开外部中断3(通道4中断)   
  91.    
  92. CommDevice[PortID].CommReceive_Head=0;   
  93. CommDevice[PortID].CommReceive_Trail=0;   
  94. CommDevice[PortID].CommSend_Head=0;   
  95. CommDevice[PortID].CommSend_Trail=0;   
  96. CommDevice[PortID].Comm_Stu=SERIAL_STU_FREE;   
  97. }   
  98. //---------------------------------------------------------------------------------------------   
  99. //       关闭指定的端口   
  100. //---------------------------------------------------------------------------------------------   
  101. /*void Port_close(Uchar PortID)  
  102. {  
  103. uchar xdata *ptr;  
  104.   
  105. ptr = CommDevice[PortID].RAMAddress_Map;            // 取端口的基地址  
  106. *(ptr+LCR) = LCR_DIV_Dis;                           // 禁止发送波特率  
  107. *(ptr+MCR) = MCR_INTX_Dis;                          // 允许中断引脚输出  
  108.   
  109. CommDevice[PortID].CommReceive_Head=0;  
  110. CommDevice[PortID].CommReceive_Trail=0;  
  111. CommDevice[PortID].CommSend_Head=0;  
  112. CommDevice[PortID].CommSend_Trail=0;  
  113. CommDevice[PortID].Comm_Stu=SERIAL_STU_FREE;  
  114.   
  115. if(PortID==0)   EX0 = false;                        // 关外部中断0(通道1中断)  
  116. if(PortID==1)   EX1 = false;                        // 关外部中断1(通道2中断)  
  117. if(PortID==2)   EX2 = false;                        // 关外部中断2(通道3中断)  
  118. if(PortID==3)   EX3 = false;                        // 关外部中断3(通道4中断)  
  119. }*/   
  120. //--------------------------------------------------------------------------------------------------   
  121.   //功  能:从串口中接收数据   
  122.   //返回值:从串口中接收到的数据个数   
  123.   //参  数:   
  124.   //        Buffer:接收数的缓冲区   
  125.   //        Count: 想从串口缓冲区中接收数据个数   
  126. //--------------------------------------------------------------------------------------------------   
  127. /*int Port_Receive(uchar Port,register void *Buffer,int Count)  
  128. {  
  129. Uchar Size=0;  
  130.   
  131. if(Port>PortNumber-1)  return Size;  
  132.   
  133. while((Size<Count)&&(ReadPortRecBuf(Port,Buffer)))  
  134.      {  
  135.        Size++;  
  136.        Buffer=Buffer+1;  
  137.      };//end while   
  138. return Size;  
  139. }*/   
  140. //--------------------------------------------------------------------------------------------------   
  141.   //功  能:往串口发送数据   
  142.   //返回值:已发送的数据个数   
  143.   //参  数:   
  144.   //        Buffer:发送数据的缓冲区   
  145.   //        Count: 想从串口发送数据的个数   
  146. //--------------------------------------------------------------------------------------------------   
  147. int Port_Send(uchar Port,register void *Buffer,int Count)   
  148. {   
  149. uchar           i=0;   
  150. Uchar           Size=0;   
  151. uchar xdata     *ptr;   
  152.    
  153. if(Port>PortNumber-1)  return Size;   
  154.    
  155. ptr = CommDevice[Port].RAMAddress_Map;                                                                          // 取端口基地址   
  156.    
  157. while(Size<Count)   
  158.      {   
  159.         if(WritePortSendBuf(Port,*((Uchar *)Buffer)))   
  160.           {   
  161.             Size=Size+1;   
  162.             Buffer=Buffer+1;   
  163.           };//end if   
  164.         if(CommDevice[Port].Comm_Stu==SERIAL_STU_FREE)   
  165.           {   
  166.             for(i=0;(i<FIFO_SIZE)&&(CommDevice[Port].CommSend_Head!=CommDevice[Port].CommSend_Trail);i++)   
  167.                 {      
  168.                   CommDevice[Port].CommSend_Trail=(CommDevice[Port].CommSend_Trail+1)%CommMaxSendBuffer;   
  169.                   *(ptr+THR) = CommDevice[Port].CommSendBuffer[CommDevice[Port].CommSend_Trail];                // 发送数据   
  170.                 };//end for   
  171.             CommDevice[Port].Comm_Stu = SERIAL_STU_BUSY;   
  172.           };//end if   
  173.      };//end while   
  174. return Size;   
  175. }   
  176. //----------------------------------------------------------------------------------------------------   
  177.    
  178.    
  179.    
  180. //----------------------------------------------------------------------------------------------------   
  181. //私有函数   
  182. //----------------------------------------------------------------------------------------------------   
  183.   //功  能:从串口接收缓冲区中取得数据    
  184.   //返回值:   
  185.   //         true:   从缓冲区中成功取得数据   
  186.   //         false:  从缓冲区中无法取得数据   
  187.   //参  数: *Byte:从缓冲区中取得数据返回   
  188. //-----------------------------------------------------------------------------------------------------   
  189. /*bool ReadPortRecBuf(uchar Port,Uchar *Byte)  
  190. {  
  191. if(CommDevice[Port].CommReceive_Trail==CommDevice[Port].CommReceive_Head)  return false;  
  192.   
  193. *Byte=CommDevice[Port].CommReceiveBuffer[CommDevice[Port].CommReceive_Trail];  
  194. CommDevice[Port].CommReceive_Trail=(CommDevice[Port].CommReceive_Trail+1)%CommMaxReceiveBuffer;  
  195.   
  196. return true;  
  197. }*/   
  198. //-----------------------------------------------------------------------------------------------------   
  199.   //功  能:往串口发送缓冲区中写数据   
  200.   //返回值:   
  201.   //         true:   往缓冲区中写数据成功   
  202.   //         false:  从缓冲区中写数据失败   
  203.   //参  数: Byte:  往缓冲区中写入的数据   
  204. //------------------------------------------------------------------------------   
  205. bool WritePortSendBuf(uchar Port,Uchar Byte)   
  206. {   
  207. if((CommDevice[Port].CommSend_Head+1)%CommMaxSendBuffer==CommDevice[Port].CommSend_Trail) return false;   
  208.    
  209. CommDevice[Port].CommSend_Head=(CommDevice[Port].CommSend_Head+1)%CommMaxSendBuffer;   
  210. CommDevice[Port].CommSendBuffer[CommDevice[Port].CommSend_Head]=Byte;   
  211.    
  212. return true;   
  213. }   
  214. //--------------------------------------------------------------------------------   
  215. // 外部扩展串行口1中断处理程序(外部中断0)   
  216. //--------------------------------------------------------------------------------   
  217. void serial1_int() interrupt 0   
  218. {   
  219.     uchar xdata *ptr;   
  220.     uchar event;   
  221.     uchar port;   
  222.        
  223.     port =  CHANL1_PORT;   
  224.     ptr =   CommDevice[port].RAMAddress_Map;                                                // 读ISR 中断状态寄存器   
  225.     event = *(ptr+ISR)&0x0f;                                                                // 读最高优先级别的中断码   
  226.     _nop_();   
  227.        
  228.     switch(event)   
  229.           {   
  230.             case EVENT_RXRDY:                                                               // 接受数据到中断   
  231.             case EVENT_RXRDY_TIMEOUT:   
  232.                 for(;*(ptr+LSR)&EVENT_LSR_RECEIVE;)   
  233.                     {   
  234.                         CommDevice[port].CommReceiveBuffer[CommDevice[port].CommReceive_Head] = *(ptr+RHR);   
  235.                         if(((CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer)!=CommDevice[port].CommReceive_Trail)   
  236.                             CommDevice[port].CommReceive_Head=(CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer;   
  237.                     }; //end for   
  238.                 break;     
  239.             case EVENT_TXRDY:                                                               //发送数据中断   
  240.                 if(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head)         //判断数据包是否发送完毕   
  241.                     {   
  242.                         for(event=0;(event<FIFO_SIZE)&&(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head);event++)   
  243.                             {      
  244.                                 CommDevice[port].CommSend_Trail=(CommDevice[port].CommSend_Trail+1)%CommMaxSendBuffer;   
  245.                                 *(ptr+THR) = CommDevice[port].CommSendBuffer[CommDevice[port].CommSend_Trail];   
  246.                             };//end for   
  247.                           CommDevice[port].Comm_Stu = SERIAL_STU_BUSY;   
  248.                     }else CommDevice[port].Comm_Stu = SERIAL_STU_FREE;   
  249.                  break;   
  250.           };//end switch   
  251. }   
  252. //--------------------------------------------------------------------------------   
  253. // 外部扩展串行口2中断处理程序(外部中断2)   
  254. //--------------------------------------------------------------------------------   
  255. void serial2_int() interrupt 2   
  256. {   
  257.     uchar xdata *ptr;   
  258.     uchar event;   
  259.     uchar port;   
  260.        
  261.     port =  CHANL2_PORT;   
  262.     ptr =   CommDevice[port].RAMAddress_Map;                                                //读ISR 中断状态寄存器   
  263.     event = *(ptr+ISR)&0x0f;                                                                //读最高优先级别的中断码   
  264.     _nop_();   
  265.        
  266.     switch(event)   
  267.           {   
  268.             case EVENT_RXRDY:                                                               //接受数据到中断   
  269.             case EVENT_RXRDY_TIMEOUT:   
  270.                 for(;*(ptr+LSR)&EVENT_LSR_RECEIVE;)   
  271.                     {   
  272.                         CommDevice[port].CommReceiveBuffer[CommDevice[port].CommReceive_Head] = *(ptr+RHR);   
  273.                         if(((CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer)!=CommDevice[port].CommReceive_Trail)   
  274.                             CommDevice[port].CommReceive_Head=(CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer;   
  275.                     }; //end for   
  276.                 break;     
  277.             case EVENT_TXRDY:                                                               //发送数据中断   
  278.                 if(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head)         //判断数据包是否发送完毕   
  279.                     {   
  280.                         for(event=0;(event<FIFO_SIZE)&&(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head);event++)   
  281.                             {      
  282.                                 CommDevice[port].CommSend_Trail=(CommDevice[port].CommSend_Trail+1)%CommMaxSendBuffer;   
  283.                                 *(ptr+THR) = CommDevice[port].CommSendBuffer[CommDevice[port].CommSend_Trail];   
  284.                             };//end for   
  285.                         CommDevice[port].Comm_Stu = SERIAL_STU_BUSY;   
  286.                     }else CommDevice[port].Comm_Stu = SERIAL_STU_FREE;   
  287.                  break;   
  288.           };//end switch   
  289. }   
  290.    
  291. //--------------------------------------------------------------------------------   
  292. // 外部扩展串行口3中断处理程序(外部中断6)   
  293. //--------------------------------------------------------------------------------   
  294. void serial3_int () interrupt 6   
  295. {   
  296.     uchar xdata *ptr;   
  297.     uchar event;   
  298.     uchar port;   
  299.        
  300.     port =  CHANL3_PORT;   
  301.     ptr =   CommDevice[port].RAMAddress_Map;                                            // 读ISR 中断状态寄存器   
  302.     event = *(ptr+ISR)&0x0f;                                                            // 读最高优先级别的中断码   
  303.     _nop_();   
  304.        
  305.     switch(event)   
  306.           {   
  307.             case EVENT_RXRDY:                                                           // 接受数据到中断   
  308.             case EVENT_RXRDY_TIMEOUT:   
  309.                 for(;*(ptr+LSR)&EVENT_LSR_RECEIVE;)   
  310.                     {   
  311.                         CommDevice[port].CommReceiveBuffer[CommDevice[port].CommReceive_Head] = *(ptr+RHR);   
  312.                         if(((CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer)!=CommDevice[port].CommReceive_Trail)   
  313.                             CommDevice[port].CommReceive_Head=(CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer;   
  314.                     }; //end for   
  315.                 break;     
  316.             case EVENT_TXRDY:                                                           //发送数据中断   
  317.                 if(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head)     //判断数据包是否发送完毕   
  318.                     {   
  319.                         for(event=0;(event<FIFO_SIZE)&&(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head);event++)   
  320.                             {      
  321.                                 CommDevice[port].CommSend_Trail=(CommDevice[port].CommSend_Trail+1)%CommMaxSendBuffer;   
  322.                                 *(ptr+THR) = CommDevice[port].CommSendBuffer[CommDevice[port].CommSend_Trail];   
  323.                             };//end for   
  324.                         CommDevice[port].Comm_Stu = SERIAL_STU_BUSY;   
  325.                     }else CommDevice[port].Comm_Stu = SERIAL_STU_FREE;   
  326.                  break;   
  327.           };//end switch   
  328. }   
  329. //--------------------------------------------------------------------------------   
  330. // 外部扩展串行口4中断处理程序(外部中断7)   
  331. //--------------------------------------------------------------------------------   
  332. void serial4_int () interrupt 7   
  333. {   
  334.     uchar xdata *ptr;   
  335.     uchar event;   
  336.     uchar port;   
  337.        
  338.     port =  CHANL4_PORT;   
  339.     ptr =   CommDevice[port].RAMAddress_Map;                                            // 读ISR 中断状态寄存器   
  340.     event = *(ptr+ISR)&0x0f;                                                            // 读最高优先级别的中断码   
  341.     _nop_();   
  342.        
  343.     switch(event)   
  344.           {   
  345.             case EVENT_RXRDY:                                                               // 接受数据到中断   
  346.             case EVENT_RXRDY_TIMEOUT:   
  347.                 for(;*(ptr+LSR)&EVENT_LSR_RECEIVE;)   
  348.                     {   
  349.                         CommDevice[port].CommReceiveBuffer[CommDevice[port].CommReceive_Head] = *(ptr+RHR);   
  350.                         if(((CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer)!=CommDevice[port].CommReceive_Trail)   
  351.                             CommDevice[port].CommReceive_Head=(CommDevice[port].CommReceive_Head+1)%CommMaxReceiveBuffer;   
  352.                     }; //end for   
  353.                 break;   
  354.             case EVENT_TXRDY:                                                               //发送数据中断   
  355.                 if(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head)         //判断数据包是否发送完毕   
  356.                     {   
  357.                         for(event=0;(event<FIFO_SIZE)&&(CommDevice[port].CommSend_Trail!=CommDevice[port].CommSend_Head);event++)   
  358.                             {      
  359.                                 CommDevice[port].CommSend_Trail=(CommDevice[port].CommSend_Trail+1)%CommMaxSendBuffer;   
  360.                                 *(ptr+THR) = CommDevice[port].CommSendBuffer[CommDevice[port].CommSend_Trail];   
  361.                             };//end for   
  362.                         CommDevice[port].Comm_Stu = SERIAL_STU_BUSY;   
  363.                     }else CommDevice[port].Comm_Stu = SERIAL_STU_FREE;   
  364.                  break;   
  365.           };//end switch   
  366. }   
  367. //--------------------------------------------------------------------------------   
 

posted on 2015-12-30 16:52  嵌入式操作系统  阅读(280)  评论(0编辑  收藏  举报

导航