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

📄 f31x_adc0_externalinput.lst

📁 C8051F31系列单片机的例子
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT                                               03/20/2009 15:55:34 PAGE 1   


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

line level    source

   1          //-----------------------------------------------------------------------------
   2          // F31x_ADC0_ExternalInput.c
   3          //-----------------------------------------------------------------------------
   4          // Copyright 2006 Silicon Laboratories, Inc.
   5          // http://www.silabs.com
   6          //
   7          // Program Description:
   8          // --------------------
   9          //
  10          // This example code takes and averages 2048 analog measurements from input
  11          // P2.5 using ADC0, then prints the results to a terminal window via the UART.
  12          //
  13          // The system is clocked by the internal 24.5MHz oscillator.  Timer 2 triggers
  14          // a conversion on ADC0 on each overflow.  The completion of this conversion
  15          // in turn triggers an interrupt service routine (ISR).  The ISR averages 
  16          // 2048 measurements, then prints the value to the terminal via printf before
  17          // starting another average cycle.
  18          //
  19          // The analog multiplexer selects P2.5 as the positive ADC0 input.  This 
  20          // port is configured as an analog input in the port initialization routine.
  21          // The negative ADC0 input is connected via mux to ground, which provides
  22          // for a single-ended ADC input.
  23          //
  24          // A 100kohm potentiometer may be connected as a voltage divider between 
  25          // P0.0 and AGND on the terminal strip as shown below:
  26          //
  27          // ---------
  28          //          |
  29          //          |
  30          //          |        
  31          //         o| P0.0 ----| 
  32          //         o| GND   ---|<-|
  33          //         o|             |
  34          //         o| P2.5--------|        
  35          //          |
  36          //----------   
  37          // C8051F310-TB
  38          //
  39          // Note that the ADC reference voltage is tied to Vdd, not a precision 
  40          // internal reference.  This limits the precision of the measurement,
  41          // as Vdd is set by the regulator on the board, not the C8051F310.
  42          //
  43          // Terminal output is done via printf, which directs the characters to 
  44          // UART0.  A UART initialization routine is therefore necessary.
  45          //
  46          // ADC Settling Time Requirements, Sampling Rate:
  47          // ----------------------------------------------
  48          //
  49          // The total sample time per input is comprised of an input setting time 
  50          // (Tsettle), followed by a conversion time (Tconvert): 
  51          //
  52          // Tsample  = Tsettle + Tconvert
  53          //
  54          // |--------Settling-------|==Conversion==|----Settling--- . . .
  55          // Timer 2 overflow        ^                          
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT                                               03/20/2009 15:55:34 PAGE 2   

  56          // ADC0 ISR                               ^       
  57          //
  58          // The ADC input voltage must be allowed adequate time to settle before the 
  59          // conversion is made.  This settling depends on the external source
  60          // impedance, internal mux impedance, and internal capacitance.
  61          // Settling time is given by:
  62          //
  63          //                   | 2^n |
  64          //    Tsettle =   ln | --- | * Rtotal * Csample
  65          //                   | SA  |       
  66          //
  67          // In this application, assume a 100kohm potentiometer as the voltage divider.
  68          // The expression evaluates to:
  69          //
  70          //                   | 2^10 |
  71          //    Tsettle =   ln | ---- | * 105e3 * 5e-12 = 4.4uS
  72          //                   | 0.25 |    
  73          //
  74          // In addition, one must allow at least 1.5uS after changing analog mux 
  75          // inputs or PGA settings.  The settling time in this example, then, is 
  76          // dictated by the large external source resistance.
  77          //
  78          // The conversion is 10 periods of the SAR clock <SAR_CLK>.  At 3 MHz, 
  79          // this time is 10 * 400nS = 3.3 uS.
  80          //
  81          // 
  82          // Tsample, minimum  = Tsettle + Tconvert
  83          //                   = 4.4uS + 3.3uS 
  84          //                   = 7.7 uS
  85          // 
  86          // Timer 2 is set to start a conversion  every 100uS, which is far longer 
  87          // than the minimum required.
  88          //
  89          // F310 Resources:
  90          // ---------------
  91          // Timer1: clocks UART
  92          // Timer2: overflow initiates ADC conversion
  93          //
  94          // How To Test:
  95          // ------------
  96          // 1) Download code to a 'F310 device on a C8051F310-TB development board
  97          // 2) Connect serial cable from the transceiver to a PC
  98          // 3) On the PC, open HyperTerminal (or any other terminal program) and connect
  99          //    to the COM port at <BAUDRATE> and 8-N-1
 100          // 4) Connect a variable voltage source (between 0 and VDD) 
 101          //    to P2.5, or a potentiometer voltage divider as shown above.
 102          // 5) HyperTerminal will print the voltage measured by the device if
 103          //    everything is working properly
 104          //
 105          // FID:            31X000011
 106          // Target:         C8051F310
 107          // Tool chain:     Keil C51 7.50 / Keil EVAL C51
 108          // Command Line:   None
 109          //
 110          //
 111          // Release 1.0
 112          //    -Initial Revision (clm)
 113          //    -24-Jul-06
 114          
 115          
 116          //-----------------------------------------------------------------------------
 117          // Includes
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT                                               03/20/2009 15:55:34 PAGE 3   

 118          //-----------------------------------------------------------------------------
 119          #include "c8051F310.h"                 // SFR declarations
 120          #include <stdio.h>
 121          
 122          //-----------------------------------------------------------------------------
 123          // 16-bit SFR Definitions for 'F31x
 124          //-----------------------------------------------------------------------------
 125          
 126          sfr16 TMR2RL   = 0xca;                 // Timer2 reload value 
 127          sfr16 TMR2     = 0xcc;                 // Timer2 counter
 128          sfr16 ADC0     = 0xbd;                 // ADC0 result
 129          
 130          //-----------------------------------------------------------------------------
 131          // Global CONSTANTS
 132          //-----------------------------------------------------------------------------
 133          
 134          #define SYSCLK       24500000          // SYSCLK frequency in Hz
 135          #define BAUDRATE     115200            // Baud rate of UART in bps
 136          
 137          sbit LED = P3^3;                       // LED='1' means ON
 138          
 139          //-----------------------------------------------------------------------------
 140          // Function PROTOTYPES
 141          //-----------------------------------------------------------------------------
 142          void SYSCLK_Init (void);
 143          void PORT_Init (void);
 144          void Timer2_Init(void);
 145          void ADC0_Init(void);
 146          void UART0_Init (void);
 147          
 148          
 149          void ADC_Init()
 150          {       
 151   1          int SARCLK=3000000; // 3 Mhz
 152   1              // Use AD0BUSY as conversion enable
 153   1              ADC0CN=0x40;                                    // ADC with normal track mode
 154   1              //REF0CN=0x0e;                                  // Enable temp sensor and use on chip Vref (VDD)
 155   1      //      REF0CN=0x0A;                                    // Enable temp sensor and use on external Vref
 156   1              AMX0N=0x1F;                                             // Single-Ended inputs
 157   1              ADC0CF=(SYSCLK/SARCLK-1)<<3;    // Conversion clock setting & results are right-justified
 158   1              ADC0CF |= 0x00;                     // right-justify results 
 159   1              EIE1 &= 0xF1;                                   // Disable ADC interrupt & disable win compare interrupts
 160   1              AD0EN=1;                                                // Enable ADC, ready for data conversion
 161   1      }
 162          
 163          unsigned int ADC_Read_Ch(unsigned char chan_num)
 164          {       
 165   1          unsigned long result;
 166   1              static int measure = 2048;
 167   1              static unsigned long accumulator = 0;
 168   1              unsigned long mV;                   // measured voltage in mV
 169   1      
 170   1      
 171   1      
 172   1              AMX0P = chan_num;                                       // Select chan to be read thru the MUX
 173   1              AD0INT = 0;                                                     // Clear conversion complete indicator
 174   1              AD0BUSY = 1;                                            // Initiate conversion
 175   1              
 176   1              while (!AD0INT);                                        // Wait for convesion completed.
 177   1              
 178   1         
 179   1      
C51 COMPILER V8.08   F31X_ADC0_EXTERNALINPUT                                               03/20/2009 15:55:34 PAGE 4   

 180   1         result = ADC0H;                                              // Stuff converted data
 181   1         result = (result << 8) | ADC0L; 
 182   1      
 183   1      
 184   1         accumulator += result;
 185   1         measure--;
 186   1      
 187   1         if(measure == 0)
 188   1         {  
 189   2            measure = 2048;
 190   2            result = accumulator / 2048;
 191   2            accumulator=0;
 192   2      
 193   2            // The 10-bit ADC value is averaged across 2048 measurements.  
 194   2            // The measured voltage applied to P1.4 is then:
 195   2            //
 196   2            //                           Vref (mV)
 197   2            //   measurement (mV) =   --------------- * result (bits) 
 198   2            //                       (2^10)-1 (bits)
 199   2      
 200   2            mV =  result * 3250 / 1023;   
 201   2            printf("P3.1 voltage: %ld mV\n",mV);
 202   2         }
 203   1      
 204   1              
 205   1              
 206   1              return result;
 207   1      }
 208          
 209          //-----------------------------------------------------------------------------
 210          // MAIN Routine
 211          //-----------------------------------------------------------------------------
 212          void main (void) 
 213          {
 214   1         PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer 
 215   1                                             // enable)
 216   1      
 217   1         SYSCLK_Init ();                     // Initialize system clock to 
 218   1                                             // 12MHz
 219   1         PORT_Init ();                       // Initialize crossbar and GPIO

⌨️ 快捷键说明

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