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

📄 f34x_uart_multiuart.lst

📁 80C51 UART全双工通讯的典型应用
💻 LST
📖 第 1 页 / 共 2 页
字号:
 208          //-----------------------------------------------------------------------------
 209          // UART0_Init
 210          //-----------------------------------------------------------------------------
 211          //
 212          // Return Value : None
 213          // Parameters   : None
 214          //
 215          // Configure the UART0 using Timer1, for baudrate; and 8-N-1.
 216          //
 217          //-----------------------------------------------------------------------------
 218          void UART0_Init (void)
 219          {
 220   1         SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
 221   1                                             //        level of STOP bit is ignored
 222   1                                             //        RX enabled
 223   1                                             //        ninth bits are zeros
 224   1                                             //        clear RI0 and TI0 bits
 225   1      
 226   1         if (SYSCLK/BAUDRATE0/2/256 < 1) {
 227   2            TH1 = -(SYSCLK/BAUDRATE0/2);
 228   2            CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
 229   2            CKCON |=  0x08;
 230   2         } else if (SYSCLK/BAUDRATE0/2/256 < 4) {
 231   2            TH1 = -(SYSCLK/BAUDRATE0/2/4);
 232   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01                 
 233   2            CKCON |=  0x09;
 234   2         } else if (SYSCLK/BAUDRATE0/2/256 < 12) {
 235   2            TH1 = -(SYSCLK/BAUDRATE0/2/12);
 236   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
 237   2         } else {
 238   2            TH1 = -(SYSCLK/BAUDRATE0/2/48);
 239   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
 240   2            CKCON |=  0x02;
 241   2         }
C51 COMPILER V8.08   F34X_UART_MULTIUART                                                   07/01/2008 20:14:26 PAGE 5   

 242   1      
 243   1         TL1 = TH1;                          // init Timer1
 244   1         TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
 245   1         TMOD |=  0x20;                       
 246   1         TR1 = 1;                            // START Timer1
 247   1         TI0 = 1;                            // Indicate TX0 ready
 248   1      }
 249          
 250          //-----------------------------------------------------------------------------
 251          // UART1_Init
 252          //-----------------------------------------------------------------------------
 253          //
 254          // Return Value : None
 255          // Parameters   : None
 256          //
 257          // Configure UART1 for baudrate1 and 8-N-1.
 258          //
 259          //-----------------------------------------------------------------------------
 260          
 261          void UART1_Init (void)
 262          {
 263   1         SMOD1 = 0x0C;                       // set to disable parity, 8-data bit,
 264   1                                             // disable extra bit, 
 265   1                                             // stop bit 1 bit wide
 266   1      
 267   1         SCON1 = 0x10;                       // SCON1: 8-bit variable bit rate
 268   1                                             //        level of STOP bit is ignored
 269   1                                             //        RX enabled
 270   1                                             //        ninth bits are zeros
 271   1                                             //        clear RI0 and TI0 bits
 272   1      
 273   1         if (SYSCLK/BAUDRATE1/2/0xFFFF < 1) {
 274   2            SBRL1 = -(SYSCLK/BAUDRATE1/2);
 275   2            SBCON1 |= 0x03;                  // set prescaler to 1
 276   2         } else if (SYSCLK/BAUDRATE1/2/0xFFFF < 4) {
 277   2            SBRL1 = -(SYSCLK/BAUDRATE1/2/4);
 278   2            SBCON1 &= ~0x03;
 279   2            SBCON1 |= 0x01;                  // set prescaler to 4
 280   2      
 281   2         } else if (SYSCLK/BAUDRATE1/2/0xFFFF < 12) {
 282   2            SBRL1 = -(SYSCLK/BAUDRATE1/2/12);
 283   2            SBCON1 &= ~0x03;                 // set prescaler to 12
 284   2         } else {
 285   2            SBRL1 = -(SYSCLK/BAUDRATE1/2/48);
 286   2            SBCON1 &= ~0x03;
 287   2            SBCON1 |= 0x02;                  // set prescaler to 4
 288   2         }
 289   1      
 290   1         SCON1 |= 0x02;                      // indicate ready for TX
 291   1         SBCON1 |= 0x40;                     // enable baud rate generator
 292   1      }
 293          
 294          
 295          //-----------------------------------------------------------------------------
 296          // putchar
 297          //-----------------------------------------------------------------------------
 298          //
 299          // Return Value : UART0/1 buffer value
 300          // Parameters   : character to be transmitted across UART0/1
 301          //
 302          // This is an overloaded fuction found in the stdio library.  When the
 303          // function putchar is called, either by user code or through calls to stdio
C51 COMPILER V8.08   F34X_UART_MULTIUART                                                   07/01/2008 20:14:26 PAGE 6   

 304          // routines such as printf, the following routine will be executed instead 
 305          // of the function located in the stdio library.
 306          //
 307          // The function checks the UART global variable to determine which UART to 
 308          // use to receive a character.
 309          //
 310          // The routine expands '\n' to include a carriage return as well as a 
 311          // new line character by first checking to see whether the character  
 312          // passed into the routine equals '\n'.  If it is, the routine waits for 
 313          // TI0/TI1 to be set, indicating that UART 0/1 is ready to transmit another 
 314          // byte.  The routine then clears TI0/TI1 bit, and sets the UART0/1 output 
 315          // buffer to '0x0d', which is the ASCII character for carriage return.
 316          //
 317          // The routine the waits for TI0/TI1 to be set, clears TI0/TI1, and sets
 318          // the UART output buffer to <c>.  
 319          //
 320          //-----------------------------------------------------------------------------
 321          
 322          char putchar (char c)  {
 323   1      
 324   1         if (UART == 0) {
 325   2      
 326   2            if (c == '\n')  {                // check for newline character
 327   3               while (!TI0);                 // wait until UART0 is ready to transmit
 328   3               TI0 = 0;                      // clear interrupt flag
 329   3               SBUF0 = 0x0d;                 // output carriage return command
 330   3            }
 331   2            while (!TI0);                    // wait until UART0 is ready to transmit
 332   2            TI0 = 0;                         // clear interrupt flag
 333   2            return (SBUF0 = c);              // output <c> using UART 0
 334   2         }
 335   1      
 336   1         else if (UART == 1) {
 337   2            if (c == '\n')  {                // check for newline character
 338   3               while (!(SCON1 & 0x02));      // wait until UART1 is ready to transmit
 339   3               SCON1 &= ~0x02;               // clear TI1 interrupt flag
 340   3               SBUF1 = 0x0d;                 // output carriage return
 341   3            }
 342   2            while (!(SCON1 & 0x02));         // wait until UART1 is ready to transmit
 343   2            SCON1 &= ~0x02;                  // clear TI1 interrupt flag
 344   2            return (SBUF1 = c);              // output <c> using UART 1
 345   2         }
 346   1      }
 347          
 348          //-----------------------------------------------------------------------------
 349          // _getkey
 350          //-----------------------------------------------------------------------------
 351          //
 352          // Return Value : byte received from UART0/1
 353          // Parameters   : none
 354          
 355          // This is an overloaded fuction found in the stdio library.  When the
 356          // function _getkey is called, either by user code or through calls to stdio
 357          // routines such as scanf, the following routine will be executed instead 
 358          // of the function located in the stdio library.
 359          //
 360          // The function checks the UART global variable to determine which UART to 
 361          // use to receive a character.
 362          //
 363          // The routine waits for RI0/RI1 to be set, indicating that a byte has
 364          // been received across the UART0/UART1 RX line.  The routine saves the 
 365          // received character into a local variable, clears the RI0/RI1 interrupt
C51 COMPILER V8.08   F34X_UART_MULTIUART                                                   07/01/2008 20:14:26 PAGE 7   

 366          // flag, and returns the received character value.
 367          //
 368          //-----------------------------------------------------------------------------
 369          char _getkey ()  {
 370   1        char c;
 371   1      
 372   1        if (UART == 0) {
 373   2          while (!RI0);                      // wait until UART0 receives a character
 374   2          c = SBUF0;                         // save character to local variable
 375   2          RI0 = 0;                           // clear UART0 receive interrupt flag
 376   2          return (c);                        // return value received through UART0
 377   2        }
 378   1      
 379   1        else if (UART == 1) {
 380   2          while (!(SCON1 & 0x01));           // wait until UART1 receives a character
 381   2          c = SBUF1;                         // save character to local variable
 382   2          SCON1 &= ~0x01;                    // clear UART1 receive interrupt flag
 383   2          return (c);                        // return value received through UART1
 384   2        }
 385   1      }
 386          
 387          //-----------------------------------------------------------------------------
 388          // Delay
 389          //-----------------------------------------------------------------------------
 390          //
 391          // Return Value : none
 392          // Parameters   : none
 393          //
 394          // Used for a small pause of approximately 40 us.
 395          //
 396          //-----------------------------------------------------------------------------
 397          
 398          void Delay(void)
 399          {
 400   1         int x;
 401   1         for(x = 0;x < 500;x)
 402   1            x++;
 403   1      }
 404          
 405          //-----------------------------------------------------------------------------
 406          // End Of File
 407          //-----------------------------------------------------------------------------


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    261    ----
   CONSTANT SIZE    =     55    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----       1
   IDATA SIZE       =   ----    ----
   BIT SIZE         =      1    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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