?? f06x_adc2_externalinput_mux.c
字號:
//-----------------------------------------------------------------------------
// F06x_ADC2_ExternalInput_Mux.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// This code example illustrates using the internal analog multiplexer to
// measure analog voltages on up to 8 different analog inputs. Results are
// printed to a PC terminal program via the UART.
//
// The inputs are sequentially scanned, beginning with input 0 (AIN2.0), up
// to input number <ANALOG_INPUTS>-1 (maximum ANALOG_INPUTS = 8, which will
// scan all analog inputs AIN2.0 - AIN2.7).
//
//
// ADC Settling Time Requirements, Sampling Rate:
// ----------------------------------------------
//
// The total sample time per intput is comprised of an input setting time
// (Tsettle), followed by a conversion time (Tconvert):
//
// Tsample = Tsettle + Tconvert
//
// Settling and conversion times may overlap, as the ADC holds the value once
// conversion begins. This code example takes advantage of this to increase
// the settling time above the minimum required. In other words, when
// converting the value from analog input Ain(n), the input mux is switched
// over to the next input Ain(n+1) to begin settling.
//
// |--------Settling Ain(n)--------|=Conversion Ain(n)=|
// |--------Settling Ain(n+1)--------|=Conversion Ain(n+1)=|
// |--------Settling Ain(n+2)--------|
// ISR: Timer 2 ^ ^ ^
// ISR: ADC2 ^ ^
//
// The ADC input voltage must be allowed adequate time to settle before the
// conversion is made. This settling depends on the external source
// impedance, internal mux impedance, and internal capacitance.
// Settling time is given by:
//
// | 2^n |
// Tsettle = ln | --- | * Rtotal * Csample
// | SA |
//
// In this application, assume a 100kohm potentiometer as the voltage divider.
// The expression evaluates to:
//
// | 2^12 |
// Tsettle = ln | ---- | * 105e3 * 10e-12 = 10.2uS
// | 0.25 |
//
// In addition, one must allow at least 1.5uS after changing analog mux
// inputs or PGA settings. The settling time in this example, then, is
// dictated by the large external source resistance.
//
// The conversion is 16 periods of the SAR clock <SAR_CLK>. At 2.5 MHz,
// this time is 16 * 400nS = 6.4 uS.
//
//
// Tsample, minimum = Tsettle + Tconvert
// = 10.2uS + 6.4uS
// = 16.6 uS
//
// Timer 2 is set to change the mux input and start a conversion every 20uS.
//
// General:
// --------
//
// The system is clocked using the internal 24.5MHz oscillator. Results are
// printed to the UART from a loop with the rate set by a delay based on Timer 2.
// This loop periodically reads the ADC value from a global array, Result.
//
// The ADC makes repeated measurements at 20uS intervals based on Timer 2.
// The end of each ADC conversion initiates an interrupt which calls an
// averaging function. <INT_DEC> samples are averaged then the Result
// values updated.
//
// For each power of 4 of <INT_DEC>, you gain 1 bit of effective resolution.
// For example, <INT_DEC> = 256 gain you 4 bits of resolution: 4^4 = 256.
//
// The ADC input multiplexer is set for a single-ended input. The example
// sequentially scans through the inputs, starting at AIN0.0. <ANALOG_INPUTS>
// inputs are read. The amplifier is set for unity gain so a voltage range of
// 0 to Vref (2.43V) may be measured. Although voltages up to Vdd may be
// applied without damaging the device, only the range 0 to Vref may be
// measured by the ADC. The input is available at the 8-position board-edge
// connector, J20, on the C8051FX20-TB.
//
// A 100kohm potentiometer may be connected as a voltage divider between
// VREF and AGND as shown below:
//
// ---------
// |
// 8 o| AGND ----|
// o| VREF ----|<-|
// o| AIN2.0 | |
// o| | |
// o| --------
// o|
// o|
// 1 o|
// |
// ---------
//
// How To Test:
//
// 1) Download code to a 'F06x device that is connected to a UART transceiver
// 2) Connect serial cable from the transceiver to a PC
// 3) On the PC, open HyperTerminal (or any other terminal program) and connect
// to the COM port at <BAUDRATE> and 8-N-1
// 4) Connect a variable voltage source (between 0 and Vref)
// to AIN2.0 - AIN2.7, or a potentiometer voltage divider as shown above.
/// AIN2.0 - AIN2.7 are shared with the Port1 pins available on J12
// 5) HyperTerminal will print the voltages measured by the device if
// everything is working properly. Note that some of the analog inputs are
// floating and will return nonzero values.
//
// Target: C8051F06x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Command Line: None
//
//
// Release 1.0
// -Initial Revision SM
// -21-July-06
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f060.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F06x
//-----------------------------------------------------------------------------
sfr16 DP = 0x82; // data pointer
sfr16 RCAP2 = 0xCA; // Timer2 reload/capture value
sfr16 RCAP3 = 0xCA; // Timer3 reload/capture value
sfr16 RCAP4 = 0xCA; // Timer4 reload/capture value
sfr16 TMR2 = 0xCC; // Timer2 counter/timer
sfr16 TMR3 = 0xCC; // Timer3 counter/timer
sfr16 TMR4 = 0xCC; // Timer4 counter/timer
sfr16 ADC2 = 0xBE; // ADC2 data
sfr16 ADC0GT = 0xC4; // ADC0 greater than window
sfr16 ADC0LT = 0xC6; // ADC0 less than window
sfr16 DAC0 = 0xD2; // DAC0 data
sfr16 DAC1 = 0xD2; // DAC1 data
sfr16 CAN0DAT = 0xD8; // CAN data window
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define BAUDRATE 115200 // Baud rate of UART in bps
#define SYSCLK 24500000 // Output of PLL derived from (INTCLK*2)
#define INT_DEC 256 // Integrate and decimate ratio
#define SAR_CLK 2500000 // Desired SAR clock speed
#define SAMPLE_DELAY 250 // Delay in ms before displaying sample
#define ANALOG_INPUTS 8 // Number of AIN pins to measure
// (min=1, max=8)
sbit LED = P1^6; // LED: '1' = ON; '0' = OFF
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
void UART1_Init (void);
void ADC2_Init (void);
void TIMER2_Init (void);
void ADC2_ISR (void);
void TIMER2_ISR (void);
void Wait_MS (unsigned int ms);
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
long Result[ANALOG_INPUTS]; // ADC2 decimated value, one for each
// analog input
unsigned char amux_input=0; // index of analog MUX inputs
unsigned char amux_convert=0;
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
unsigned char i;
long measurement; // measured voltage in mV
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize oscillator
PORT_Init (); // Initialize crossbar and GPIO
UART1_Init (); // Initialize UART1
TIMER2_Init (); // Initialize Timer2 to overflow at 1 mS
ADC2_Init (); // Init ADC
SFRPAGE = ADC2_PAGE;
AD2EN = 1; // Enable ADC
EA = 1; // Enable global interrupts
while (1)
{
EA = 0; // Disable interrupts
SFRPAGE = UART1_PAGE;
printf("\f");
for(i=0; i<ANALOG_INPUTS; i++)
{
// The 10-bit ADC value is averaged across INT_DEC measurements.
// The result is then stored in Result, and is right-justified
// The measured voltage applied to AIN 2.1 is then:
//
// Vref (mV)
// measurement (mV) = --------------- * Result (bits)
// (2^10)-1 (bits)
measurement = Result[i] * 2430 / 1023;
printf("AIN0.%bu voltage: %ld\tmV\n",i,measurement);
}
EA = 1; // Re-enable interrupts
Wait_MS(SAMPLE_DELAY); // Wait before displaying new values
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine initializes the system clock to use the internal oscillator
// at 24.5 MHz.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
OSCICN = 0x83; // set internal oscillator to run
// at its maximum frequency
CLKSEL = 0x00; // Select the internal osc. as
// the SYSCLK source
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine configures the crossbar and GPIO ports.
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
XBR0 = 0x00;
XBR1 = 0x00;
XBR2 = 0x44; // Enable crossbar and weak pull-up
// Enable UART1
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -