SY8835_For_Demo_Ourself/UsrSrc/adc/adc.c

205 lines
5.1 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 sy8835
*
* @version 1.0
* @date 2024/11/01 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 2024/11/01 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
\******************************************************************************/
int8_t ADC_Offset_Error = 0; //ADC Offset Error 校准值寄存器
//int8_t ADC_Gain_Error = 0; //ADC Gain Error 校准值寄存器
uint8_t ADC_Chn_Num = 0;
idata uint16_t Vbat_Adc = 0;
idata uint16_t g_Vpmu_Adc = 0;
idata uint16_t g_Vntc_Adc = 0;
#if 0
idata uint16_t g_Vbjt_Adc = 0;
idata uint16_t g_Vp13_Adc = 0;
idata uint16_t g_Vref_Adc = 0;
#endif
uint8_t g_PMU_ADC_Chn = 0;
idata uint16_t g_pmu_Adc_Ivol = 0;
idata uint16_t g_pmu_Adc_Vin = 0;
idata uint16_t g_pmu_Adc_Vpmid = 0;
idata uint16_t g_pmu_Adc_Vor = 0;
idata uint16_t g_pmu_Adc_Vol = 0;
idata uint16_t g_pmu_Adc_Vdd = 0;
idata uint16_t g_pmu_Adc_Ivor = 0;
/******************************************************************************\
Functions definitions
\******************************************************************************/
/*
*******************************************************************************
* void Adc_Init(void)
*
* Description : ADC Initialization. --- 系统初始化调用。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void Adc_Init(void)
{
SFRADDR = ADC_CTL2; //ADC 2.5V Enable b[7];ADC1 Pullup 100K Enable b[0].
SFRDATA = 0x81;
SFRADDR = MFP_CTL0; //Set P14 As ADC1 For NTC
SFRDATA |= 0x10;
ExtSfr_Write(ADC_CTL0,0xAB); //0B10101001,0xA9--ADC EN;Average time:16; Single mode; ADC CLK divided by 24=460k
// SFRADDR = ADC_IntEn; //Enable ADC Interrupt.b[4:0]:ADC4-ADC0
// SFRDATA |= 0x03;
SFRADDR = ADC_OFFSET;
ADC_Offset_Error = SFRDATA;
}
/*
*******************************************************************************
* void PMU_ADC_Chn_Data(PMU_ADC_CHANNELS_E pmu_Adc_Chn)
*
* Description : 设置相应通道的使能。在主循环中按通道设置各个通道的使能在中断中获取对应的ADC值减少主函数的阻塞。
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void PMU_ADC_Chn_Data(PMU_ADC_CHANNELS_E pmu_Adc_Chn)
{
if( ADC_IntFlag & 0x01 )
{
ADC_IntFlag = 0x01;
g_Vpmu_Adc = ExtSfr_Read(ADC_CH0_L);
g_Vpmu_Adc |= (uint16_t)( ( ExtSfr_Read(ADC_CH0_H) & 0x03 ) << 8 );
I2cSfr_Write( XSEN_CTRL , (0x08 | pmu_Adc_Chn) ); //Xsen切换通道后需要等待至少100us后ADC再进行采样。
g_Vpmu_Adc = ((uint32_t)( g_Vpmu_Adc - ADC_Offset_Error ) * 2500) >> 10 ; //ADC的LSB = 2500mV / 1024 = 2.44mV
/*获取xSen对应的ADC值*/
switch (g_PMU_ADC_Chn)
{
case pmu_ADC_IVOL:
g_pmu_Adc_Ivol = g_Vpmu_Adc >> 2; //采样比例4mV/mA
break;
case pmu_ADC_VBAT:
Vbat_Adc = g_Vpmu_Adc << 1; //vbat的2分压
break;
case pmu_ADC_VIN:
g_pmu_Adc_Vin = g_Vpmu_Adc << 3; //vin的8分压
break;
case pmu_ADC_VPMID:
g_pmu_Adc_Vpmid = g_Vpmu_Adc << 2; //PMID的4分压
break;
case pmu_ADC_VOR:
g_pmu_Adc_Vor = g_Vpmu_Adc << 2; //vor的4分压
break;
case pmu_ADC_VOL:
g_pmu_Adc_Vol = g_Vpmu_Adc << 2; //vol的4分压
break;
case pmu_ADC_VDD:
g_pmu_Adc_Vdd = g_Vpmu_Adc << 1; //vdd的2分压
break;
case pmu_ADC_IVOR:
g_pmu_Adc_Ivor = g_Vpmu_Adc >> 2; //采样比例4mV/mA
break;
default:
break;
}
}
g_PMU_ADC_Chn = pmu_Adc_Chn;
ADC_CTL1 = 0x80 | ( ADC_PMU_CH0 ); //ADC Start Enable,Set Channel N Enabel.
}
/*
*******************************************************************************
* void ADC_NTC_Data(void)
*
* Description : 获取NTC ADC值
*
* Arguments : NONE
* Returns : NONE
* Notes : NONE
*
*******************************************************************************
*/
void ADC_NTC_Data(void)
{
if( ADC_IntFlag & 0x02 ) //获取NTC ADC 码值
{
ADC_IntFlag = 0x02;
g_Vntc_Adc = ExtSfr_Read(ADC_CH1_L);
g_Vntc_Adc |= (uint16_t)( ( ExtSfr_Read(ADC_CH1_H) & 0x03 ) << 8 );
g_Vntc_Adc = g_Vntc_Adc - ADC_Offset_Error ;
}
ADC_CTL1 = 0x80 | ( ADC_NTC_CH1 ); //ADC Start Enable,Set Channel N Enabel.
}
#endif