📄 f500_adc0_externalinput.c
字号:
//-----------------------------------------------------------------------------
// F500_ADC0_ExternalInput.c
//-----------------------------------------------------------------------------
// Copyright 2008 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
// --------------------
//
// This example code takes and averages 2048 analog measurements from input
// P1.2 using ADC0, then prints the results to a terminal window via the UART.
//
// The system is clocked by the internal 24 MHz 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 P1.2 as the ADC0 input. The C8051F50x
// ADC supports single-ended inputs and so the voltage on P1.2 is compared to
// GND.
//
// P1.2 on the C8051F500-TB is connected to a potentiometer. When the
// potentiometer is set to the maximum setting, the voltage input is VIO,
// which is 5V. In order to measure this signal using the on-chip 2.25V
// voltage reference, the on-chip Gain module is used to scale the signal.
//
// 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 | ---- | * 15e3 * 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.
//
// F500 Resources:
// ---------------
// ADC0 : Uses on-chip VREF
// Timer2: overflow initiates ADC conversion
//
// How To Test:
// ------------
// 1) Download code to a 'F500 device on a C8051F500-TB development board
// 2) Connect USB cable from the development board to a PC
// 3) On the PC, open HyperTerminal (or any other terminal program) and connect
// to the USB port (virtual com port) at <BAUDRATE>, 8 data bits, no parity,
// 1 stop bit and no flow control.
// 4) Ensure that a shorting block is put on J20 (potentiometer) and J22 (VREF)
// 5) HyperTerminal will print the voltage measured by the device if
// everything is working properly
//
//
// Target: C8051F500 (Side A of a C8051F500-TB)
// Tool chain: Keil C51 8.0 / Keil EVAL C51
// Command Line: None
//
// Release 1.1 / 10 JUN 2008 (ADT)
// -Edited formatting
//
// Release 1.0 / 03 MAR 2008 (GP)
// -Initial Revision
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <compiler_defs.h>
#include <C8051F500_defs.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24000000 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
SBIT (LED, SFR_P1, 3); // LED==1 means ON
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void PORT_Init (void);
void TIMER2_Init (void);
void ADC0_Init (void);
void UART0_Init (void);
INTERRUPT_PROTO (ADC_ISR, INTERRUPT_ADC0_EOC);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
SFRPAGE = ACTIVE_PAGE; // Set for PCA0MD
PCA0MD &= ~0x40; // Disable the watchdog timer
SYSCLK_Init (); // Initialize system clock
PORT_Init (); // Initialize crossbar and GPIO
TIMER2_Init (); // Init Timer2 for ADC conversion clock
UART0_Init (); // Initialize UART0 for printf's
ADC0_Init (); // Initialize ADC0
EA = 1; // Enable global interrupts
while (1); // Spin forever
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// This routine initializes the system clock to use the internal 24 MHz
// oscillator as its clock source. Also enables missing clock detector reset.
//
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x87; // Configure internal oscillator for
// maximum frequency
RSTSRC = 0x04; // enable missing clock detector
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value: None
// Parameters: None
//
// Configure the Crossbar and GPIO ports.
//
// P0.0 - analog VREF
// P0.4 - digital push-pull UART TX
// P0.5 - digital open-drain UART RX
//
// P1.2 - analog ADC0 analog
// P1.3 - digital push-pull LED
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P0SKIP |= 0x01; // Skip P0.0 (VREF)
P1SKIP |= 0x04; // Skip P1.2 (ADC input)
P0MDOUT |= 0x10; // Set TX pin to push-pull
P0MDIN &= ~0x01; // Set VREF to analog
P1MDOUT |= 0x08; // Enable LED as a push-pull output
P1MDIN &= ~0x04; // Set P1.2 as an analog input
XBR0 = 0x01; // Enable UART0
XBR2 = 0x40; // Enable crossbar and weak pull-ups
SFRPAGE = SFRPAGE_save;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -