📄 analog.c
字号:
//-----------------------------------------------------------------------------
// Net ANALOG.C
//
// This module handles the analog inputs which are external temperature
// sensor, the on-chip temperature sensor, and operating voltage.
//-----------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#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 = MUX_CPU_VOLTS; // Select AIN1 for next read
mux_select = MUX_CPU_VOLTS;
break;
case MUX_CPU_VOLTS:
temp_long = ADC0;
temp_long = 24*temp_long/655*10;
cpu_voltage = temp_long;
AMX0SL = MUX_AIR_TEMP; // Select on-chip temp sensor
mux_select = MUX_AIR_TEMP;
break;
case MUX_AIR_TEMP:
temp_long = ADC0;
temp_long = 24*temp_long/655*2;
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -