204 lines
5.0 KiB
C
204 lines
5.0 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 Vref_AdcValue(void)
|
||
*
|
||
* Description : Vref Value.»ñÈ¡»ù×¼²Î¿¼µçѹADCÖµ¡£
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : Iref Value (mA)
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
|
||
static uint16_t Vref_AdcValue(void)
|
||
{
|
||
uint8_t index = 0;
|
||
uint16_t n_Vref_Data = 0;
|
||
uint16_t n_ADC_Plus = 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 = 0x61; //Channel set and Start conversion
|
||
|
||
while(ADC_CTL1 & 0x01); //Wait for conversion complete
|
||
|
||
n_Vref_Data = ADC_DATL;
|
||
|
||
n_Vref_Data |= (uint16_t)(ADC_DATH << 8);
|
||
|
||
n_ADC_Plus += n_Vref_Data;
|
||
|
||
n_Vref_Data = 0;
|
||
}
|
||
|
||
n_Vref_Data = (n_ADC_Plus / 8);
|
||
|
||
return n_Vref_Data;
|
||
}
|
||
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* uint16_t Vbat_Value(void)
|
||
*
|
||
* Description : Vbat Value.»ñÈ¡µç³Øµçѹ¡£
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : Vbat Value (mV)
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
uint16_t Vbat_Value(void)
|
||
{
|
||
uint8_t index = 0;
|
||
uint16_t n_Vbat_Data = 0;
|
||
uint16_t n_ADC_Plus = 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 = 0x51; //Channel set and Start conversion
|
||
|
||
while(ADC_CTL1 & 0x01); //Wait for conversion complete
|
||
|
||
n_Vbat_Data = ADC_DATL;
|
||
|
||
n_Vbat_Data |= (uint16_t)(ADC_DATH << 8);
|
||
|
||
n_ADC_Plus += n_Vbat_Data;
|
||
#ifdef _DEBUG_ADC
|
||
printf("Vbat:%d,plus:%d.\r\n",(uint16_t)n_Vbat_Data,(uint16_t)n_ADC_Plus);
|
||
#endif
|
||
n_Vbat_Data = 0;
|
||
}
|
||
|
||
n_Vbat_Data = ( n_ADC_Plus / 4 ); //Vbat²ÉÑùֵΪ¶þ·Öѹֵ¡£
|
||
#ifdef _DEBUG_ADC
|
||
printf("bat_Vbat1:%d.\r\n",(uint16_t)n_Vbat_Data);
|
||
#endif
|
||
|
||
n_ADC_Plus = Vref_AdcValue(); //»ñÈ¡VREF
|
||
|
||
n_Vbat_Data = (uint32_t)(n_Vbat_Data) * VREF / n_ADC_Plus; //Vadx = VREF*DATAadx/DATAvref
|
||
|
||
#ifdef _DEBUG_ADC
|
||
printf("bat,Vbat:%d,vref:%d.\r\n",(uint16_t)n_Vbat_Data, n_ADC_Plus);
|
||
#endif
|
||
|
||
return n_Vbat_Data;
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* uint16_t ADC_Chn_Value(ADC_CHANNELS_E adc_chn)
|
||
*
|
||
* Description : ADC_Chn_Value. ADC0 ~ ADC4¡£
|
||
|
||
* Arguments : NONE
|
||
|
||
* Returns : ADC Value (mV)
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
#if ADC_CHN_ENABLE
|
||
|
||
uint16_t ADC_Chn_Value(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 = 0;
|
||
}
|
||
|
||
adc_data = adc_plus_data / 8;
|
||
#ifdef _DEBUG_ADC
|
||
printf("adc_chn1:%d.\r\n",(uint16_t)adc_data);
|
||
#endif
|
||
adc_plus_data = Vref_AdcValue(); //»ñÈ¡VREF
|
||
|
||
adc_data = (uint32_t)(adc_data) * VREF / adc_plus_data; //Vadx = VREF*DATAadx/DATAvref
|
||
|
||
#ifdef _DEBUG_ADC
|
||
printf("Adc,Vadc:%d,Vref:%d.\r\n", (uint16_t)adc_data, (uint16_t)adc_plus_data);
|
||
#endif
|
||
|
||
return adc_data;
|
||
}
|
||
#endif
|
||
|
||
#endif
|
||
|