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

📄 f31x_adc0_externalinput_mux.lst

📁 C8051F31系列单片机的例子
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE F31X_ADC0_EXTERNALINPUT_MUX
OBJECT MODULE PLACED IN F31x_ADC0_ExternalInput_Mux.OBJ
COMPILER INVOKED BY: E:\Apps\Keil\C51\BIN\c51.exe F31x_ADC0_ExternalInput_Mux.c DB OE

line level    source

   1          //-----------------------------------------------------------------------------
   2          // F31x_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 (P1.0), up
  12          // to input number <ANALOG_INPUTS>-1 based on the values in <PIN_TABLE>.
  13          //
  14          //
  15          // ADC Settling Time Requirements, Sampling Rate:
  16          // ----------------------------------------------
  17          //
  18          // The total sample time per input is comprised of an input setting time
  19          // (Tsettle), followed by a conversion time (Tconvert):
  20          //
  21          // Tsample  = Tsettle + Tconvert
  22          //
  23          // Settling and conversion times may overlap, as the ADC holds the value once
  24          // conversion begins.  This program takes advantage of this to increase the
  25          // settling time above the minimum required.  In other words, when
  26          // converting the value from analog input Ain(n), the input mux is switched
  27          // over to the next input Ain(n+1) to begin settling.
  28          //
  29          // |---Settling Ain(n)---|=Conversion Ain(n)=|
  30          //                       |---Settling Ain(n+1)---|=Conversion Ain(n+1)=|
  31          //                                               |---Settling Ain(n+2)---|
  32          // ISR:  Timer 2         ^                       ^                       ^
  33          // ISR:  ADC0                                ^                         ^
  34          //
  35          // The ADC input voltage must be allowed adequate time to settle before the
  36          // conversion is made.  This settling depends on the external source
  37          // impedance, internal mux impedance, and internal capacitance.
  38          // Settling time is given by:
  39          //
  40          //                   | 2^n |
  41          //    Tsettle =   ln | --- | * Rtotal * Csample
  42          //                   | SA  |
  43          //
  44          // In this application, assume a 100kohm potentiometer as the voltage divider.
  45          // The expression evaluates to:
  46          //
  47          //                   | 2^12 |
  48          //    Tsettle =   ln | ---- | * 105e3 * 10e-12 = 10.2uS
  49          //                   | 0.25 |
  50          //
  51          // In addition, one must allow at least 1.5 us after changing analog MUX
  52          // inputs or PGA settings.  The settling time in this example, then, is
  53          // dictated by the large external source resistance.
  54          //
  55          // The conversion is 16 periods of the SAR clock.  At 2.5 MHz,
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 2   

  56          // this time is 16 * 400nS = 6.4 uS.
  57          //
  58          //
  59          // Tsample, minimum  = Tsettle + Tconvert
  60          //                   = 10.2uS + 6.4uS
  61          //                   = 16.6 uS
  62          //
  63          // Timer2 is set to change the MUX input and start a conversion every 20 us.
  64          //
  65          // General:
  66          // --------
  67          //
  68          // The system is clocked using the internal 24.5MHz oscillator. Results are
  69          // printed to the UART from a loop with the rate set by a delay based on
  70          // Timer0. This loop periodically reads the ADC value from a global array,
  71          // <RESULT>.
  72          //
  73          // The ADC makes repeated measurements at 20 us intervals based on Timer2.
  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 P1.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.
  87          //
  88          // A 100 kohm potentiometer may be connected as a voltage divider between
  89          // VREF and AGND as shown below:
  90          //
  91          // ---------
  92          //          |
  93          //         o| AGND ----|
  94          //         o| VREF ----|<-|
  95          //         o| P1.x     |  |
  96          //         o|    |        |
  97          //         o|     --------
  98          //         o|
  99          //         o|
 100          //         o|
 101          //          |
 102          // ---------
 103          //
 104          // How To Test:
 105          //
 106          // 1) Download code to a 'F31x device that is connected to a UART transceiver
 107          // 2) Verify the TX and RX jumpers are populated on J3.
 108          // 3) Connect a serial cable from the DB9 connector to a PC
 109          // 4) On the PC, open HyperTerminal (or any other terminal program) and connect
 110          //    to the COM port at <BAUDRATE> and 8-N-1
 111          // 5) Connect a variable voltage source (between 0 and Vref) to the Port1 pins,
 112          //    or a potentiometer voltage divider as shown above.
 113          // 6) HyperTerminal will print the voltages measured by the device if
 114          //    everything is working properly.  Note that some of the analog inputs are
 115          //    floating and will return nonzero values.
 116          //
 117          // Target:         C8051F31x
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 3   

 118          // Tool chain:     Keil C51 7.50 / Keil EVAL C51
 119          // Command Line:   None
 120          //
 121          // Release 1.0
 122          //    -Initial Revision (SM / TP)
 123          //    -19 OCT 2006
 124          //
 125          
 126          
 127          //-----------------------------------------------------------------------------
 128          // Includes
 129          //-----------------------------------------------------------------------------
 130          
 131          #include "c8051F310.h"                 // SFR declarations
 132          #include <stdio.h>
 133          
 134          //-----------------------------------------------------------------------------
 135          // 16-bit SFR Definitions for 'F31x
 136          //-----------------------------------------------------------------------------
 137          
 138          sfr16 TMR2RL = 0xCA;                   // Timer2 reload value
 139          sfr16 TMR2 = 0xCC;                     // Timer2 counter
 140          sfr16 ADC0 = 0xBD;                     // 10-bit ADC0 result
 141          
 142          //-----------------------------------------------------------------------------
 143          // Global CONSTANTS
 144          //-----------------------------------------------------------------------------
 145          
 146          #define SYSCLK        24500000         // SYSCLK frequency in Hz
 147          #define BAUDRATE      115200           // Baud rate of UART in bps
 148          #define ANALOG_INPUTS 6                // Number of AIN pins to measure,
 149                                                 // skipping the UART0 pins
 150          #define INT_DEC       256              // Integrate and decimate ratio
 151          
 152          #define TIMER0_RELOAD_HIGH  0          // Timer0 High register
 153          #define TIMER0_RELOAD_LOW 255          // Timer0 Low register
 154          
 155          //-----------------------------------------------------------------------------
 156          // Function PROTOTYPES
 157          //-----------------------------------------------------------------------------
 158          
 159          void Oscillator_Init (void);
 160          void Port_Init (void);
 161          void Timer2_Init(void);
 162          void ADC0_Init(void);
 163          void UART0_Init (void);
 164          
 165          void Timer0_wait(int ms);
 166          
 167          //-----------------------------------------------------------------------------
 168          // Global Variables
 169          //-----------------------------------------------------------------------------
 170          
 171          long RESULT[ANALOG_INPUTS];            // ADC0 decimated value, one for each
 172                                                 // analog input
 173          
 174          // Port1 ADC Inputs
 175          unsigned char idata PIN_TABLE[ANALOG_INPUTS] = {0,1,2,3,4,5};
 176          
 177          unsigned char AMUX_INPUT = 0;          // Index of analog MUX inputs
 178          
 179          //-----------------------------------------------------------------------------
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 4   

 180          // MAIN Routine
 181          //-----------------------------------------------------------------------------
 182          void main (void)
 183          {
 184   1         unsigned char i;
 185   1         unsigned long measurement;
 186   1      
 187   1         PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer
 188   1                                             // enable)
 189   1      
 190   1         Oscillator_Init ();                 // Initialize system clock to
 191   1                                             // 24.5MHz
 192   1         Port_Init ();                       // Initialize crossbar and GPIO
 193   1         Timer2_Init();                      // Init Timer2 to generate
 194   1                                             // overflows to trigger ADC
 195   1         UART0_Init();                       // Initialize UART0 for printf's
 196   1         ADC0_Init();                        // Initialize ADC0
 197   1      
 198   1         EA = 1;                             // Enable global interrupts
 199   1         while (1)
 200   1         {
 201   2            EA = 0;                          // Disable interrupts
 202   2            printf("\f");
 203   2            for(i = 0; i < ANALOG_INPUTS; i++)
 204   2            {
 205   3               // The 10-bit ADC value is averaged across INT_DEC measurements.
 206   3               // The result is then stored in RESULT, and is right-justified
 207   3               // The measured voltage applied to the port pins is then:
 208   3               //
 209   3               //                           Vref (mV)
 210   3               //   measurement (mV) =   --------------- * Result (bits)
 211   3               //                        (2^10)-1 (bits)
 212   3      
 213   3               measurement =  (RESULT[i] - 8) * 3268 / 1023;
 214   3               printf("P1.%bu voltage: %4ld mV\n",PIN_TABLE[i],measurement);
 215   3            }
 216   2            EA = 1;                          // Re-enable interrupts
 217   2      
 218   2            Timer0_wait(25);                 // Wait before displaying new values
 219   2         }
 220   1      }
 221          
 222          //-----------------------------------------------------------------------------
 223          // Initialization Subroutines
 224          //-----------------------------------------------------------------------------
 225          
 226          //-----------------------------------------------------------------------------
 227          // SYSCLK_Init
 228          //-----------------------------------------------------------------------------
 229          //
 230          // Return Value : None
 231          // Parameters   : None
 232          //
 233          // This routine initializes the system clock to use the internal 24.5MHz
 234          // oscillator as its clock source.  Also enables missing clock detector reset.
 235          //
 236          //-----------------------------------------------------------------------------
 237          void Oscillator_Init (void)
 238          {
 239   1         OSCICN = 0x83;                      // Configure internal oscillator for
 240   1                                             // its highest frequency
 241   1         RSTSRC = 0x04;                      // Enable missing clock detector
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT_MUX                                           07/28/2008 14:50:43 PAGE 5   

 242   1      }
 243          
 244          //-----------------------------------------------------------------------------
 245          // Port_Init
 246          //-----------------------------------------------------------------------------
 247          //
 248          // Return Value : None
 249          // Parameters   : None
 250          //
 251          // Configure the Crossbar and GPIO ports.
 252          //
 253          // P0.4 - UART TX (push-pull)
 254          // P0.5 - UART RX

⌨️ 快捷键说明

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