//-----------------------------------------------------------------------------
// F34x_ADC0_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 (P2.0), up
// to input number <ANALOG_INPUTS>-1 based on the values in <PIN_TABLE>.
//
//
// ADC Settling Time Requirements, Sampling Rate:
// ----------------------------------------------
//
// The total sample time per input 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 program 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: ADC0 ^ ^
//
// 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.5 us 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. At 2.5 MHz,
// this time is 16 * 400nS = 6.4 uS.
//
//
// Tsample, minimum = Tsettle + Tconvert
// = 10.2uS + 6.4uS
// = 16.6 uS
//
// Timer2 is set to change the MUX input and start a conversion every 20 us.
//
// General:
// --------
//
// The system is clocked using the internal 12MHz oscillator. Results are
// printed to the UART from a loop with the rate set by a delay based on
// Timer0. This loop periodically reads the ADC value from a global array,
// <RESULT>.
//
// The ADC makes repeated measurements at 20 us intervals based on Timer2.
// 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 P2.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.
//
// A 100 kohm potentiometer may be connected as a voltage divider between
// VREF and AGND as shown below:
//
// ---------
// |
// o| AGND ----|
// o| VREF ----|<-|
// o| P2.x | |
// o| | |
// o| --------
// o|
// o|
// o|
// |
// ---------
//
// How To Test:
//
// 1) Download code to a 'F34x device that is connected to a UART transceiver
// 2) Verify the TX and RX jumpers are populated on J3.
// 3) Connect a serial cable from the DB9 connector to a PC
// 4) On the PC, open HyperTerminal (or any other terminal program) and connect
// to the COM port at <BAUDRATE> and 8-N-1
// 5) Connect a variable voltage source (between 0 and Vref) to the Port2 pins,
// or a potentiometer voltage divider as shown above.
// 6) 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.
//
// FID: 34X000092
// Target: C8051F34x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Command Line: None
//
// Release 1.0
// -Initial Revision (SM / TP)
// -19 OCT 2006
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F340.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F34x
//-----------------------------------------------------------------------------
sfr16 TMR2RL = 0xCA; // Timer2 reload value
sfr16 TMR2 = 0xCC; // Timer2 counter
sfr16 ADC0 = 0xBD; // 10-bit ADC0 result
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 12000000 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
#define ANALOG_INPUTS 6 // Number of AIN pins to measure,
// skipping the UART0 pins
#define INT_DEC 256 // Integrate and decimate ratio
#define TIMER0_RELOAD_HIGH 0 // Timer0 High register
#define TIMER0_RELOAD_LOW 255 // Timer0 Low register
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void Oscillator_Init (void);
void Port_Init (void);
void Timer2_Init(void);
void ADC0_Init(void);
void UART0_Init (void);
void Timer0_wait(int ms);
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
long RESULT[ANALOG_INPUTS]; // ADC0 decimated value, one for each
// analog input
// The <PIN_MUX_TABLE> values are the values to be written to the AMX0P
// register to select the P2.<PIN_TABLE> port pins.
// For the 'F340, the AMX0P settings correspond to the following port pins:
//
// AMX0P Port Pin
// 0x00 P2.0
// 0x01 P2.1
// 0x02 P2.2
// 0x03 P2.3
// 0x04 P2.5
// 0x05 P2.6
//
unsigned char idata PIN_TABLE[ANALOG_INPUTS] = {0,1,2,3,5,6};
unsigned char idata PIN_MUX_TABLE[ANALOG_INPUTS] = {0,1,2,3,4,5};
unsigned char AMUX_INPUT = 0; // Index of analog MUX inputs
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
unsigned char i;
unsigned long measurement;
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer