110 lines
3.8 KiB
C
110 lines
3.8 KiB
C
/*!
|
|
* \file adc.c
|
|
*
|
|
* \brief Target adc implementation
|
|
*
|
|
* \copyright Revised BSD License, see section \ref LICENSE.
|
|
*
|
|
* \code
|
|
*
|
|
* _ __ _ ________ _
|
|
* | |/ /(_)___ / ____/ /_ (_)___
|
|
* | // / __ \/ / / __ \/ / __ \
|
|
* / |/ / / / / /___/ / / / / /_/ /
|
|
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
|
* /_/
|
|
* (C) 2022-2025 XinChip
|
|
*
|
|
* \endcode
|
|
*
|
|
* \author ( XinChip ) Alex-J
|
|
*
|
|
* \author ( XinChip )
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------
|
|
INCLUDE HEADE FILES
|
|
------------------------------------------------------------------------------------*/
|
|
#include "adc.h"
|
|
#include "xc_drv_adc.h"
|
|
#include "xc_drv_systick.h"
|
|
#include "main.h"
|
|
/*------------------------------------------------------------------------------------
|
|
Macros
|
|
-------------------------------------------------------------------------------------*/
|
|
|
|
/*------------------------------------------------------------------------------------
|
|
Global Variables
|
|
-------------------------------------------------------------------------------------*/
|
|
|
|
/*------------------------------------------------------------------------------------
|
|
Functions
|
|
-------------------------------------------------------------------------------------*/
|
|
/**
|
|
* @brief adc_demo
|
|
* @details Sampling once per second with 3.3v reference voltage
|
|
* @param void
|
|
* @retval void
|
|
*/
|
|
|
|
void adc_demo(void)
|
|
{
|
|
DEBUG("__ADC_DEMO__\r");
|
|
|
|
uint16_t integer;
|
|
uint16_t decimal;
|
|
uint16_t adc_val;
|
|
uint16_t adc_2v48_param;
|
|
uint16_t after_calibrat_adc_val;
|
|
ADC_InitCfg_t adc_cfg ;
|
|
|
|
/*----- Special Read the 2.48 v calibration value -----*/
|
|
/*< If the content of the parameter
|
|
storage area is invalid, the default assignment is 10000 >*/
|
|
if(false == xc_adc_2v4_calibrate_read(&adc_2v48_param))
|
|
{
|
|
adc_2v48_param = 10000;
|
|
}
|
|
|
|
/*----- Special Pin -----*/
|
|
/*< The SWD and SWCK pins can also be used as ADC pins.
|
|
Pay attention to the function remapping of the PIN pins. >*/
|
|
|
|
adc_cfg.Freq = ADC_FREQ_2M;
|
|
adc_cfg.RefVol = ADC_REF_VOL_2_48V;
|
|
adc_cfg.SampEdge = ADC_SAMPEDGE_RISE;
|
|
adc_cfg.ExtDataMode = ADC_EXT_DATA_MODE_32BIT;
|
|
adc_cfg.ExtEdgeSel = ADC_EXT_EDGE_SEL_INTER;
|
|
adc_cfg.ExtSampleNum = ADC_EXT_SAMPLE_NUM_8;
|
|
adc_cfg.ExtTriggerSel = ADC_EXT_TRIGGER_SEL_PWM0;
|
|
|
|
xc_adc_init(&adc_cfg);
|
|
|
|
while(1)
|
|
{
|
|
|
|
xc_adc_start_get_value( ADC_CH5_PIN1 ,&adc_val);
|
|
|
|
if(adc_cfg.RefVol == ADC_REF_VOL_3_3V)
|
|
{
|
|
integer = ((adc_val)*ADV_REF_VOLT_3_3V*100)/(1.0*4096)/100;
|
|
decimal = ((uint32_t)((adc_val*ADV_REF_VOLT_3_3V*100)/4096)%100);
|
|
|
|
DEBUG("3.3v ref adc_vol=%d.%02d ,adc_val=%d\n", integer, decimal,adc_val);
|
|
}
|
|
else if(adc_cfg.RefVol == ADC_REF_VOL_2_48V)
|
|
{
|
|
after_calibrat_adc_val = adc_val *adc_2v48_param/10000;
|
|
integer = ((adc_val)*ADV_REF_VOLT_2_4_8V*100)/(1.0*4096)/100;
|
|
decimal = ((uint32_t)((adc_val*ADV_REF_VOLT_2_4_8V*100)/4096)%100);
|
|
DEBUG("2.48v ref adc_vol=%d.%02d ,adc_val=%d\n", integer, decimal,adc_val);
|
|
|
|
integer = ((after_calibrat_adc_val)*ADV_REF_VOLT_2_4_8V*100)/(1.0*4096)/100;
|
|
decimal = ((uint32_t)((after_calibrat_adc_val*ADV_REF_VOLT_2_4_8V*100)/4096)%100);
|
|
DEBUG("after_calibrat 2.48v ref adc_vol=%d.%02d ,adc_val=%d\n", integer, decimal,after_calibrat_adc_val);
|
|
}
|
|
delay_ms(1000);
|
|
}
|
|
}
|
|
|