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

📄 f06x_adc2_externalinput.lst

📁 // This program measures the voltage on an external ADC input and prints the // result to a termin
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V8.08   F06X_ADC2_EXTERNALINPUT                                               02/18/2008 14:59:47 PAGE 1   


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

line level    source

   1          //-----------------------------------------------------------------------------
   2          // F06x_ADC0_ExternalInput.c
   3          //-----------------------------------------------------------------------------
   4          // Copyright 2005 Silicon Laboratories, Inc.
   5          // http://www.silabs.com
   6          //
   7          // Program Description:
   8          //
   9          // This program measures the voltage on an external ADC input and prints the 
  10          // result to a terminal window via the UART.
  11          //
  12          // The system is clocked using the internal 24.5MHz oscillator.  
  13          // Results are printed to the UART from a loop with the rate set by a delay 
  14          // based on Timer 2.  This loop periodically reads the ADC value from a global 
  15          // variable, Result.
  16          //
  17          // The ADC makes repeated measurements at a rate determined by SAMPLE_RATE 
  18          // using Timer 3. The end of each ADC conversion initiates an interrupt which 
  19          // calls an averaging function.  
  20          // <INT_DEC> samples are averaged then the Result value updated.
  21          //
  22          // For each power of 4 of <INT_DEC>, you gain 1 bit of effective resolution.
  23          // For example, <INT_DEC> = 256 gain you 4 bits of resolution: 4^4 = 256.
  24          // 
  25          // The ADC input multiplexer is set for a single-ended input at AIN2.1.  
  26          // The input amplifier is set for unity gain so a voltage range of 0 to Vref 
  27          // (2.43V) may be measured.  Although voltages up to Vdd may be applied without
  28          // damaging the device, only the range 0 to Vref may be measured by the ADC.  
  29          //
  30          // A 100kohm potentiometer may be connected as a voltage divider between 
  31          // VREF and AGND as shown below: (minimum value = 12Kohms as the maximum 
  32          // recommended current through the Vref is of 200uA @2.4V)
  33          //
  34          // ---------
  35          //          |        
  36          //       8 o| AGND ----| 
  37          //         o| VREF ----|<-|
  38          //         o| AIN2.1   |  |
  39          //         o|    |        |
  40          //         o|     -------- 
  41          //         o|
  42          //         o|
  43          //       1 o|
  44          //          |
  45          //----------   
  46          //
  47          // How To Test:
  48          //
  49          // 1) Download code to a 'F06x device that is connected to a UART transceiver
  50          // 2) Connect serial cable from the transceiver to a PC
  51          // 3) On the PC, open HyperTerminal (or any other terminal program) and connect
  52          //    to the COM port at <BAUDRATE> and 8-N-1
  53          // 4) Connect a variable voltage source (between 0 and Vref) 
  54          //    to AIN 2.1, or a potentiometer voltage divider as shown above.
  55          // 5) HyperTerminal will print the voltage measured by the device if
C51 COMPILER V8.08   F06X_ADC2_EXTERNALINPUT                                               02/18/2008 14:59:47 PAGE 2   

  56          //    everything is working properly
  57          //
  58          // FID:            06X000022
  59          // Target:         C8051F06x
  60          // Tool chain:     Keil C51 7.50 / Keil EVAL C51
  61          // Command Line:   None
  62          //
  63          //
  64          // Release 1.0
  65          //    -Initial Revision (CG)
  66          //    -8-June-06
  67          //
  68          //-----------------------------------------------------------------------------
  69          // Includes
  70          //-----------------------------------------------------------------------------
  71          
  72          #include <c8051F060.h>                 // SFR declarations
  73          #include <stdio.h>                     
  74          
  75          //-----------------------------------------------------------------------------
  76          // 16-bit SFR Definitions for 'F06x
  77          //-----------------------------------------------------------------------------
  78          
  79          sfr16 ADC2     = 0xbe;                 // ADC2 data
  80          sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
  81          sfr16 RCAP3    = 0xca;                 // Timer3 capture/reload
  82          sfr16 TMR2     = 0xcc;                 // Timer2
  83          sfr16 TMR3     = 0xcc;                 // Timer3
  84          
  85          //-----------------------------------------------------------------------------
  86          // Global Constants
  87          //-----------------------------------------------------------------------------
  88          
  89          #define BAUDRATE     9600            // Baud rate of UART in bps
  90          #define SYSCLK       24500000          // System Clock
  91          #define SAMPLE_RATE  50000             // Sample frequency in Hz
  92          #define INT_DEC      256               // Integrate and decimate ratio
  93          #define SAR_CLK      2500000           // Desired SAR clock speed
  94          
  95          #define SAMPLE_DELAY 2000                // Delay in ms before taking sample
  96          
  97          sbit LED = P1^6;                       // LED='1' means ON
  98          sbit SW1 = P3^7;                       // SW1='0' means switch pressed
  99          
 100          //-----------------------------------------------------------------------------
 101          // Function Prototypes
 102          //-----------------------------------------------------------------------------
 103          
 104          void OSCILLATOR_Init (void);           
 105          void PORT_Init (void);
 106          void UART1_Init (void);
 107          void ADC2_Init (void);
 108          void TIMER3_Init (int counts);
 109          void ADC2_ISR (void);
 110          void Wait_MS (unsigned int ms);
 111          
 112          //-----------------------------------------------------------------------------
 113          // Global Variables
 114          //-----------------------------------------------------------------------------
 115          
 116          long Result;                           // ADC0 decimated value
 117          
C51 COMPILER V8.08   F06X_ADC2_EXTERNALINPUT                                               02/18/2008 14:59:47 PAGE 3   

 118          //-----------------------------------------------------------------------------
 119          // main() Routine
 120          //-----------------------------------------------------------------------------
 121          
 122          void main (void)
 123          {
 124   1         long measurement;                   // Measured voltage in mV
 125   1      
 126   1         WDTCN = 0xde;                       // Disable watchdog timer
 127   1         WDTCN = 0xad;
 128   1      
 129   1         OSCILLATOR_Init ();                 // Initialize oscillator
 130   1         PORT_Init ();                       // Initialize crossbar and GPIO
 131   1         UART1_Init ();                      // Initialize UART1
 132   1         TIMER3_Init (SYSCLK/SAMPLE_RATE);   // Initialize Timer3 to overflow at
 133   1                                             // sample rate
 134   1      
 135   1         ADC2_Init ();                       // Init ADC
 136   1      
 137   1         SFRPAGE = ADC2_PAGE;
 138   1         AD2EN = 1;                          // Enable ADC
 139   1      
 140   1         EA = 1;                             // Enable global interrupts
 141   1      
 142   1         while (1)
 143   1         {
 144   2            EA = 0;                          // Disable interrupts
 145   2      
 146   2            // The 10-bit ADC2 value is averaged across INT_DEC measurements.  
 147   2            // The result is then stored in Result, and is right-justified 
 148   2            // The measured voltage applied to AIN 2.1 is then:
 149   2            //
 150   2            //                           Vref (mV)
 151   2            //   measurement (mV) =   --------------- * Result (bits) 
 152   2            //                       (2^10)-1 (bits)
 153   2      
 154   2            measurement =  Result * 2430 / 1023;
 155   2      
 156   2            EA = 1;                          // Re-enable interrupts
 157   2      
 158   2            SFRPAGE = UART1_PAGE;
 159   2      
 160   2            printf("AIN2.1 voltage: %ld mV\n",measurement);
 161   2      
 162   2            SFRPAGE = CONFIG_PAGE;
 163   2            LED = ~SW1;                      // LED reflects state of switch
 164   2      
 165   2            Wait_MS(SAMPLE_DELAY);           // Wait 50 milliseconds before taking
 166   2                                             // another sample
 167   2         }
 168   1      }
 169          
 170          //-----------------------------------------------------------------------------
 171          // Initialization Subroutines
 172          //-----------------------------------------------------------------------------
 173          
 174          //-----------------------------------------------------------------------------
 175          // OSCILLATOR_Init
 176          //-----------------------------------------------------------------------------
 177          //
 178          // Return Value : None
 179          // Parameters   : None
C51 COMPILER V8.08   F06X_ADC2_EXTERNALINPUT                                               02/18/2008 14:59:47 PAGE 4   

 180          //
 181          // This function initializes the system clock to use the internal oscillator
 182          // at 24.5 MHz.
 183          //
 184          //-----------------------------------------------------------------------------
 185          void OSCILLATOR_Init (void)
 186          {
 187   1         char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page
 188   1      
 189   1         SFRPAGE = CONFIG_PAGE;              // Set SFR page
 190   1      
 191   1         OSCICN = 0x83;                      // Set internal oscillator to run
 192   1                                             // at its maximum frequency
 193   1      
 194   1         CLKSEL = 0x00;                      // Select the internal osc. as
 195   1                                             // the SYSCLK source
 196   1      
 197   1         SFRPAGE = SFRPAGE_SAVE;             // Restore SFR page
 198   1      }
 199          
 200          //-----------------------------------------------------------------------------
 201          // PORT_Init
 202          //-----------------------------------------------------------------------------
 203          //
 204          // Return Value : None
 205          // Parameters   : None
 206          //
 207          // This function configures the crossbar and GPIO ports.
 208          //
 209          // P0.4   digital   push-pull     UART TX
 210          // P0.5   digital   open-drain    UART RX
 211          // P1.6   digital   push-pull     LED
 212          // AIN1.1 analog                  Analog input 
 213          //-----------------------------------------------------------------------------
 214          void PORT_Init (void)
 215          {

⌨️ 快捷键说明

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