223 lines
5.6 KiB
C
223 lines
5.6 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
|
||
\******************************************************************************/
|
||
#define ADC_AVERAGE_CNT 8
|
||
|
||
/******************************************************************************\
|
||
Variables definitions
|
||
\******************************************************************************/
|
||
xdata uint8_t ADC_Chn_Num = 0;
|
||
|
||
xdata uint16_t g_Value_Adc = 0;
|
||
|
||
xdata uint16_t g_Value_Adc_Avg = 0;
|
||
#if 0
|
||
xdata uint16_t g_Value_Adc0 = 0;
|
||
|
||
xdata uint16_t g_Value_Adc1 = 0;
|
||
|
||
xdata uint16_t g_Vref_Adc = 0;
|
||
#endif
|
||
xdata uint16_t g_Vntc_Adc = 0;
|
||
|
||
xdata uint16_t g_pmu_Adc_Ivol = 0;
|
||
|
||
xdata uint16_t g_pmu_Adc_Ivor = 0;
|
||
|
||
xdata uint16_t g_Vbat_Adc = 0;
|
||
|
||
xdata uint16_t g_pmu_Adc_Vout = 0;
|
||
|
||
xdata uint8_t g_ADC_Chn = 0;
|
||
|
||
xdata uint8_t Adc_Index = 0;
|
||
|
||
|
||
/******************************************************************************\
|
||
Functions definitions
|
||
\******************************************************************************/
|
||
/*
|
||
*******************************************************************************
|
||
* void Adc_Init(void)
|
||
*
|
||
* Description : ADC Initialization. --- 系统初始化调用。( ADC采样转换一次需要19个CLK,一个CLK 4us,转换一次需要76us。)
|
||
*
|
||
* Arguments : NONE
|
||
|
||
* Returns : NONE
|
||
|
||
* Notes : NONE
|
||
*
|
||
*******************************************************************************
|
||
*/
|
||
void Adc_Init(void)
|
||
{
|
||
SFRADDR = MFP_CTL1; //Set P07 As NTC
|
||
SFRDATA |= 0x80;
|
||
|
||
SFRADDR = ADC_CTL0; //0B10111101,<0x29>--ADC EN;ADC 2.5V Enable b[4];ADC1 Pullup 100K Enable b[5]; continue mode; ADC CLK divided by 48=230k
|
||
SFRDATA = 0xB5;
|
||
|
||
ADC_CTL1 = 0x51; //Start ADC , Enable BAT Chn.
|
||
|
||
SFRADDR = IRQ_EN10; //Enable ADC Interrupt(INT6).
|
||
SFRDATA |= 0x01;
|
||
|
||
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 )
|
||
{
|
||
g_ADC_Chn = pmu_Adc_Chn;
|
||
|
||
ADC_CTL1 = ( pmu_Adc_Chn << 4 ) | 0x01; //ADC Start Enable,Set Channel N Enabel.
|
||
|
||
g_Value_Adc_Avg = 0;
|
||
|
||
Adc_Index = 0;
|
||
}
|
||
|
||
/*
|
||
*******************************************************************************
|
||
* 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
|
||
{
|
||
if(IRQ_FLAG10 & ADC_Data_Ready)
|
||
{
|
||
g_Value_Adc = ADC_DATL; //先取低8位数据,锁定当前ADC采样值。
|
||
g_Value_Adc |= (uint16_t)( ( ADC_DATH & 0x03 ) << 8 );
|
||
|
||
//g_Value_Adc = ((uint32_t)g_Value_Adc * 2500) >> 10 ; //ADC的LSB = 2500mV / 1024 = 2.44mV
|
||
|
||
if( Adc_Index < ADC_AVERAGE_CNT ) //求均值
|
||
{
|
||
g_Value_Adc_Avg += g_Value_Adc;
|
||
|
||
Adc_Index++;
|
||
}
|
||
else
|
||
{
|
||
g_Value_Adc_Avg = g_Value_Adc_Avg >> 3;
|
||
|
||
/*获取xSen对应的ADC值*/
|
||
switch (g_ADC_Chn)
|
||
{
|
||
|
||
case ADC_CH1:
|
||
//g_Value_Adc1 = g_Value_Adc_Avg;
|
||
break;
|
||
|
||
case ADC_CH0:
|
||
//g_Value_Adc0 = g_Value_Adc_Avg;
|
||
break;
|
||
|
||
case ADC_NTC:
|
||
|
||
g_Vntc_Adc = g_Value_Adc_Avg;
|
||
|
||
break;
|
||
|
||
case ADC_IVOL:
|
||
|
||
g_Value_Adc_Avg = ((uint32_t)g_Value_Adc_Avg * 2500) >> 10 ; //ADC的LSB = 2500mV / 1024 = 2.44mV
|
||
|
||
g_pmu_Adc_Ivol = g_Value_Adc_Avg / 5; //IVOL电压位VOL的电流放大5倍送给ADC,例如:100mA电流,ADC采样电压为500mV。
|
||
|
||
break;
|
||
|
||
case ADC_IVOR:
|
||
|
||
g_Value_Adc_Avg = ((uint32_t)g_Value_Adc_Avg * 2500) >> 10 ; //ADC的LSB = 2500mV / 1024 = 2.44mV
|
||
|
||
g_pmu_Adc_Ivor = g_Value_Adc_Avg / 5; //IVOR电压位VOR的电流放大5倍送给ADC,例如:100mA电流,ADC采样电压为500mV。
|
||
|
||
break;
|
||
|
||
case ADC_BAT:
|
||
|
||
g_Value_Adc_Avg = ((uint32_t)g_Value_Adc_Avg * 2500) >> 10 ; //ADC的LSB = 2500mV / 1024 = 2.44mV
|
||
|
||
g_Vbat_Adc = g_Value_Adc_Avg << 1; //VBAT的2分压。
|
||
|
||
break;
|
||
|
||
case ADC_VREF:
|
||
//g_Vref_Adc = g_Value_Adc_Avg;
|
||
break;
|
||
|
||
case ADC_VOUT:
|
||
|
||
g_Value_Adc_Avg = ((uint32_t)g_Value_Adc_Avg * 2500) >> 10 ; //ADC的LSB = 2500mV / 1024 = 2.44mV
|
||
|
||
g_pmu_Adc_Vout = g_Value_Adc_Avg << 1; //VOUT的2分压。
|
||
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
|
||
g_Value_Adc_Avg = 0;
|
||
|
||
ADC_CTL1 &= ~0x01; //Disable ADC.
|
||
}
|
||
}
|
||
|
||
IRQ_FLAG10 = ADC_Data_Ready;
|
||
|
||
}
|
||
|
||
|
||
#endif
|
||
|