笔记:(六)ADC模数转换驱动
#include "include.h"
static volatile u8 getAdcValueOk = 0;
static ADCType AdcValue;
void adcConversiontoStart(void)
{
_pwrc = 0x80;
_pgac0 = 0x00;
_pgac1 = 0x00;
_adcs = 0x1F;
_ador2 = 0;
_ador1 = 1;
_ador0 = 1;
_flms2 = 1;
_flms1 = 1;
_flms0 = 0;
_pgacs = 0x06;
_adoff = 1;
_adslp = 1;
_adrst = 0;
_adrst = 1;
_adrst = 0;
_emi = 1;
_adf = 0;
_ade = 1;
_adcdl = 0;
_eoc = 1;
_adslp = 0;
_adoff = 0;
}
DEFINE_ISR(adcISR, adcVector)
{
static u8 cnt = 0;
if (_eoc == 1)
{
_eoc = 0;
_adcdl = 1;
if (++cnt > 3)
{
AdcValue.AdcByte.byte0 = _adrl;
AdcValue.AdcByte.byte1 = _adrm;
AdcValue.AdcByte.byte2 = _adrh;
getAdcValueOk = 1;
cnt = 0;
}
_adcdl = 0;
}
}
u32 getAdcValue(void)
{
memset(&AdcValue, 0, sizeof(AdcValue));
getAdcValueOk = 0;
adcConversiontoStart();
while (!getAdcValueOk);
adcConversiontoStop();
return AdcValue.Bits32;
}
#ifndef _ADC_H
#define _ADC_H
#include "BH66F2652.h"
#define adcVector 0x0c
#define SETLDO_DISABLE() { _ldoen= 0x00; }
#define SETADCMODE_SLEEP() { _adslp = 1; }
#define SETADCPOWER_OFF() { _adoff = 1; }
#define adcConversiontoStop() { _ldoen=0x00; _adslp=1; _adoff=1;}
#define SETADCDATARATE_10HZ() { _adcs=0x1f; _flms2=0;_flms1=1;_flms0=0; _ador2 = 0;_ador1=0;_ador0=0;}
#define SETADCCHANN_AN1() { _chsn3 = 0; _chsn2 = 0; _chsn1 = 0; _chsn0 = 0;}
#define SETADCCHANP_AN0() { _chsp3 = 0; _chsp2 = 1; _chsp1 = 1; _chsp0 = 0;}
#define SETGAINVRF_0_5() { _vgs1 = 0; _vgs0 = 1; }
#define SETGAINADC_1() { _ags1 = 0; _ags0 = 0; }
#define SETGAINPGA_128() { _pgs2 = 1; _pgs1 = 1; _pgs0 = 1;}
#define SETDCOFSET_0V() { _dcset2 = 0; _dcset1 = 0; _dcset0 = 0; }
#define SETLDO_EN2_4() { _pwrc = 0x80; }
#define SETADCPOWER_ON() { _adoff = 0; }
#define SETADCMODE_NORMAL() { _adslp = 0; }
#define SETADCSTARTCONVERT() { _adrst = 0; _adrst = 1; _adrst = 0; _adcdl = 0; _eoc = 0; }
#define SETADCINTERRUPT_ENABLE() { _adf = 0; _ade = 1;}
extern void getBatteryValue(void);
extern void ChargingManage(void);
#endif