184 lines
4.3 KiB
C
184 lines
4.3 KiB
C
/*
|
||
******************************************************************************
|
||
*
|
||
* @file adc.c
|
||
* @brief adc module
|
||
* @ic TP3102
|
||
*
|
||
* @version 1.0
|
||
* @date 2024/03/26 15:00:40
|
||
* @author Alex Xu
|
||
*
|
||
* Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd.
|
||
* All Rights Reserved
|
||
*
|
||
* History:
|
||
* Revision Date Author Desc
|
||
* 1.0.0 2022/08/04 Alex build this file
|
||
******************************************************************************/
|
||
/*_____ I N C L U D E S ____________________________________________________*/
|
||
|
||
#include "adc.h"
|
||
#include "system.h"
|
||
#include "sys_tim.h"
|
||
|
||
#if ADC_ENABLE
|
||
|
||
/******************************************************************************\
|
||
Macro definitions
|
||
\******************************************************************************/
|
||
|
||
/******************************************************************************\
|
||
Variables definitions
|
||
\******************************************************************************/
|
||
|
||
uint16_t Vbat_Adc = 0;
|
||
|
||
uint16_t Vref_Adc = 311;
|
||
|
||
/******************************************************************************\
|
||
Functions definitions
|
||
\******************************************************************************/
|
||
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* uint16_t ADC_Chn_Data(ADC_CHANNELS_E adc_chn)
|
||
*
|
||
* Description : 获取相应通道的电压ADC值。
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : adc Value (mV)
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
|
||
uint16_t ADC_Chn_Data(ADC_CHANNELS_E adc_chn)
|
||
{
|
||
uint8_t index = 0;
|
||
uint16_t adc_data = 0;
|
||
uint16_t adc_plus_data = 0;
|
||
|
||
SFRADDR = ADC_CTL0;
|
||
SFRDATA = 0x8B; //0x8B--ADC EN; Single mode; ADC CLK divided by 24=460k
|
||
|
||
#if 1
|
||
if( adc_chn == ADC_VREF ) //采VREF前,需要先拉低ADC4对应的IO口进行放电。
|
||
{
|
||
SFRADDR = P0_PD; //打开P07的下拉
|
||
SFRDATA |= 0x80;
|
||
|
||
ADC_CTL1 = (ADC_CH4 << 4) | 0x01;
|
||
|
||
while(ADC_CTL1 & 0x01); //Wait for conversion complete
|
||
|
||
ADC_CTL1 = (ADC_VREF << 4) | 0x01;
|
||
|
||
SFRADDR = P0_PD; //取消P07的下拉
|
||
SFRDATA &= ~0x80;
|
||
|
||
#ifdef _DEBUG_ADC
|
||
printf("adc bug(vref:%d)\r\n",(uint16_t)adc_chn);
|
||
#endif
|
||
}
|
||
#endif
|
||
|
||
for(index=0; index<8; index++)
|
||
{
|
||
ADC_CTL1 = (adc_chn << 4) | 0x01; //Channel set and Start conversion
|
||
|
||
while(ADC_CTL1 & 0x01); //Wait for conversion complete
|
||
|
||
adc_data = ADC_DATL; //Read low byte first! Lock ADC Value.
|
||
|
||
adc_data |= (uint16_t)(ADC_DATH << 8);
|
||
|
||
adc_plus_data += adc_data;
|
||
|
||
#ifdef _DEBUG_ADC
|
||
printf("adc:%d,plus:%d,index:%d.\r\n",(uint16_t)adc_data,(uint16_t)adc_plus_data,(uint16_t)index);
|
||
#endif
|
||
}
|
||
|
||
adc_data = adc_plus_data >> 3;
|
||
|
||
#ifdef _DEBUG_ADC
|
||
printf("Adc,Vadc:%d,Vadc_plus:%d,adc_chn:%d.\r\n", (uint16_t)adc_data, (uint16_t)adc_plus_data,(uint16_t)adc_chn);
|
||
#endif
|
||
|
||
return adc_data;
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* uint16_t Vbat_Value(void)
|
||
*
|
||
* Description : Vbat Value.获取电池电压。
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : Vbat Value (mV)
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
uint16_t Vbat_Value(void)
|
||
{
|
||
uint16_t n_Vbat_Data = 0;
|
||
|
||
n_Vbat_Data = ADC_Chn_Data(ADC_BAT);
|
||
|
||
n_Vbat_Data = (uint32_t)(n_Vbat_Data) * VREF * 2 / Vref_Adc;
|
||
|
||
#ifdef _DEBUG_ADC
|
||
printf("bat,Vbat:%d.\r\n",(uint16_t)n_Vbat_Data);
|
||
#endif
|
||
|
||
return n_Vbat_Data;
|
||
}
|
||
|
||
#ifdef NTC_ENABLE
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* uint16_t ADC_NTC_Value(void)
|
||
*
|
||
* Description : 获取NTC采样电压
|
||
|
||
* Arguments : NONE
|
||
|
||
* Returns : ADC Value (mV)
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
|
||
uint16_t ADC_NTC_Value(void)
|
||
{
|
||
uint16_t adc_data = 0;
|
||
|
||
adc_data = ADC_Chn_Data(ADC_CH4);
|
||
|
||
#ifdef _DEBUG_ADC
|
||
printf("NTC_adc,Vadc:%d.\r\n", (uint16_t)adc_data);
|
||
#endif
|
||
|
||
adc_data = (uint32_t)(adc_data) * VREF / Vref_Adc;
|
||
|
||
#ifdef _DEBUG_ADC
|
||
printf("NTC_V,Vadc:%d.\r\n", (uint16_t)adc_data);
|
||
#endif
|
||
|
||
return adc_data;
|
||
}
|
||
|
||
#endif
|
||
|
||
#endif
|
||
|