这是基于STC系列的模拟数字转换器驱动,由于写的时间比较早,没有格式化为DV的方式,代码如下:
//以下是GasADC.H头文件部分
#ifndef __GASADC__
#define __GASADC__
#include "stc15w.h"
/*************************************************************************************************
内部AD采样
**************************************************************************************************/
#define ADC_POWER 0x10 //ADC电源控制位
#define ADC_FLAG 0x11 //ADC完成标志
#define ADC_START 0x07 //ADC起始控制位
#define ADC_SPEEDLL 0x00 //540个时钟
#define ADC_SPEEDL 0x02 //360个时钟
#define ADC_SPEEDH 0x04 //180个时钟
#define ADC_SPEEDHH 0x06 //90个时钟
//接口函数
void GasADCInit();
void GasADCServes();
extern unsigned char GasMADC,GasPADC;
#endif
//以下是GasADC.c正文文件部分
#include "GasADC.h"
#include "stc15w_uart.h"
static unsigned char GasADC[6];//全局的变量用来存储ADC转换结果。4通道转换结果时刻进行
unsigned char GasMADC,GasPADC;
static unsigned char index;
bit chsw = 0;
bit ADok = 0;
/************************************************************************************************
输入:
输出:
功能:ADC采样初始化
*************************************************************************************************/
void GasADCInit()
{
P1ASF = 0xf0;//设置P1口高4位为AD口
ADC_RES = 0;//清除结果寄存器
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | 4;//最低速运行
EADC = 1;//使能ADC中断
index = 0;
GasMADC = 0;
GasPADC = 0;
}
/************************************************************************************************
输入:
输出:
功能:软件滤波整形
*************************************************************************************************/
void GasADCServes()
{
unsigned int gas = 0;
if (!ADok) return;
gas = (GasADC[0] + GasADC[2] + GasADC[4] + GasADC[6] + GasADC[8]) / 5;
GasMADC = gas;
gas = (GasADC[1] + GasADC[3] + GasADC[5] + GasADC[7] + GasADC[9]) / 5;
GasPADC = gas;
}
/************************************************************************************************
输入:
输出:
功能:主要采样服务,采样所得被转移到固定数组,中断方式进行
*************************************************************************************************/
void GasADCInter() interrupt 5 using 1
{
static unsigned char ch = 1;
ADC_CONTR &= !ADC_FLAG; //清除ADC中断标志
GasADC[index++] = ADC_RES;
if(index > 7){
index = 0;
ADok = 1;
}
chsw = !chsw;
ch = 4 + (chsw ? 1 : 0);
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
}