SY883x_For_Client_JLAB_JS07/UsrSrc/adc/adc.c
Alex xu a29b5cb50b 更改内容:1、将全局变量在定义时不赋初值,需要赋初值的变量在系统初始化函数中赋值,节省ROM占用月100Bytes;
2、删除按键短按显示电量功能;
3、增加开盖Vox输出5V给耳机充电功能,有耳机入盒灯效,无出盒灯效,Vox检测到轻载后延迟10分钟后关闭Vox输出5V,转入ADT模式;
4、增加Vox充电过程中电池电压触发3.6V低电报警阈值后,Vox发送电量码pattern功能。
2025-02-10 11:37:49 +08:00

184 lines
4.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
******************************************************************************
*
* @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;
uint16_t Vref_Adc;
/******************************************************************************\
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