178 lines
4.3 KiB
C
178 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
|
|
\******************************************************************************/
|
|
|
|
idata uint16_t Vbat_Adc = 0;
|
|
|
|
|
|
/******************************************************************************\
|
|
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
|
|
|
|
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("adc4:%d,plus:%d.\r\n",(uint16_t)adc_data,(uint16_t)adc_plus_data);
|
|
#endif
|
|
|
|
}
|
|
|
|
// adc_data = adc_plus_data >> 3 - (char)(ADC_OFFSET);
|
|
adc_data = adc_plus_data >> 3;
|
|
#ifdef _DEBUG_ADC
|
|
printf("adc_chn1:%d.\r\n",(uint16_t)adc_data);
|
|
#endif
|
|
|
|
#ifdef _DEBUG_ADC
|
|
printf("Adc,Vadc:%d,Vref:%d.\r\n", (uint16_t)adc_data, (uint16_t)adc_plus_data);
|
|
#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);
|
|
|
|
#ifdef _DEBUG_ADC
|
|
printf("bat_Vbat1:%d.\r\n",(uint16_t)n_Vbat_Data);
|
|
#endif
|
|
|
|
// n_ADC_Plus = ADC_Chn_Data(ADC_VREF); //获取VREF
|
|
|
|
n_Vbat_Data = (uint32_t)(n_Vbat_Data) * VREF * 2 / ADC_Chn_Data(ADC_VREF); //Vadx = VREF*DATAadx/DATAvref
|
|
|
|
#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;
|
|
|
|
SFRADDR = MFP_CTL1; //Set P07 Pinmux As AD4 Function
|
|
SFRDATA |= 0x80;
|
|
|
|
adc_data = ADC_Chn_Data(ADC_CH4);
|
|
|
|
#ifdef _DEBUG_ADC
|
|
printf("adc_chn1:%d.\r\n",(uint16_t)adc_data);
|
|
#endif
|
|
// adc_plus_data = ADC_Chn_Data(ADC_VREF); //获取VREF
|
|
|
|
adc_data = (uint32_t)(adc_data) * VREF / ADC_Chn_Data(ADC_VREF); //Vadx = VREF*DATAadx/DATAvref
|
|
|
|
#ifdef _DEBUG_ADC
|
|
printf("Adc,Vadc:%d.\r\n", (uint16_t)adc_data);
|
|
#endif
|
|
SFRADDR = MFP_CTL1; //Set P07 Pinmux As AD4 Function
|
|
SFRDATA &= ~0x80;
|
|
|
|
return adc_data;
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|