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

📄 f320_hidtouart.lst

📁 hid to rs232 usb_hid 转 串口程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
 170   1      
 171   1      #ifndef BAUDRATE_HARDCODED
 172   1         BaudRate = 230600;
 173   1      #endif
 174   1         UART0_Init();
 175   1      
 176   1      }
 177          
 178          //-----------------------------------------------------------------------------
 179          // USB_Init
C51 COMPILER V8.17   F320_HIDTOUART                                                        03/18/2009 09:55:18 PAGE 4   

 180          //-----------------------------------------------------------------------------
 181          // USB Initialization performs the following:
 182          // - Initialize USB0
 183          // - Enable USB0 interrupts
 184          // - Enable USB0 transceiver
 185          // - Enable USB0 with suspend detection
 186          //
 187          //-----------------------------------------------------------------------------
 188          void USB_Init (void)
 189          {
 190   1      
 191   1         POLL_WRITE_BYTE (POWER,  0x08);     // Force Asynchronous USB Reset
 192   1         POLL_WRITE_BYTE (IN1IE,  0x07);     // Enable Endpoint 0-1 in interrupts
 193   1         POLL_WRITE_BYTE (OUT1IE, 0x07);     // Enable Endpoint 0-1 out interrupts
 194   1         POLL_WRITE_BYTE (CMIE,   0x07);     // Enable Reset, Resume, and Suspend
 195   1                                             // interrupts
 196   1      
 197   1         USB0XCN = 0xE0;                     // Enable transceiver; select full speed
 198   1         POLL_WRITE_BYTE (CLKREC, 0x80);     // Enable clock recovery, single-step
 199   1                                             // mode disabled
 200   1      
 201   1         EIE1 |= 0x02;                       // Enable USB0 Interrupts
 202   1      
 203   1                                             // Enable USB0 by clearing the USB
 204   1         POLL_WRITE_BYTE (POWER, 0x01);      // Inhibit Bit and enable suspend
 205   1                                             // detection
 206   1      
 207   1      }
 208          
 209          
 210          //-----------------------------------------------------------------------------
 211          // Sysclk_Init(void)
 212          //-----------------------------------------------------------------------------
 213          // This function initializes the system clock and the USB clock.
 214          //
 215          //-----------------------------------------------------------------------------
 216          void Sysclk_Init(void)
 217          {
 218   1      #ifdef _USB_LOW_SPEED_
              
                 OSCICN |= 0x03;                     // Configure internal oscillator for
                                                     // its maximum frequency and enable
                                                     // missing clock detector
              
                 CLKSEL  = SYS_INT_OSC;              // Select System clock
                 CLKSEL |= USB_INT_OSC_DIV_2;        // Select USB clock
              #else
 227   1         OSCICN |= 0x03;                     // Configure internal oscillator for
 228   1                                             // its maximum frequency and enable
 229   1                                             // missing clock detector
 230   1      
 231   1         CLKMUL  = 0x00;                     // Select internal oscillator as
 232   1                                             // input to clock multiplier
 233   1      
 234   1         CLKMUL |= 0x80;                     // Enable clock multiplier
 235   1         Delay();                            // Delay for clock multiplier to begin
 236   1         CLKMUL |= 0xC0;                     // Initialize the clock multiplier
 237   1         Delay();                            // Delay for clock multiplier to begin
 238   1      
 239   1         while(!(CLKMUL & 0x20));            // Wait for multiplier to lock
 240   1         CLKSEL  = 0x02;                     // Set sys clock to clkmul * 2
 241   1      #endif  // _USB_LOW_SPEED_
C51 COMPILER V8.17   F320_HIDTOUART                                                        03/18/2009 09:55:18 PAGE 5   

 242   1      }
 243          
 244          //-----------------------------------------------------------------------------
 245          // Port_Init(void)
 246          //-----------------------------------------------------------------------------
 247          // Port Initialization routine that configures the Crossbar and GPIO ports.
 248          //
 249          //-----------------------------------------------------------------------------
 250          void Port_Init (void)
 251          {
 252   1         P0MDOUT |= 0x10;                    // Port 0 pins 0-3 set high impedence
 253   1         XBR0     = 0x01;                    // Enable UART output
 254   1         XBR1     = 0x40;                    // Enable Crossbar, 1 PCA channel
 255   1         P0SKIP = 0xCF;
 256   1         P1SKIP = 0xFF;
 257   1         P2SKIP = 0x07;
 258   1         P2MDOUT = 0x0F;                     // DEBUG
 259   1      }
 260          
 261          
 262          //-----------------------------------------------------------------------------
 263          // UART0_Init
 264          //-----------------------------------------------------------------------------
 265          //
 266          // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
 267          //
 268          
 269          //#ifndef BAUDRATE_HARDCODED
 270          void UART0_Init (void)
 271          {
 272   1         SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
 273   1                                             //        level of STOP bit is ignored
 274   1                                             //        RX enabled
 275   1                                             //        ninth bits are zeros
 276   1                                             //        clear RI0 and TI0 bits
 277   1         if (SYSTEMCLOCK/BaudRate/2/256 < 1) {
 278   2            TH1 = -(SYSTEMCLOCK/BaudRate/2);
 279   2            CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
 280   2            CKCON |=  0x08;
 281   2         } else if (SYSTEMCLOCK/BaudRate/2/256 < 4) {
 282   2            TH1 = -(SYSTEMCLOCK/BaudRate/2/4);
 283   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01                  
 284   2            CKCON |=  0x09;
 285   2         } else if (SYSTEMCLOCK/BaudRate/2/256 < 12) {
 286   2            TH1 = -(SYSTEMCLOCK/BaudRate/2/12);
 287   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
 288   2         } else {
 289   2            TH1 = -(SYSTEMCLOCK/BaudRate/2/48);
 290   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
 291   2            CKCON |=  0x02;
 292   2         }
 293   1      
 294   1         TL1 = TH1;                          // init Timer1
 295   1         TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
 296   1         TMOD |=  0x20;                       
 297   1         TR1 = 1;                            // START Timer1
 298   1         TX_Ready = 1;                       // Flag showing that UART can transmit
 299   1         IP |= 0x10;                         // Make UART high priority
 300   1         ES0 = 1;                            // Enable UART0 interrupts
 301   1      
 302   1      
 303   1         // The following code computes the size the UART_INPUT buffer, which 
C51 COMPILER V8.17   F320_HIDTOUART                                                        03/18/2009 09:55:18 PAGE 6   

 304   1         // stores bytes received from the USB, can reach before one more
 305   1         // USB packet will overflow the buffer.  
 306   1         // This is to guard against the case where the UART is transmitting
 307   1         // bytes out at a speed that is much lower than the speed at which
 308   1         // bytes are received.
 309   1         UART_OUTPUT_OVERFLOW_BOUNDARY = UART_OUTPUT_BUFFERSIZE - OUT_DATA_SIZE;
 310   1      
 311   1      }
 312          
 313          //-----------------------------------------------------------------------------
 314          // Helper Functions
 315          //-----------------------------------------------------------------------------
 316          
 317          //-----------------------------------------------------------------------------
 318          // Delay(void)
 319          //-----------------------------------------------------------------------------
 320          // Used for a small pause, approximately 80 us in Full Speed,
 321          // and 1 ms when clock is configured for Low Speed
 322          //
 323          //-----------------------------------------------------------------------------
 324          void Delay (void)
 325          {
 326   1         int x;
 327   1         for(x = 0;x < 500;x)
 328   1            x++;
 329   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    581    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =    384    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     13    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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