📄 analog.c
字号:
//-----------------------------------------------------------------------------
// 这部分程序是利用040内部温度传感器采集出温度值及电压的值,然后通过
//以太网将这些值发送出去的.
//-----------------------------------------------------------------------------
#include <string.h>
#include "C8051f040.h"
#include "net.h"
#include "analog.h"
extern char xdata text[];
UINT idata cpu_temperature, air_temperature, cpu_voltage;
UCHAR idata mux_select;
sfr16 ADC0 = 0xbe;
//--------------------------------------------------------------------------
// 初始化ADC
//--------------------------------------------------------------------------
void init_adc(void)
{
char SFRPAGE_SAVE=SFRPAGE;
SFRPAGE = ADC0_PAGE;
ADC0CN = 0x81;
REF0CN = 0x07;
mux_select = MUX_CPU_TEMP;
AMX0SL = MUX_CPU_TEMP;
ADC0CF = (SYSCLK/2500000) << 3;
EIE2 &= ~0x02;
EIE1 &= ~0x04;
SFRPAGE = SFRPAGE_SAVE;
}
//--------------------------------------------------------------------------
// 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 CPu的工作电压
//--------------------------------------------------------------------------
void read_analog_inputs(void)
{
ULONG idata temp_long;
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = ADC0_PAGE;
AD0INT = 0;
AD0BUSY = 1;
while (AD0INT == 0);
switch (mux_select)
{
case MUX_CPU_TEMP:
temp_long = ADC0*2 - 42380;
temp_long= (temp_long * 100L) / 156;
cpu_temperature=temp_long;
AMX0SL = MUX_CPU_VOLTS;
mux_select = MUX_CPU_VOLTS;
break;
case MUX_CPU_VOLTS:
temp_long = ADC0;
temp_long = 24*temp_long/655;
cpu_voltage = temp_long;
AMX0SL = MUX_AIR_TEMP;
mux_select = MUX_AIR_TEMP;
break;
case MUX_AIR_TEMP:
temp_long = ADC0;
temp_long = 24*temp_long/655;
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;
}
SFRPAGE = SFRPAGE_SAVE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -