223 lines
5.5 KiB
C
223 lines
5.5 KiB
C
/*
|
||
******************************************************************************
|
||
*
|
||
* @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
|
||
\******************************************************************************/
|
||
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;
|
||
|
||
idata uint16_t g_Vbjt_Adc = 0;
|
||
|
||
idata uint16_t g_Vp13_Adc = 0;
|
||
|
||
idata uint16_t g_Vref_Adc = 0;
|
||
|
||
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
|
||
|
||
// ADC_CTL1 = (ADC_PMU_CH0 + 1) | (ADC_NTC_CH1 + 1) | 0x80; //ADC Start Enable,Set Channel N Enabel.
|
||
|
||
SFRADDR = ADC_IntEn; //Enable ADC Interrupt.b[4:0]:ADC4-ADC0
|
||
SFRDATA |= 0x03;
|
||
|
||
EX6 = 1; //ADC IRQ Enable
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* 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)
|
||
{
|
||
I2cSfr_Write( XSEN_CTRL , (0x08 | pmu_Adc_Chn) );
|
||
|
||
g_PMU_ADC_Chn = pmu_Adc_Chn;
|
||
|
||
ADC_CTL1 = 0x81; //ADC Start Enable,Set Channel N Enabel.
|
||
|
||
SFRADDR = ADC_IntEn; //Enable ADC Interrupt.b[4:0]:ADC4-ADC0
|
||
SFRDATA = 0x01;
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* void EX6_ADC_isr(void ) interrupt Interrupt_Vector_IE6
|
||
*
|
||
* Description : System External Intterupt 6 adc Interrupt.
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void EX6_ADC_isr(void) interrupt Interrupt_Vector_IE6
|
||
{
|
||
uint8_t reg_addr_bk = 0;
|
||
|
||
reg_addr_bk = SFRADDR;
|
||
|
||
if(ADC_IntFlag & 0x01)
|
||
{
|
||
g_Vpmu_Adc = ExtSfr_Read(ADC_CH0_L);
|
||
g_Vpmu_Adc |= (uint16_t)( ( ExtSfr_Read(ADC_CH0_H) & 0x03 ) << 8 );
|
||
|
||
g_Vpmu_Adc = ((uint32_t)g_Vpmu_Adc * 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;
|
||
}
|
||
}
|
||
|
||
if(ADC_IntFlag & 0x02)
|
||
{
|
||
g_Vntc_Adc = ExtSfr_Read(ADC_CH1_L);
|
||
g_Vntc_Adc |= (uint16_t)( ( ExtSfr_Read(ADC_CH1_H) & 0x03 ) << 8 );
|
||
}
|
||
|
||
if(ADC_IntFlag & 0x04)
|
||
{
|
||
g_Vbjt_Adc = ExtSfr_Read(ADC_CH2_L);
|
||
g_Vbjt_Adc |= (uint16_t)( ( ExtSfr_Read(ADC_CH2_H) & 0x03 ) << 8 );
|
||
}
|
||
|
||
if(ADC_IntFlag & 0x08)
|
||
{
|
||
g_Vp13_Adc = ExtSfr_Read(ADC_CH3_L);
|
||
g_Vp13_Adc |= (uint16_t)( ( ExtSfr_Read(ADC_CH3_H) & 0x03 ) << 8 );
|
||
}
|
||
|
||
if(ADC_IntFlag & 0x10)
|
||
{
|
||
g_Vref_Adc = ExtSfr_Read(ADC_CH4_L);
|
||
g_Vref_Adc |= (uint16_t)( ( ExtSfr_Read(ADC_CH4_H) & 0x03 ) << 8 );
|
||
}
|
||
|
||
ADC_IntFlag = 0x0F;
|
||
|
||
SFRADDR = reg_addr_bk;
|
||
}
|
||
|
||
|
||
#endif
|
||
|