//-----------------------------------------------------------------------------
// F12x_ADC0_ExternalInput.c
//-----------------------------------------------------------------------------
// Program Description:
//
// This program measures the voltage on an external ADC input and prints the
// result to a terminal window via the UART.
//
// The system is clocked using the internal 24.5MHz oscillator multiplied
// up to 49MHz by the on-chip PLL. 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 variable, Result.
//
// The ADC makes repeated measurements at a rate determined by SAMPLE_RATE using
// Timer 3. The end of each ADC conversion initiates an interrupt which calls an
// averaging function. <INT_DEC> samples are averaged then the Result value 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 at AIN0.1. The input
// 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| AIN0.1 | |
// o| | |
// o| --------
// o|
// o|
// 1 o|
// |
//----------
//
// How To Test:
//
// 1) Download code to a 'F12x 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 AIN 0.1, or a potentiometer voltage divider as shown above.
// 5) HyperTerminal will print the voltage measured by the device if
// everything is working properly
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f120.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F12x
//-----------------------------------------------------------------------------
sfr16 ADC0 = 0xbe; // ADC0 data
sfr16 RCAP2 = 0xca; // Timer2 capture/reload
sfr16 RCAP3 = 0xca; // Timer3 capture/reload
sfr16 TMR2 = 0xcc; // Timer2
sfr16 TMR3 = 0xcc; // Timer3
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define uchar unsigned char
#define uint unsigned int
#define SYSCLK 49000000 // Output of PLL derived from (INTCLK*2)
#define SAMPLE_RATE 50000 // Sample frequency in Hz
#define INT_DEC 256 // Integrate and decimate ratio
#define SAR_CLK 2500000 // Desired SAR clock speed
#define SAMPLE_DELAY 50 // Delay in ms before taking sample
#define Lcd_Data P2
sbit RS = P3^5;
sbit RW = P3^6;
sbit EN = P3^7;
sbit LED = P4^6; // LED='1' means ON
sbit SW1 = P4^0; // SW1='0' means switch pressed
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
void ADC0_Init (void);
void TIMER3_Init (int counts);
void ADC0_ISR (void);
void Delay_Ms (unsigned int ms);
void lcd_init(void);
void write_command(uchar command);
void write_data(uchar lcd_data);
void lcd_clear(void);
void write_str(uchar x , uchar y , uchar *s);
void write_char(uchar x , uchar y, uchar lcd_data);
void lcd_init(void);
void lcd_display();
void delayus(uint n);
void Buffer_Updata( int xx );
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
long Result; // ADC0 decimated value
uchar Show_Buffer[ 16 ] = "Votage = 0.000 V";
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
long measurement; // Measured voltage in mV
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize oscillator
PORT_Init (); // Initialize crossbar and GPIO
TIMER3_Init (SYSCLK/SAMPLE_RATE); // Initialize Timer3 to overflow at
// sample rate
ADC0_Init (); // Init ADC
SFRPAGE = ADC0_PAGE;
AD0EN = 1; // Enable ADC
lcd_init();
lcd_clear();
EA = 1; // Enable global interrupts
while (1)
{
EA = 0; // Disable interrupts
measurement = Result * 2430 / 4095;
EA = 1; // Re-enable interrupts
LED = ~SW1; // LED reflects state of switch
Buffer_Updata( measurement );
lcd_display();
Delay_Ms(SAMPLE_DELAY); // Wait 50 milliseconds before taking
// another sample
}
}
void Buffer_Updata( int xx )
{
Show_Buffer[ 9 ] = xx / 1000 + '0';
Show_Buffer[ 11 ] = xx % 1000 /100 + '0';
Show_Buffer[ 12 ] = xx % 1000 %100 / 10 + '0';
Show_Buffer[ 13 ] = xx % 1000 %100 % 10 + '0';
}
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// This function initializes the system clock to use the internal oscillator
// at 24.5 MHz multiplied by two using the PLL.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
int loop; // Software timer
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
//Turn on the PLL and increase the system clock by a factor of M/N = 2
SFRPAGE = CONFIG_PAGE;
PLL0CN = 0x00; // Set internal osc. as PLL source
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x10; // Set FLASH read time for 50MHz clk
// or less
SFRPAGE = CONFIG_PAGE;
PLL0CN |= 0x01; // Enable Power to PLL
PLL0DIV = 0x01; // Set Pre-divide value to N (N = 1)
PLL0FLT = 0x01; // Set the PLL filter register for
// a refer