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

📄 f31x_adc0_externalinput.lst

📁 C8051F31系列单片机的例子
💻 LST
📖 第 1 页 / 共 2 页
字号:
 220   1         Timer2_Init();                      // Init Timer2 to generate 
 221   1                                             // overflows to trigger ADC
 222   1         UART0_Init();                       // Initialize UART0 for printf's
 223   1         ADC0_Init();                        // Initialize ADC0
 224   1      
 225   1         EA = 1;                                                               // enable global interrupts
 226   1         while (1) {                         // spin forever
 227   2         ADC_Read_Ch(0x0C);
 228   2         }
 229   1      }
 230          
 231          //-----------------------------------------------------------------------------
 232          // Initialization Subroutines
 233          //-----------------------------------------------------------------------------
 234          //
 235          //-----------------------------------------------------------------------------
 236          // SYSCLK_Init
 237          //-----------------------------------------------------------------------------
 238          // 
 239          // Return Value:  None
 240          // Parameters:    None
 241          //
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT                                               03/20/2009 15:55:34 PAGE 5   

 242          // This routine initializes the system clock to use the internal 24.5MHz 
 243          // oscillator as its clock source.  Also enables missing clock detector reset.
 244          //
 245          //-----------------------------------------------------------------------------
 246          
 247          void SYSCLK_Init (void)
 248          {
 249   1         OSCICN = 0x83;                      // configure internal oscillator for
 250   1                                             // 24.5MHz / 1
 251   1         RSTSRC = 0x04;                      // enable missing clock detector
 252   1      }
 253          
 254          //-----------------------------------------------------------------------------
 255          // PORT_Init
 256          //-----------------------------------------------------------------------------
 257          // 
 258          // Return Value:  None
 259          // Parameters:    None
 260          //
 261          // Configure the Crossbar and GPIO ports.
 262          // P0.0 - VREF input / output
 263          // P0.4 - UART TX (push-pull)
 264          // P0.5 - UART RX 
 265          // P2.5 - ADC0 analog input
 266          // P3.3 - LED (push-pull)
 267          // 
 268          //-----------------------------------------------------------------------------
 269          
 270          void PORT_Init (void)
 271          {
 272   1         XBR0     = 0x01;                    // Enable UART0
 273   1         XBR1     = 0x40;                    // Enable crossbar and weak pull-ups
 274   1         P0MDOUT |= 0x11;                    // Set P0.1, TX pins to push-pull
 275   1         P0SKIP   = 0x01;                    // force crossbar to skip P0.0
 276   1         P3MDOUT |= 0x08;                    // enable LED as a push-pull output
 277   1         P2MDIN &= ~0x20;                    // set P2.5 as an analog input
 278   1         P3MDIN = 0xFF;
 279   1         P0 = 0x01;                          // drive P0.0 high to provide Vdd at
 280   1                                             // header for external voltage divider
 281   1      }
 282          
 283          //-----------------------------------------------------------------------------
 284          // Timer2_Init
 285          //-----------------------------------------------------------------------------
 286          // 
 287          // Return Value:  None
 288          // Parameters:    None
 289          //
 290          // Configure Timer2 to 16-bit auto-reload and generate an interrupt at 100uS 
 291          // intervals.  Timer 2 overflow automatically triggers ADC0 conversion.
 292          // 
 293          //-----------------------------------------------------------------------------
 294          
 295          void Timer2_Init (void)
 296          {
 297   1         TMR2CN  = 0x00;                     // Stop Timer2; Clear TF2;
 298   1                                             // use SYSCLK as timebase, 16-bit 
 299   1                                             // auto-reload
 300   1         CKCON  |= 0x10;                     // select SYSCLK for timer 2 source
 301   1         TMR2RL  = 65535 - (SYSCLK / 10000); // init reload value for 10uS
 302   1         TMR2    = 0xffff;                   // set to reload immediately
 303   1         TR2     = 1;                        // start Timer2
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT                                               03/20/2009 15:55:34 PAGE 6   

 304   1      }
 305          
 306          //-----------------------------------------------------------------------------
 307          // ADC0_Init
 308          //-----------------------------------------------------------------------------
 309          // 
 310          // Return Value:  None
 311          // Parameters:    None
 312          //
 313          // Configures ADC0 to make single-ended analog measurements on pin P2.5
 314          //  
 315          //-----------------------------------------------------------------------------
 316          void ADC0_Init (void)
 317          {
 318   1         ADC0CN = 0x02;                      // ADC0 disabled, normal tracking, 
 319   1                                             // conversion triggered on TMR2 overflow
 320   1      
 321   1         REF0CN = 0x0E;                      // Enable on-chip VREF, select Vdd as 
 322   1                                             // voltage reference. 
 323   1      
 324   1         AMX0P = 0x11;                       // ADC0 positive input = P2.5
 325   1         AMX0N = 0x1F;                       // ADC0 negative input = GND
 326   1                                             // i.e., single ended mode
 327   1      
 328   1         ADC0CF = ((SYSCLK/3000000)-1)<<3;   // set SAR clock to 3MHz
 329   1      
 330   1         ADC0CF |= 0x00;                     // right-justify results 
 331   1      
 332   1         EIE1 |= 0x08;                       // enable ADC0 conversion complete int.
 333   1      
 334   1         AD0EN = 1;                          // enable ADC0
 335   1      }
 336          
 337          //-----------------------------------------------------------------------------
 338          // UART0_Init
 339          //-----------------------------------------------------------------------------
 340          // 
 341          // Return Value:  None
 342          // Parameters:    None
 343          //
 344          // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
 345          //
 346          //-----------------------------------------------------------------------------
 347          void UART0_Init (void)
 348          {
 349   1         SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
 350   1                                             //        level of STOP bit is ignored
 351   1                                             //        RX enabled
 352   1                                             //        ninth bits are zeros
 353   1                                             //        clear RI0 and TI0 bits
 354   1         if (SYSCLK/BAUDRATE/2/256 < 1) {
 355   2            TH1 = -(SYSCLK/BAUDRATE/2);
 356   2            CKCON |=  0x08;                  // T1M = 1; SCA1:0 = xx
 357   2         } else if (SYSCLK/BAUDRATE/2/256 < 4) {
 358   2            TH1 = -(SYSCLK/BAUDRATE/2/4);
 359   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01
 360   2            CKCON |=  0x01;
 361   2         } else if (SYSCLK/BAUDRATE/2/256 < 12) {
 362   2            TH1 = -(SYSCLK/BAUDRATE/2/12);
 363   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
 364   2         } else if (SYSCLK/BAUDRATE/2/256 < 48) {
 365   2            TH1 = -(SYSCLK/BAUDRATE/2/48);
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT                                               03/20/2009 15:55:34 PAGE 7   

 366   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
 367   2            CKCON |=  0x02;
 368   2         } else {
 369   2            while (1);                       // Error.  Unsupported baud rate
 370   2         }
 371   1      
 372   1         TL1 = TH1;                          // init Timer1
 373   1         TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
 374   1         TMOD |=  0x20;
 375   1         TR1 = 1;                            // START Timer1
 376   1         TI0 = 1;                            // Indicate TX0 ready
 377   1      }
 378          
 379          //-----------------------------------------------------------------------------
 380          // Interrupt Service Routines
 381          //-----------------------------------------------------------------------------
 382          //
 383          //-----------------------------------------------------------------------------
 384          // ADC0_ISR
 385          //-----------------------------------------------------------------------------
 386          // 
 387          // Return Value:  None
 388          // Parameters:    None
 389          //
 390          // This ISR averages 2048 samples then prints the result to the terminal.  The 
 391          // ISR is called after each ADC conversion which is triggered by Timer2.
 392          //
 393          //-----------------------------------------------------------------------------
 394          void ADC0_ISR (void) interrupt 10
 395          {
 396   1      
 397   1         static unsigned long accumulator = 0;     // accumulator for averaging
 398   1         static unsigned int measurements = 2048;  // measurement counter
 399   1         unsigned long result=0;
 400   1         unsigned long mV;                   // measured voltage in mV
 401   1      
 402   1         AD0INT = 0;                         // clear ADC0 conversion complete flag
 403   1      
 404   1         accumulator += ADC0;
 405   1         measurements--;
 406   1      
 407   1         if(measurements == 0)
 408   1         {  
 409   2            measurements = 2048;
 410   2            result = accumulator / 2048;
 411   2            accumulator=0;
 412   2      
 413   2            // The 10-bit ADC value is averaged across 2048 measurements.  
 414   2            // The measured voltage applied to P1.4 is then:
 415   2            //
 416   2            //                           Vref (mV)
 417   2            //   measurement (mV) =   --------------- * result (bits) 
 418   2            //                       (2^10)-1 (bits)
 419   2      
 420   2            mV =  result * 3250 / 1023;   
 421   2            printf("P3.1 voltage: %ld mV\n",mV);
 422   2         }
 423   1         
 424   1         LED=~LED;
 425   1      }
 426          
 427          //-----------------------------------------------------------------------------
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT                                               03/20/2009 15:55:34 PAGE 8   

 428          // End Of File
 429          //-----------------------------------------------------------------------------


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    558    ----
   CONSTANT SIZE    =     22    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     12       8
   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 + -