📄 f36x_adc0_externalinput.c
字号:
//-----------------------------------------------------------------------------
// F36x_ADC0_ExternalInput.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
// --------------------
//
// This example code takes and averages 2048 analog measurements from input
// P2.5 using ADC0, then prints the results to a terminal window via the UART.
//
// The system is clocked by the internal 24.5MHz oscillator. Timer 2 triggers
// a conversion on ADC0 on each overflow. The completion of this conversion
// in turn triggers an interrupt service routine (ISR). The ISR averages
// 2048 measurements, then prints the value to the terminal via printf before
// starting another average cycle.
//
// The analog multiplexer selects P2.5 as the positive ADC0 input. This
// port is configured as an analog input in the port initialization routine.
// The negative ADC0 input is connected via mux to ground, which provides
// for a single-ended ADC input.
//
// A 100kohm potentiometer may be connected as a voltage divider between
// VREF and AGND on the terminal strip as shown below:
//
// ---------
// |
// o| VREF ----|
// o| GND ---|<-|
// o| P2.5 | |
// o| | |
// o| --------
// o|
// |
//----------
// C8051F360-TB
//
// Terminal output is done via printf, which directs the characters to
// UART0. A UART initialization routine is therefore necessary.
//
// 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-------|==Conversion==|----Settling--- . . .
// Timer 2 overflow ^
// ADC0 ISR ^
//
// 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^10 |
// Tsettle = ln | ---- | * 105e3 * 5e-12 = 4.4uS
// | 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 10 periods of the SAR clock <SAR_CLK>. At 3 MHz,
// this time is 10 * 400nS = 3.3 uS.
//
//
// Tsample, minimum = Tsettle + Tconvert
// = 4.4uS + 3.3uS
// = 7.7 uS
//
// Timer 2 is set to start a conversion every 100uS, which is far longer
// than the minimum required.
//
// F360 Resources:
// ---------------
// Timer1: clocks UART
// Timer2: overflow initiates ADC conversion
//
// How To Test:
// ------------
// 1) Download code to a 'F360 device on a C8051F360-TB development board
// 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 P2.5, or a potentiometer voltage divider as shown above.
// 5) HyperTerminal will print the voltage measured by the device if
// everything is working properly
//
// FID: 36X000008
// Target: C8051F360
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Command Line: None
//
//
// Release 1.0
// -Initial Revision (TP / CLM)
// -13-Oct-06
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <C8051F360.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F36x
//-----------------------------------------------------------------------------
sfr16 TMR2RL = 0xCA; // Timer2 reload value
sfr16 TMR2 = 0xCC; // Timer2 counter
sfr16 ADC0 = 0xBD; // ADC0 result
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void Timer2_Init(void);
void ADC0_Init(void);
void UART0_Init (void);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
SYSCLK_Init (); // Initialize system clock to
// 24.5MHz
PORT_Init (); // Initialize crossbar and GPIO
Timer2_Init(); // Init Timer2 to generate
// overflows to trigger ADC
UART0_Init(); // Initialize UART0 for printf's
ADC0_Init(); // Initialize ADC0
EA = 1; // Enable global interrupts
SFRPAGE = LEGACY_PAGE;
while (1) { // Spin forever
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// This routine initializes the system clock to use the internal 24.5MHz / 8
// oscillator as its clock source. Also enables missing clock detector reset.
//
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
OSCICN = 0x83; // Configure internal oscillator for
// 24.5MHz
RSTSRC = 0x04; // Enable missing clock detector
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// Configure the Crossbar and GPIO ports.
//
// Pinout:
//
// P0.1 - UART TX (push-pull)
// P0.2 - UART RX
//
// P2.5 - ADC0 analog input
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
XBR0 = 0x01; // Enable UART0
XBR1 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x02; // Set TX pin to push-pull
P2MDIN &= ~0x20; // Set P2.5 as an analog input
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at 100us
// intervals. Timer 2 overflow automatically triggers ADC0 conversion.
//
//-----------------------------------------------------------------------------
void Timer2_Init (void)
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// use SYSCLK as timebase, 16-bit
// auto-reload
CKCON |= 0x10; // Select SYSCLK for Timer 2 source
TMR2RL = 65535 - (SYSCLK / 10000); // Init reload value for 100us
TMR2 = 0xFFFF; // Set to reload immediately
TR2 = 1; // Start Timer2
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// Configures ADC0 to make single-ended analog measurements on pin P2.5
//
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
ADC0CN = 0x02; // ADC0 disabled, normal tracking,
// conversion triggered on TMR2 overflow
REF0CN = 0x03; // Enable on-chip VREF and buffer
AMX0P = 0x0D; // ADC0 positive input = P2.5
AMX0N = 0x1F; // ADC0 negative input = GND
// i.e., single ended mode
ADC0CF = ((SYSCLK/3000000)-1)<<3; // Set SAR clock to 3MHz
ADC0CF &= ~0x04; // Right-justify results
EIE1 |= 0x08; // Enable ADC0 conversion complete int.
AD0EN = 1; // Enable ADC0
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
if (SYSCLK/BAUDRATE/2/256 < 1)
{
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON |= 0x08; // T1M = 1; SCA1:0 = xx
}
else if (SYSCLK/BAUDRATE/2/256 < 4)
{
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
}
else if (SYSCLK/BAUDRATE/2/256 < 12)
{
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
}
else if (SYSCLK/BAUDRATE/2/256 < 48)
{
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
else
{
while (1); // Error. Unsupported baud rate
}
TL1 = TH1; // Init Timer1
TMOD &= ~0xF0; // TMOD: Timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
// ADC0_ISR
//-----------------------------------------------------------------------------
//
// This ISR averages 2048 samples then prints the result to the terminal. The
// ISR is called after each ADC conversion which is triggered by Timer2.
//
//-----------------------------------------------------------------------------
void ADC0_ISR (void) interrupt 10
{
static unsigned long accumulator = 0; // Accumulator for averaging
static unsigned int measurements = 2048; // Measurement counter
unsigned long result=0;
unsigned long mV; // Measured voltage in mV
AD0INT = 0; // Clear ADC0 conversion complete flag
accumulator += ADC0;
measurements--;
// If 2048 measurements have been taken...
if(measurements == 0)
{
measurements = 2048; // Reset the number of measurements
result = accumulator / 2048; // Average the measurements
accumulator = 0; // Reset the accumulator
// The 10-bit ADC value is averaged across 2048 measurements.
// The measured voltage applied to P2.5 is then:
//
// Vref (mV)
// measurement (mV) = --------------- * result (bits)
// (2^10)-1 (bits)
mV = result * 2440 / 1023; // Calculate the average value in mV
printf("P2.5 voltage: %ld mV\n",mV);
}
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -