www.pudn.com > WEB020.rar > Analog.c
//----------------------------------------------------------------------------- // Copyright (c) 2002 Jim Brady // Do not use commercially without author's permission // Last revised August 2002 // Net ANALOG.C // // This module handles the analog inputs which are external temperature // sensor, the on-chip temperature sensor, and operating voltage. //----------------------------------------------------------------------------- #include#include #include "C8051f.h" #include "net.h" #include "serial.h" #include "analog.h" extern char xdata text[]; UINT idata cpu_temperature, air_temperature, cpu_voltage; UCHAR idata mux_select; sfr16 ADC0 = 0xbe; // ADC0 data //-------------------------------------------------------------------------- // Initialize the A/D converter // //-------------------------------------------------------------------------- void init_adc(void) { ADC0CN = 0x81; // ADC0 enabled; normal tracking // mode; ADC0 conversions are initiated // on write to AD0BUSY; ADC0 data is // left-justified REF0CN = 0x07; // enable temp sensor, on-chip VREF, // and VREF output buffer mux_select = MUX_CPU_TEMP; // CPU on-chip temp sensor AMX0SL = MUX_CPU_TEMP; ADC0CF = (SYSCLK/2500000) << 3; // ADC conversion clock = 2.5MHz // ADC0CF |= 0x01; // PGA gain = 2 EIE2 &= ~0x02; // disable ADC0 EOC interrupt EIE1 &= ~0x04; // disable ADC0 window compare interrupt } //-------------------------------------------------------------------------- // This function is a little state machine which reads one analog // inputs at a time, out of the 3 possible inputs // 1. On-chip temperature // 2. External air temperature // 3. CPU operating voltage //-------------------------------------------------------------------------- void read_analog_inputs(void) { ULONG idata temp_long; AD0INT = 0; // clear conversion complete indicator AD0BUSY = 1; // initiate conversion while (AD0INT == 0); // wait for conversion complete switch (mux_select) { case MUX_CPU_TEMP: temp_long = ADC0 - 42380/2; temp_long= (temp_long * 200L) / 156; cpu_temperature=temp_long; AMX0SL = 0x00; // Select AIN1 for next read mux_select = MUX_CPU_VOLTS; break; case MUX_CPU_VOLTS: temp_long = ADC0; temp_long = 24*temp_long/6552; cpu_voltage = temp_long; AMX0SL = 0x01; // Select on-chip temp sensor mux_select = MUX_AIR_TEMP; break; case MUX_AIR_TEMP: temp_long = ADC0; temp_long = 33*temp_long/6552; air_temperature = temp_long; AMX0SL = MUX_CPU_TEMP; mux_select = MUX_CPU_TEMP; break; default: AMX0SL = MUX_CPU_TEMP; mux_select = MUX_CPU_TEMP; break; } }