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

📄 softuart.lst

📁 用软件实现的异步串行口源程序代码 速度还可以
💻 LST
📖 第 1 页 / 共 4 页
字号:
 169          
 170          void SendCommBuffer2(unsigned char *base, unsigned char size)  //发送多字节
 171          {
 172   1      unsigned char i=0;
 173   1              if (!size) return;                      // 发送字节为空
 174   1              while (i<size)                          // 将发送的数据存入发送缓冲区,注意缓冲区的大小,本例程未考虑缓冲区溢出情况
 175   1              {        
 176   2                      CommSendBuffer2[CommSendBufferTail2]=base[i]; 
 177   2                      i++;
 178   2                      CommSendBufferTail2++; 
 179   2                      if (CommSendBufferTail2==DB_SENDMAXSIZE2)
C51 COMPILER V6.23a  SOFTUART                                                              04/15/2002 18:18:05 PAGE 4   

 180   2                      { 
 181   3                              CommSendBufferTail2=0;
 182   3                      }
 183   2              }
 184   1              if (SendItComm2)
 185   1              {
 186   2                      STXBSY0 = 1; 
 187   2              TDR0 = CommSendBuffer2[CommSendBufferHead2]; 
 188   2                      CCF0 = 1;       
 189   2              }
 190   1      }
 191          
 192          bit GetCommChar2(unsigned char idata *ch)    //读取一字节数据,  有数据返回1,无数据返回0.
 193          { 
 194   1              if (CommRecBufferTail2==CommRecBufferHead2) return 0;     
 195   1              *ch=CommRecBuffer2[CommRecBufferHead2];
 196   1              CommRecBufferHead2++;
 197   1              if (CommRecBufferHead2==DB_RECMAXSIZE2)
 198   1              {
 199   2                      CommRecBufferHead2=0;
 200   2              }
 201   1              if (CommRecBufferTail2==CommRecBufferHead2) FlagRecComm2=0;
 202   1              return 1;
 203   1      }
 204          
 205          //------------------------------------------------------------------------------------------
 206          // SW_UART_INIT: 软件UART 初始化程序
 207          // - 配置PCA: 模块1 设置为下跳沿捕捉方式; 模块0 设置为软件定时方式
 208          //   PCA 时基 = 系统时钟/4; 关闭PCA中断 
 209          //
 210          void    SW_UART_INIT0(void)
 211          {
 212   1              PCA0CPM1 = 0x10;                // Module 1 in negative capture mode; module 1 interrupt disabled.
 213   1              PCA0CPM0 = 0x48;                // Module 0 in software timer mode; module  0 interrupt disabled.       
 214   1              PCA0CN = 0;                             // Leave PCA disabled
 215   1              PCA0MD = 0x02;                  // PCA timebase = SYSCLK/4; PCA counter
 216   1                                                              // interrupt disabled.
 217   1              CCF1 = 0;                               // Clear pending PCA module 0 and
 218   1              CCF0 = 0;                               // module 1 capture/compare interrupts.
 219   1              SRI0 = 0;                               // Clear Receive complete flag.
 220   1              STI0 = 0;                               // Clear Transmit complete flag.
 221   1              SW_TX0 = 1;                             // TX line initially high.      
 222   1              STXBSY0 = 0;                            // Clear SW_UART Busy flag
 223   1      
 224   1              SREN0 = 1;                              // Enable SW_UART Receiver
 225   1              SES0 = 1;                               // User-level interrupt support enabled.
 226   1              SendItComm2=1;
 227   1      }
 228          
 229          //------------------------------------------------------------------------------------------
 230          // SW_UART_ENABLE: SW_UART Enable Routine
 231          // Enables SW_UART for use.
 232          // - Enables PCA module 0 interrupts
 233          // - Enables PCA module 1 interrupts
 234          // - Starts PCA counter.
 235          //
 236          void    SW_UART_ENABLE0(void)
 237          {
 238   1              PCA0CPM1 |= 0x01;               // Enable module 1 (receive) interrupts.
 239   1              PCA0CPM0 |= 0x01;               // Enable module 0 (transmit) interrupts.
 240   1              CR = 1;                                 // Start PCA counter.
 241   1              EIE1 |= 0x08;                   // Enable PCA interrupts
C51 COMPILER V6.23a  SOFTUART                                                              04/15/2002 18:18:05 PAGE 5   

 242   1              EA = 1;                                 // Globally enable interrupts
 243   1      }
 244          
 245          void ClearCommRecBuffer3(void)
 246          {
 247   1              CommRecBufferHead3=CommRecBufferTail3=0;
 248   1              FlagRecComm3=0;
 249   1      }
 250          
 251          void SendCommChar3(char ch)                     //发送一字节
 252          {
 253   1              CommSendBuffer3[CommSendBufferTail3]=ch;   // 将欲发送的字节存入发送缓冲区
 254   1              CommSendBufferTail3++;                                     // 移动发送缓冲区队列指针
 255   1              if (CommSendBufferTail3==DB_SENDMAXSIZE3)  // 检查循环队列指针
 256   1              {       
 257   2                      CommSendBufferTail3=0;                                  
 258   2              }
 259   1              if (SendItComm3)                                                   // 空闲状态启动发送
 260   1              {        
 261   2                      STXBSY1 = 1;                                               // 发送标志置位
 262   2              TDR1 = CommSendBuffer3[CommSendBufferHead3]; 
 263   2                      CCF2 = 1;                                                          // 强制PCA模块0中断,启动发送
 264   2              }
 265   1              return ;
 266   1      }
 267          
 268          void SendCommBuffer3(unsigned char *base, unsigned char size)  //发送多字节
 269          {
 270   1      unsigned char i=0;
 271   1              if (!size) return;                      // 发送字节为空
 272   1              while (i<size)                          // 将发送的数据存入发送缓冲区,注意缓冲区的大小,本例程未考虑缓冲区溢出情况
 273   1              {        
 274   2                      CommSendBuffer3[CommSendBufferTail3]=base[i]; 
 275   2                      i++;
 276   2                      CommSendBufferTail3++; 
 277   2                      if (CommSendBufferTail3==DB_SENDMAXSIZE3)
 278   2                      { 
 279   3                              CommSendBufferTail3=0;
 280   3                      }
 281   2              }
 282   1              if (SendItComm3)
 283   1              {
 284   2                      STXBSY1 = 1; 
 285   2              TDR1 = CommSendBuffer2[CommSendBufferHead2]; 
 286   2                      CCF2 = 1;       
 287   2              }
 288   1      }
 289          
 290          bit GetCommChar3(unsigned char idata *ch)    //读取一字节数据,  有数据返回1,无数据返回0.
 291          { 
 292   1              if (CommRecBufferTail3==CommRecBufferHead3) return 0;     
 293   1              *ch=CommRecBuffer3[CommRecBufferHead3];
 294   1              CommRecBufferHead3++;
 295   1              if (CommRecBufferHead3==DB_RECMAXSIZE2)
 296   1              {
 297   2                      CommRecBufferHead3=0;
 298   2              }
 299   1              if (CommRecBufferTail3==CommRecBufferHead3) FlagRecComm3=0;
 300   1              return 1;
 301   1      }
 302          
 303          
C51 COMPILER V6.23a  SOFTUART                                                              04/15/2002 18:18:05 PAGE 6   

 304          
 305          void    SW_UART_INIT1(void)
 306          {
 307   1              PCA0CPM3 = 0x10;                // Module 1 in negative capture mode; module 1 interrupt disabled.
 308   1              PCA0CPM2 = 0x48;                // Module 0 in software timer mode; module  0 interrupt disabled.       
 309   1              PCA0CN = 0;                             // Leave PCA disabled
 310   1              PCA0MD = 0x02;                  // PCA timebase = SYSCLK/4; PCA counter
 311   1                                                              // interrupt disabled.
 312   1              CCF3 = 0;                               // Clear pending PCA module 0 and
 313   1              CCF2 = 0;                               // module 1 capture/compare interrupts.
 314   1              SRI1 = 0;                               // Clear Receive complete flag.
 315   1              STI1 = 0;                               // Clear Transmit complete flag.
 316   1              SW_TX1 = 1;                             // TX line initially high.      
 317   1              STXBSY1 = 0;                            // Clear SW_UART Busy flag
 318   1      
 319   1              SREN1 = 1;                              // Enable SW_UART Receiver
 320   1              SES1 = 1;                               // User-level interrupt support enabled.
 321   1              SendItComm3=1;
 322   1      }
 323          
 324          void    SW_UART_ENABLE1(void)
 325          {
 326   1              PCA0CPM3 |= 0x01;               // Enable module 1 (receive) interrupts.
 327   1              PCA0CPM2 |= 0x01;               // Enable module 0 (transmit) interrupts.
 328   1              CR = 1;                                 // Start PCA counter.
 329   1              EIE1 |= 0x08;                   // Enable PCA interrupts
 330   1              EA=1;
 331   1      }
 332          
 333          //------------------------------------------------------------------------------------
 334          // Interrupt Service Routines
 335          //------------------------------------------------------------------------------------
 336          //
 337          // PCA_ISR: PCA Interrupt Service Routine.
 338          // This ISR is triggered by both transmit and receive functions, for each bit that
 339          // is transmitted or received. 
 340          // - Checks module 1 interrupt flag (CCF1); if set, services receive state.
 341          // - Checks module 0 interrupt flag (CCF0); if set, services transmit state.
 342          // - Checks module 3 interrupt flag (CCF3); if set, services receive state.
 343          // - Checks module 2 interrupt flag (CCF2); if set, services transmit state.

⌨️ 快捷键说明

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