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

📄 f31x_adc0_externalinput_mux.lst

📁 C8051F31系列单片机的例子
💻 LST
📖 第 1 页 / 共 2 页
字号:
 255          //
 256          // P1.0 - analog input (ADC0)
 257          // P1.1 - analog input (ADC0)
 258          // P1.2 - analog input (ADC0)
 259          // P1.3 - analog input (ADC0)
 260          // P1.4 - analog input (ADC0)
 261          // P1.5 - analog input (ADC0)
 262          //
 263          //-----------------------------------------------------------------------------
 264          void Port_Init (void)
 265          {
 266   1         P1SKIP = 0x3F;                      // Skip all analog pins
 267   1      
 268   1         XBR0 = 0x01;                        // UART0 TX and RX pins enabled
 269   1         XBR1 = 0x40;                        // Enable crossbar and weak pull-ups
 270   1      
 271   1         P0MDOUT |= 0x10;                    // Enable TX0 as a push-pull output
 272   1      
 273   1         P1MDIN &= ~0x3F;                    // Set desired pins as analog inputs
 274   1      }
 275          
 276          //-----------------------------------------------------------------------------
 277          // Timer2_Init
 278          //-----------------------------------------------------------------------------
 279          //
 280          // Return Value : None
 281          // Parameters   : None
 282          //
 283          // Configure Timer2 to 16-bit auto-reload and generate an interrupt at 10 us
 284          // intervals.  Timer2 overflows automatically triggers ADC0 conversion.
 285          //
 286          //-----------------------------------------------------------------------------
 287          void Timer2_Init (void)
 288          {
 289   1         TMR2CN = 0x00;                      // Stop Timer2; Clear TF2;
 290   1                                             // use SYSCLK as timebase, 16-bit
 291   1                                             // auto-reload
 292   1         CKCON |= 0x10;                      // Select SYSCLK for timer 2 source
 293   1         TMR2RL = 65535 - (SYSCLK / 10000);  // Init reload value for 10 us
 294   1         TMR2 = 0xffff;                      // Set to reload immediately
 295   1         ET2 = 1;                            // Enable Timer2 interrupts
 296   1         TR2 = 1;                            // Start Timer2
 297   1      }
 298          
 299          //-----------------------------------------------------------------------------
 300          // ADC0_Init
 301          //-----------------------------------------------------------------------------
 302          //
 303          // Return Value : None
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 6   

 304          // Parameters   : None
 305          //
 306          // Configures ADC0 to make single-ended analog measurements on Port 1 according
 307          // to the values of <ANALOG_INPUTS> and <PIN_TABLE>.
 308          //
 309          //-----------------------------------------------------------------------------
 310          void ADC0_Init (void)
 311          {
 312   1         ADC0CN = 0x02;                      // ADC0 disabled, normal tracking,
 313   1                                             // conversion triggered on TMR2 overflow
 314   1      
 315   1         REF0CN = 0x0A;                      // Enable VREF
 316   1      
 317   1         AMX0P = PIN_TABLE[0];               // ADC0 initial positive input = P1.0
 318   1         AMX0N = 0x1F;                       // ADC0 negative input = GND
 319   1                                             // i.e., single ended mode
 320   1      
 321   1         ADC0CF = ((SYSCLK/3000000)-1)<<3;   // Set SAR clock to 3MHz
 322   1      
 323   1         ADC0CF |= 0x00;                     // Right-justify results
 324   1      
 325   1         EIE1 |= 0x08;                       // Enable ADC0 EOC interrupt
 326   1      
 327   1         AD0EN = 1;                          // Enable ADC0
 328   1      }
 329          
 330          //-----------------------------------------------------------------------------
 331          // UART0_Init
 332          //-----------------------------------------------------------------------------
 333          //
 334          // Return Value : None
 335          // Parameters   : None
 336          //
 337          // Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
 338          //
 339          //-----------------------------------------------------------------------------
 340          void UART0_Init (void)
 341          {
 342   1         SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
 343   1                                             //        level of STOP bit is ignored
 344   1                                             //        RX enabled
 345   1                                             //        ninth bits are zeros
 346   1                                             //        clear RI0 and TI0 bits
 347   1         if (SYSCLK/BAUDRATE/2/256 < 1) {
 348   2            TH1 = -(SYSCLK/BAUDRATE/2);
 349   2            CKCON |=  0x08;                  // T1M = 1; SCA1:0 = xx
 350   2         } else if (SYSCLK/BAUDRATE/2/256 < 4) {
 351   2            TH1 = -(SYSCLK/BAUDRATE/2/4);
 352   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01
 353   2            CKCON |=  0x01;
 354   2         } else if (SYSCLK/BAUDRATE/2/256 < 12) {
 355   2            TH1 = -(SYSCLK/BAUDRATE/2/12);
 356   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
 357   2         } else if (SYSCLK/BAUDRATE/2/256 < 48) {
 358   2            TH1 = -(SYSCLK/BAUDRATE/2/48);
 359   2            CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
 360   2            CKCON |=  0x02;
 361   2         } else {
 362   2            while (1);                       // Error.  Unsupported baud rate
 363   2         }
 364   1      
 365   1         TL1 = TH1;                          // Init Timer1
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 7   

 366   1         TMOD &= ~0xF0;                      // TMOD: timer 1 in 8-bit autoreload
 367   1         TMOD |=  0x20;
 368   1         TR1 = 1;                            // START Timer1
 369   1         TI0 = 1;                            // Indicate TX0 ready
 370   1      }
 371          
 372          //-----------------------------------------------------------------------------
 373          // Interrupt Service Routines
 374          //-----------------------------------------------------------------------------
 375          
 376          //-----------------------------------------------------------------------------
 377          // Timer2_ISR
 378          //-----------------------------------------------------------------------------
 379          //
 380          // This routine changes to the next Analog MUX input whenever Timer2 overflows
 381          // for the next ADC sample.  This allows the ADC to begin setting on the new
 382          // input while converting the old input.
 383          //
 384          //-----------------------------------------------------------------------------
 385          void Timer2_ISR (void) interrupt 5
 386          {
 387   1         TF2H = 0;                           // Clear Timer2 interrupt flag
 388   1      
 389   1         // Set up the AMUX for the next ADC input
 390   1         // ADC0 positive input = P1.<PIN_TABLE[AMUX_INPUT+1]>
 391   1         // ADC0 negative input = GND
 392   1         // i.e., single ended mode
 393   1         if (AMUX_INPUT == (ANALOG_INPUTS - 1))
 394   1         {
 395   2            AMX0P = PIN_TABLE[0];
 396   2         }
 397   1         else
 398   1         {
 399   2            AMX0P = PIN_TABLE[AMUX_INPUT+1];
 400   2         }
 401   1      }
 402          
 403          //-----------------------------------------------------------------------------
 404          // ADC0_ISR
 405          //-----------------------------------------------------------------------------
 406          //
 407          // This ISR averages <INT_DEC> samples for each analog MUX input then prints
 408          // the results to the terminal.  The ISR is called after each ADC conversion,
 409          // which is triggered by Timer2.
 410          //
 411          //-----------------------------------------------------------------------------
 412          void ADC0_ISR (void) interrupt 10
 413          {
 414   1         static unsigned int_dec = INT_DEC;  // Integrate/decimate counter
 415   1                                             // A new result is posted when
 416   1                                             // int_dec is 0
 417   1      
 418   1         // Integrate accumulator for the ADC samples from input pins
 419   1         static long accumulator[ANALOG_INPUTS] = 0x00000000;
 420   1      
 421   1         unsigned char i;                    // Loop counter
 422   1      
 423   1      
 424   1         AD0INT = 0;                         // Clear ADC conversion complete
 425   1                                             // overflow
 426   1      
 427   1      
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 8   

 428   1         accumulator[AMUX_INPUT] += ADC0;    // Read the ADC value and add it to the
 429   1                                             // running total
 430   1      
 431   1         // Reset sample counter <int_dec> and <AMUX_INPUT> if the final input was
 432   1         // just read
 433   1         if(AMUX_INPUT == (ANALOG_INPUTS - 1))
 434   1         {
 435   2            int_dec--;                       // Update decimation counter
 436   2                                             // when the last of the analog inputs
 437   2                                             // is sampled
 438   2      
 439   2            if (int_dec == 0)                // If zero, then post the averaged
 440   2            {                                // results
 441   3               int_dec = INT_DEC;            // Reset counter
 442   3      
 443   3               // Copy each averaged ADC0 value into the RESULT array
 444   3               for(i = 0; i < ANALOG_INPUTS; i++)
 445   3               {
 446   4                  // Copy averaged values into RESULT
 447   4                  RESULT[i] = accumulator[i] / int_dec;
 448   4      
 449   4                  // Reset accumulators
 450   4                  accumulator[i] = 0x00000000;
 451   4               }
 452   3            }
 453   2      
 454   2            AMUX_INPUT = 0;                  // Reset input index back to P1.0
 455   2         }
 456   1         // Otherwise, increment the AMUX channel counter
 457   1         else
 458   1         {
 459   2            AMUX_INPUT++;                    // Step to the next analog mux input
 460   2         }
 461   1      }
 462          
 463          //-----------------------------------------------------------------------------
 464          // Support Subroutines
 465          //-----------------------------------------------------------------------------
 466          
 467          //-----------------------------------------------------------------------------
 468          // Timer0_Init
 469          //-----------------------------------------------------------------------------
 470          //
 471          // Return Value : None
 472          // Parameters   :
 473          //   1) int ms - number of milliseconds to wait
 474          //                        range is positive range of an int: 0 to 32767
 475          //
 476          // This function configures the Timer0 as a 16-bit timer, interrupt enabled.
 477          // Using the internal osc. at 24.5MHz with a prescaler of 1:8 and reloading the
 478          // T0 registers with TIMER0_RELOAD_HIGH/LOW, it will wait for <ms>
 479          // milliseconds.
 480          // Note: The Timer0 uses a 1:12 prescaler
 481          //-----------------------------------------------------------------------------
 482          void Timer0_wait(int ms)
 483          {
 484   1         TH0 = TIMER0_RELOAD_HIGH;           // Init Timer0 High register
 485   1         TL0 = TIMER0_RELOAD_LOW ;           // Init Timer0 Low register
 486   1         TMOD |= 0x01;                       // Timer0 in 16-bit mode
 487   1         CKCON &= 0xFC;                      // Timer0 uses a 1:12 prescaler
 488   1         TR0  = 1;                           // Timer0 ON
 489   1      
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 9   

 490   1         while(ms)
 491   1         {
 492   2            TF0 = 0;                         // Clear flag to initialize
 493   2            while(!TF0);                     // Wait until timer overflows
 494   2            ms--;                            // Decrement ms
 495   2         }
 496   1      
 497   1         TR0 = 0;                            // Timer0 OFF
 498   1      }
 499          
 500          //-----------------------------------------------------------------------------
 501          // End Of File
 502          //-----------------------------------------------------------------------------


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    498    ----
   CONSTANT SIZE    =     27    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     51       6
   IDATA SIZE       =      6    ----
   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 + -