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

📄 da0_da3.lst

📁 8051F020通其AN0
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.02   DA0_DA3                                                               07/14/2008 16:53:08 PAGE 1   


C51 COMPILER V8.02, COMPILATION OF MODULE DA0_DA3
OBJECT MODULE PLACED IN da0_da3.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE da0_da3.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          //-----------------------------------------------------------------------------
   2          // F02x_ADC0_ExternalInput_Mux.c
   3          //-----------------------------------------------------------------------------
   4          // Copyright 2005 Silicon Laboratories, Inc.
   5          // http://www.silabs.com
   6          //
   7          // This code example illustrates using the internal analog multiplexer to 
   8          // measure analog voltages on up to 8 different analog inputs.  Results are
   9          // printed to a PC terminal program via the UART.
  10          //
  11          // The inputs are sequentially scanned, beginning with input 0 (AIN0.0), up
  12          // to input number <ANALOG_INPUTS>-1 (maximum ANALOG_INPUTS = 8, which will
  13          // scan all analog inputs AIN0.0 - AIN0.7).
  14          //
  15          //
  16          // ADC Settling Time Requirements, Sampling Rate:
  17          // ----------------------------------------------
  18          //
  19          // The total sample time per intput is comprised of an input setting time 
  20          // (Tsettle), followed by a conversion time (Tconvert): 
  21          //
  22          // Tsample  = Tsettle + Tconvert
  23          //
  24          // Settling and conversion times may overlap, as the ADC holds the value once 
  25          // conversion begins.  This code example takes advantage of this to increase
  26          // the settling time above the minimum required.  In other words, when 
  27          // converting the value from analog input Ain(n), the input mux is switched 
  28          // over to the next input Ain(n+1) to begin settling.
  29          //
  30          // |--------Settling Ain(n)--------|=Conversion Ain(n)=| 
  31          //                                 |--------Settling Ain(n+1)--------|=Conversion Ain(n+1)=|
  32          //                                                                   |--------Settling Ain(n+2)--------|
  33          // ISR:  Timer 2                   ^                                 ^                                 ^
  34          // ISR:  ADC0                                          ^                                   ^
  35          //
  36          // The ADC input voltage must be allowed adequate time to settle before the 
  37          // conversion is made.  This settling depends on the external source
  38          // impedance, internal mux impedance, and internal capacitance.
  39          // Settling time is given by:
  40          //
  41          //                   | 2^n |
  42          //    Tsettle =   ln | --- | * Rtotal * Csample
  43          //                   | SA  |       
  44          //
  45          // In this application, assume a 100kohm potentiometer as the voltage divider.
  46          // The expression evaluates to:
  47          //
  48          //                   | 2^12 |
  49          //    Tsettle =   ln | ---- | * 105e3 * 10e-12 = 10.2uS
  50          //                   | 0.25 |    
  51          //
  52          // In addition, one must allow at least 1.5uS after changing analog mux 
  53          // inputs or PGA settings.  The settling time in this example, then, is 
  54          // dictated by the large external source resistance.
  55          //
C51 COMPILER V8.02   DA0_DA3                                                               07/14/2008 16:53:08 PAGE 2   

  56          // The conversion is 16 periods of the SAR clock <SAR_CLK>.  At 2.5 MHz, 
  57          // this time is 16 * 400nS = 6.4 uS.
  58          //
  59          // 
  60          // Tsample, minimum  = Tsettle + Tconvert
  61          //                   = 10.2uS + 6.4uS 
  62          //                   = 16.6 uS
  63          // 
  64          // Timer 2 is set to change the mux input and start a conversion every 20uS.
  65          //
  66          // General:
  67          // --------
  68          //
  69          // The system is clocked using the internal 24.5MHz oscillator multiplied
  70          // up to 49MHz by the on-chip PLL.  Results are printed to the UART from a loop
  71          // with the rate set by a delay based on Timer 2.  This loop periodically reads
  72          // the ADC value from a global array, Result.
  73          //
  74          // The ADC makes repeated measurements at 20uS intervals based on Timer 2.
  75          // The end of each ADC conversion initiates an interrupt which calls an 
  76          // averaging function.  <INT_DEC> samples are averaged then the Result 
  77          // values updated.
  78          //
  79          // For each power of 4 of <INT_DEC>, you gain 1 bit of effective resolution.
  80          // For example, <INT_DEC> = 256 gain you 4 bits of resolution: 4^4 = 256.
  81          // 
  82          // The ADC input multiplexer is set for a single-ended input.  The example
  83          // sequentially scans through the inputs, starting at AIN0.0.  <ANALOG_INPUTS> 
  84          // inputs are read.  The amplifier is set for unity gain so a voltage range of 
  85          // 0 to Vref (2.43V) may be measured.  Although voltages up to Vdd may be 
  86          // applied without damaging the device, only the range 0 to Vref may be 
  87          // measured by the ADC.  The input is available at the 8-position board-edge 
  88          // connector, J20, on the C8051FX20-TB.
  89          //
  90          // A 100kohm potentiometer may be connected as a voltage divider between 
  91          // VREF and AGND as shown below:
  92          //
  93          // ---------
  94          //          |        
  95          //       8 o| AGND ----| 
  96          //         o| VREF ----|<-|
  97          //         o| AIN0.1   |  |
  98          //         o|    |        |
  99          //         o|     -------- 
 100          //         o|
 101          //         o|
 102          //       1 o|
 103          //          |
 104          // ---------   
 105          //
 106          // How To Test:
 107          //
 108          // 1) Download code to a 'F02x device that is connected to a UART transceiver
 109          // 2) Connect serial cable from the transceiver to a PC
 110          // 3) On the PC, open HyperTerminal (or any other terminal program) and connect
 111          //    to the COM port at <BAUDRATE> and 8-N-1
 112          // 4) Connect a variable voltage source (between 0 and Vref) 
 113          //    to AIN 0.1, or a potentiometer voltage divider as shown above.
 114          // 5) HyperTerminal will print the voltages measured by the device if
 115          //    everything is working properly.  Note that some of the analog inputs are
 116          //    floating and will return nonzero values.
 117          //
C51 COMPILER V8.02   DA0_DA3                                                               07/14/2008 16:53:08 PAGE 3   

 118          // FID:            02X000016
 119          // Target:         C8051F02x
 120          // Tool chain:     Keil C51 7.50 / Keil EVAL C51
 121          // Command Line:   None
 122          //
 123          //
 124          // Release 1.0
 125          //    -Initial Revision (PD)
 126          //    -18-Jul-06
 127          //
 128          //-----------------------------------------------------------------------------
 129          // Includes
 130          //-----------------------------------------------------------------------------
 131          
 132          #include <c8051f020.h>                 // SFR declarations
 133          #include <stdio.h>                     
 134          
 135          //-----------------------------------------------------------------------------
 136          // 16-bit SFR Definitions for 'F02x
 137          //-----------------------------------------------------------------------------
 138          
 139          sfr16 ADC0     = 0xbe;                 // ADC0 data
 140          sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
 141          sfr16 RCAP3    = 0x92;                 // Timer3 capture/reload
 142          sfr16 TMR2     = 0xcc;                 // Timer2
 143          sfr16 TMR3     = 0x94;                 // Timer3
 144          
 145          //-----------------------------------------------------------------------------
 146          // Global Constants
 147          //-----------------------------------------------------------------------------
 148          
 149          #define BAUDRATE     115200            // Baud rate of UART in bps
 150          #define SYSCLK       22118400          // Output of PLL derived from (INTCLK*2)
 151          #define INT_DEC      256               // Integrate and decimate ratio
 152          #define SAR_CLK      2500000           // Desired SAR clock speed
 153          
 154          #define SAMPLE_DELAY 250               // Delay in ms before displaying sample
 155          
 156          #define ANALOG_INPUTS 8                // Number of AIN pins to measure 
 157                                                 // (min=1, max=8)
 158          
 159          sbit LED = P1^6;                       // LED='1' means ON
 160          sbit SW1 = P3^7;                       // SW1='0' means switch pressed
 161          
 162          //-----------------------------------------------------------------------------
 163          // Function Prototypes
 164          //-----------------------------------------------------------------------------
 165          
 166          void OSCILLATOR_Init (void);           
 167          void PORT_Init (void);
 168          void UART0_Init (void);
 169          void ADC0_Init (void);
 170          void TIMER3_Init (void);
 171          void ADC0_ISR (void);
 172          void TIMER2_ISR (void);
 173          void Wait_MS (unsigned int ms);
 174          
 175          //-----------------------------------------------------------------------------
 176          // Global Variables
 177          //-----------------------------------------------------------------------------
 178          
 179          long Result[ANALOG_INPUTS];            // ADC0 decimated value, one for each 
C51 COMPILER V8.02   DA0_DA3                                                               07/14/2008 16:53:08 PAGE 4   

 180                                                 // analog input
 181          unsigned char amux_input=0;            // index of analog MUX inputs
 182          unsigned char amux_convert=0;
 183          
 184          //-----------------------------------------------------------------------------
 185          // main() Routine
 186          //-----------------------------------------------------------------------------
 187          
 188          void main (void)
 189          {
 190   1         unsigned char i;
 191   1         long measurement;                   // measured voltage in mV
 192   1      
 193   1         WDTCN = 0xde;                       // Disable watchdog timer
 194   1         WDTCN = 0xad;
 195   1      
 196   1         OSCILLATOR_Init ();                 // Initialize oscillator
 197   1         PORT_Init ();                       // Initialize crossbar and GPIO
 198   1         UART0_Init ();                      // Initialize UART1
 199   1      
 200   1         TIMER3_Init ();                     // Initialize Timer2 to overflow at 1 mS
 201   1      
 202   1         ADC0_Init ();                       // Init ADC
 203   1      
 204   1         AD0EN = 1;                          // Enable ADC
 205   1      
 206   1         EA = 1;                             // Enable global interrupts
 207   1      
 208   1         while (1)
 209   1         {
 210   2            EA = 0;                          // Disable interrupts
 211   2      
 212   2            printf("\f");
 213   2            for(i=0; i<ANALOG_INPUTS; i++)
 214   2               {
 215   3               // The 12-bit ADC value is averaged across INT_DEC measurements.  
 216   3               // The result is then stored in Result, and is right-justified 
 217   3               // The measured voltage applied to AIN 0.1 is then:
 218   3               //
 219   3               //                           Vref (mV)
 220   3               //   measurement (mV) =   --------------- * Result (bits) 
 221   3               //                       (2^12)-1 (bits)
 222   3      
 223   3               measurement =  Result[i] * 2430 / 4095;
 224   3               printf("AIN0.%bu voltage: %ld\tmV\n",i,measurement);
 225   3               }
 226   2            EA = 1;                          // Re-enable interrupts
 227   2            Wait_MS(SAMPLE_DELAY);           // Wait before displaying new values
 228   2         }
 229   1      }
 230          
 231          //-----------------------------------------------------------------------------
 232          // Initialization Subroutines
 233          //-----------------------------------------------------------------------------
 234          
 235          //-----------------------------------------------------------------------------
 236          // OSCILLATOR_Init
 237          //-----------------------------------------------------------------------------
 238          //
 239          // Return Value : None
 240          // Parameters   : None
 241          //
C51 COMPILER V8.02   DA0_DA3                                                               07/14/2008 16:53:08 PAGE 5   

 242          // This routine initializes the system clock to use an 22.1184MHz crystal
 243          // as its clock source.
 244          //
 245          //
 246          //-----------------------------------------------------------------------------
 247          void OSCILLATOR_Init (void)
 248          {
 249   1         int i;                              // delay counter
 250   1      
 251   1         OSCXCN = 0x67;                      // start external oscillator with
 252   1                                             // 22.1184MHz crystal
 253   1      
 254   1         for (i=0; i < 256; i++) ;           // wait for oscillator to start
 255   1      
 256   1         while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle
 257   1      
 258   1         OSCICN = 0x88;                      // select external oscillator as SYSCLK
 259   1                                             // source and enable missing clock
 260   1                                             // detector

⌨️ 快捷键说明

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