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

📄 f06x_adc2_externalinput_mux.lst

📁 // This program measures the voltage on an external ADC input and prints the // result to a termin
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V8.08   F06X_ADC2_EXTERNALINPUT_MUX                                           02/14/2008 15:32:09 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE F06X_ADC2_EXTERNALINPUT_MUX
OBJECT MODULE PLACED IN F06x_ADC2_ExternalInput_Mux.OBJ
COMPILER INVOKED BY: C:\SiLabs\MCU\IDEfiles\C51\BIN\C51.exe F06x_ADC2_ExternalInput_Mux.c DB OE

line level    source

   1          //-----------------------------------------------------------------------------
   2          // F06x_ADC2_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 (AIN2.0), up
  12          // to input number <ANALOG_INPUTS>-1 (maximum ANALOG_INPUTS = 8, which will
  13          // scan all analog inputs AIN2.0 - AIN2.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:  ADC2                                          ^                                   ^
  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.08   F06X_ADC2_EXTERNALINPUT_MUX                                           02/14/2008 15:32:09 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. Results are
  70          // printed to the UART from a loop with the rate set by a delay based on Timer 2.
  71          // This loop periodically reads the ADC value from a global array, Result.
  72          //
  73          // The ADC makes repeated measurements at 20uS intervals based on Timer 2.
  74          // The end of each ADC conversion initiates an interrupt which calls an
  75          // averaging function.  <INT_DEC> samples are averaged then the Result
  76          // values updated.
  77          //
  78          // For each power of 4 of <INT_DEC>, you gain 1 bit of effective resolution.
  79          // For example, <INT_DEC> = 256 gain you 4 bits of resolution: 4^4 = 256.
  80          //
  81          // The ADC input multiplexer is set for a single-ended input.  The example
  82          // sequentially scans through the inputs, starting at AIN0.0.  <ANALOG_INPUTS>
  83          // inputs are read.  The amplifier is set for unity gain so a voltage range of
  84          // 0 to Vref (2.43V) may be measured.  Although voltages up to Vdd may be
  85          // applied without damaging the device, only the range 0 to Vref may be
  86          // measured by the ADC.  The input is available at the 8-position board-edge
  87          // connector, J20, on the C8051FX20-TB.
  88          //
  89          // A 100kohm potentiometer may be connected as a voltage divider between
  90          // VREF and AGND as shown below:
  91          //
  92          // ---------
  93          //          |
  94          //       8 o| AGND ----|
  95          //         o| VREF ----|<-|
  96          //         o| AIN2.0   |  |
  97          //         o|    |        |
  98          //         o|     --------
  99          //         o|
 100          //         o|
 101          //       1 o|
 102          //          |
 103          // ---------
 104          //
 105          // How To Test:
 106          //
 107          // 1) Download code to a 'F06x device that is connected to a UART transceiver
 108          // 2) Connect serial cable from the transceiver to a PC
 109          // 3) On the PC, open HyperTerminal (or any other terminal program) and connect
 110          //    to the COM port at <BAUDRATE> and 8-N-1
 111          // 4) Connect a variable voltage source (between 0 and Vref)
 112          //    to AIN2.0 - AIN2.7, or a potentiometer voltage divider as shown above.
 113          ///   AIN2.0 - AIN2.7 are shared with  the Port1 pins available on J12
 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.08   F06X_ADC2_EXTERNALINPUT_MUX                                           02/14/2008 15:32:09 PAGE 3   

 118          // Target:         C8051F06x
 119          // Tool chain:     Keil C51 7.50 / Keil EVAL C51
 120          // Command Line:   None
 121          //
 122          //
 123          // Release 1.0
 124          //    -Initial Revision SM
 125          //    -21-July-06
 126          //
 127          
 128          
 129          //-----------------------------------------------------------------------------
 130          // Includes
 131          //-----------------------------------------------------------------------------
 132          
 133          #include <c8051f060.h>                 // SFR declarations
 134          #include <stdio.h>
 135          
 136          //-----------------------------------------------------------------------------
 137          // 16-bit SFR Definitions for 'F06x
 138          //-----------------------------------------------------------------------------
 139          
 140          sfr16 DP       = 0x82;                 // data pointer
 141          sfr16 RCAP2    = 0xCA;                 // Timer2 reload/capture value
 142          sfr16 RCAP3    = 0xCA;                 // Timer3 reload/capture value
 143          sfr16 RCAP4    = 0xCA;                 // Timer4 reload/capture value
 144          sfr16 TMR2     = 0xCC;                 // Timer2 counter/timer
 145          sfr16 TMR3     = 0xCC;                 // Timer3 counter/timer
 146          sfr16 TMR4     = 0xCC;                 // Timer4 counter/timer
 147          sfr16 ADC2     = 0xBE;                 // ADC2 data
 148          sfr16 ADC0GT   = 0xC4;                 // ADC0 greater than window
 149          sfr16 ADC0LT   = 0xC6;                 // ADC0 less than window
 150          sfr16 DAC0     = 0xD2;                 // DAC0 data
 151          sfr16 DAC1     = 0xD2;                 // DAC1 data
 152          sfr16 CAN0DAT  = 0xD8;                 // CAN data window
 153          
 154          //-----------------------------------------------------------------------------
 155          // Global Constants
 156          //-----------------------------------------------------------------------------
 157          
 158          #define BAUDRATE     115200            // Baud rate of UART in bps
 159          #define SYSCLK       24500000          // Output of PLL derived from (INTCLK*2)
 160          #define INT_DEC      256               // Integrate and decimate ratio
 161          #define SAR_CLK      2500000           // Desired SAR clock speed
 162          
 163          #define SAMPLE_DELAY 250               // Delay in ms before displaying sample
 164          
 165          #define ANALOG_INPUTS 8                // Number of AIN pins to measure
 166                                                 // (min=1, max=8)
 167          
 168          sbit  LED = P1^6;                      // LED: '1' = ON; '0' = OFF
 169          
 170          
 171          //-----------------------------------------------------------------------------
 172          // Function Prototypes
 173          //-----------------------------------------------------------------------------
 174          
 175          void OSCILLATOR_Init (void);
 176          void PORT_Init (void);
 177          void UART1_Init (void);
 178          void ADC2_Init (void);
 179          void TIMER2_Init (void);
C51 COMPILER V8.08   F06X_ADC2_EXTERNALINPUT_MUX                                           02/14/2008 15:32:09 PAGE 4   

 180          void ADC2_ISR (void);
 181          void TIMER2_ISR (void);
 182          void Wait_MS (unsigned int ms);
 183          
 184          //-----------------------------------------------------------------------------
 185          // Global Variables
 186          //-----------------------------------------------------------------------------
 187          
 188          long Result[ANALOG_INPUTS];            // ADC2 decimated value, one for each
 189                                                 // analog input
 190          unsigned char amux_input=0;            // index of analog MUX inputs
 191          unsigned char amux_convert=0;
 192          
 193          
 194          //-----------------------------------------------------------------------------

⌨️ 快捷键说明

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