笔记:合泰单片机BH66F2652开发(六)ADC

笔记:(六)ADC模数转换驱动

#include "include.h"


static volatile u8 	getAdcValueOk = 0;
static ADCType  AdcValue;
/*******************************************************************************
 * fuction	adcInit	
 * brief	ADC转换初始化
 * param	无
 * return	无
 *******************************************************************************/  
void adcConversiontoStart(void)//AN1
{
    
    	
//	SETADCDATARATE_10HZ();
//	SETADCCHANN_AN1();
//	SETADCCHANP_AN0();
//	SETGAINVRF_0_5();
//	SETGAINADC_1();
//	SETGAINPGA_128();
//	_inis 	= 0;/**/  //或者直接写_pgac1 = 0x00;
//	_inx1 	= 0;/**/
//	_inx0 	= 0;/**/
//	SETDCOFSET_0V();
//	SETLDO_EN2_4();
//	SETADCPOWER_ON();
//	SETADCMODE_NORMAL();
//	SETADCSTARTCONVERT();
//	_emi    = 1;
//	SETADCINTERRUPT_ENABLE();



/*	_pwrc = 0x83;//使能LDO 3.3v*/
	_pwrc = 0x80;//使能LDO 2.4v
	
	_pgac0 = 0x00;
/*	_pgac0 = 0x27;*/

	_pgac1 = 0x00;
	_adcs  = 0x1F; 
	_ador2 = 0; //OSR = 0248;
	_ador1 = 1;
	_ador0 = 1;
	_flms2 = 1;
	_flms1 = 1;
	_flms0 = 0; 
	_pgacs = 0x06;
	_adoff = 1;	//关闭ADC转换器模块电源  如使用则设置为0
	_adslp = 1; //打开休眠模式 进入低功耗 若使用ADC 则设置为0
	_adrst = 0; //0正常工作  从0到1则复位  再到0就正常工作
	_adrst = 1; 
	_adrst = 0; //0正常工作
	_emi   = 1;
	_adf   = 0;
	_ade   = 1; //判断 _adf = 1;说明有中断请求    0x0c
	_adcdl = 0;
	_eoc   = 1;
	_adslp = 0;	//正常工作模式  非睡眠
	_adoff = 0;	//打开ADC模块电源
} 
/*******************************************************************************
 * fuction	adcISR	
 * brief	ADC中断
 * param	adcISR:函数名   adcVector:向量
 * return	无
 *******************************************************************************/  
/*u8 adcbuff[5]={0xAA,0,0,0,0xBB};*/
DEFINE_ISR(adcISR, adcVector) 
{
    
    
	static u8 cnt = 0;
	
	if (_eoc == 1)    //ADC 转换完成
	{
    
    
		_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;//数据读取完成,取消锁存
	}
}
/*******************************************************************************
 * fuction	getAdcValue
 * brief	获取ADC值
 * param	无
 * return	无
 *******************************************************************************/  
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; }//Vref增益控制選擇	程序裡面可隨時切換
#define SETGAINADC_1()				{ _ags1 = 0; _ags0 = 0; }//ADC增益控制選擇	程序裡面可隨時切換
#define SETGAINPGA_128()			{ _pgs2 = 1; _pgs1 = 1; _pgs0 = 1;}//PGA增益控制選擇	程序裡面可隨時切換
#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
/*--------------------------------END THE FILE-----------------------------------*/

猜你喜欢

转载自blog.csdn.net/FutureStudio1994/article/details/114128080