188 lines
5.6 KiB
C
188 lines
5.6 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"
|
|
/*------------------------------------------------------------------------------------
|
|
Macros
|
|
-------------------------------------------------------------------------------------*/
|
|
|
|
/*------------------------------------------------------------------------------------
|
|
Global Variables
|
|
-------------------------------------------------------------------------------------*/
|
|
uint8_t adc_ch_buff[1000];
|
|
uint32_t adc_val_buff[500];
|
|
uint16_t adc_con = 0;
|
|
/*------------------------------------------------------------------------------------
|
|
Functions
|
|
-------------------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
* @brief adc_channel_hw_demo
|
|
* @details
|
|
* @param void
|
|
* @retval void
|
|
*/
|
|
void adc_channelswitch_hw_demo(void)
|
|
{
|
|
DEBUG("ADC channelswitch hw demo\n");
|
|
|
|
uint32_t val;
|
|
uint16_t integer;
|
|
uint16_t decimal;
|
|
ADC_InitCfg_t adc_cfg;
|
|
ADC_FIFO_TypeDef_t *fifo_val = NULL;
|
|
uint8_t timer0_cnt;
|
|
uint16_t timer1_cnt;
|
|
|
|
delay_ms(500);
|
|
|
|
// Prevent GPIO6 from being connected to the op amp,
|
|
// causing GADC\pull-up invalidation problem
|
|
cpr_opa_ctrl_reg__pdbias__setf(DISABLE);
|
|
|
|
GPIO_InitCfg_t GPIO_InitCfg = {0};
|
|
GPIO_InitCfg.Mux = GPIO_Mux0;
|
|
GPIO_InitCfg.FunSel = GPIO_Dx;
|
|
GPIO_InitCfg.Pull = GPIO_NOPULL;
|
|
// Board Led1 Initialization
|
|
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
|
|
GPIO_InitCfg.Int = NOT_INT;
|
|
GPIO_InitCfg.Pin = GPIO_20;
|
|
xc_gpio_init(&GPIO_InitCfg);
|
|
|
|
GPIO_InitCfg.Pin = GPIO_18;
|
|
xc_gpio_init(&GPIO_InitCfg);
|
|
|
|
GPIO_InitCfg.Pin = GPIO_19;
|
|
xc_gpio_init(&GPIO_InitCfg);
|
|
|
|
GPIO_InitCfg.Pin = GPIO_0;
|
|
xc_gpio_init(&GPIO_InitCfg);
|
|
|
|
xc_gpio_fun_sel(GPIO_3, UART0_TX); // 更换打印口
|
|
xc_gpio_fun_sel(GPIO_2, UART0_RX);
|
|
|
|
adc_cfg.Freq = ADC_FREQ_8M;
|
|
adc_cfg.RefVol = ADC_REF_VOL_3_3V;
|
|
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_4;
|
|
adc_cfg.ExtTriggerSel = ADC_EXT_TRIGGER_SEL_PWM0;
|
|
|
|
xc_adc_init(&adc_cfg);
|
|
|
|
timer1_cnt = 31;
|
|
timer0_cnt = 1;
|
|
|
|
xc_adc_wait_time_set(timer0_cnt, timer1_cnt);
|
|
|
|
(*(volatile uint32_t *)(0x40018000)) &= ~((uint32_t)0xf<<16);
|
|
(*(volatile uint32_t *)(0x40018000)) |= ((uint32_t)0x3<<16);
|
|
(*(volatile uint32_t *)(0x40018000)) |= ((uint32_t)0xf<<20);
|
|
(*(volatile uint32_t *)(0x40018028)) = 0x12340000;
|
|
(*(volatile uint32_t *)(0x4001802C)) = 0x00000000;
|
|
|
|
xc_adc_ch_auto_set(ADC_CHAN_CTL_CHAN_1_SET | ADC_CHAN_CTL_CHAN_2_SET |
|
|
ADC_CHAN_CTL_CHAN_3_SET | ADC_CHAN_CTL_CHAN_4_SET);
|
|
|
|
xc_adc_ch_auto_enable();
|
|
|
|
xc_adc_enable_it(ADC_INT_READ_REQ_INT_SET |ADC_INT_FIFO_ERROR_INT_SET );
|
|
|
|
NVIC_EnableIRQ(GADC_IRQn);
|
|
|
|
xc_adc_fifo_req_len_set(ADC_FIFO_CTL_READ_REQ_THRESH_LEN_8);
|
|
|
|
xc_adc_fifo_flush();
|
|
|
|
xc_adc_it_set(adc_int_get());
|
|
|
|
xc_adc_enable();
|
|
|
|
while (1) {
|
|
|
|
if (adc_con >= 500 && adc_con != 0xffff) {
|
|
fifo_val = (ADC_FIFO_TypeDef_t *)&val;
|
|
|
|
for (uint16_t i = 0; i < 500; i++) {
|
|
val = adc_val_buff[i];
|
|
|
|
integer = ((uint32_t)(fifo_val->value_1) * (3.3 * 100)) /
|
|
(1.0 * 4095) / 100;
|
|
decimal =
|
|
((uint32_t)((fifo_val->value_1 * (3.3 * 100)) / 4095) %
|
|
100);
|
|
DEBUG("i_dex1: %4d ch : %d val: %d adc_vol=%d.%02dV\n", i,
|
|
fifo_val->chanel_1, fifo_val->value_1, integer, decimal);
|
|
|
|
integer = ((uint32_t)(fifo_val->value_2) * (3.3 * 100)) /
|
|
(1.0 * 4095) / 100;
|
|
decimal =
|
|
((uint32_t)((fifo_val->value_2 * (3.3 * 100)) / 4095) %
|
|
100);
|
|
DEBUG("i_dex2: %4d ch : %d val: %d adc_vol=%d.%02dV\n", i,
|
|
fifo_val->chanel_2, fifo_val->value_2, integer, decimal);
|
|
|
|
if ((i % 2))
|
|
DEBUG("\r\n\r\n");
|
|
}
|
|
adc_con = 0xffff;
|
|
}
|
|
}
|
|
}
|
|
|
|
__RAM_CODE void ADC_channelswitch_hw_Intr_Callback(void)
|
|
{
|
|
uint32_t adc_int;
|
|
|
|
adc_int = xc_adc_it_get();
|
|
|
|
if (!(adc_int & (ADC_INT_FIFO_ERROR_INT_SET | ADC_INT_READ_REQ_INT_SET))) {
|
|
return;
|
|
}
|
|
|
|
if (adc_con >= 500) {
|
|
xc_adc_it_set(xc_adc_it_get());
|
|
return;
|
|
}
|
|
|
|
xc_adc_it_set(xc_adc_it_get());
|
|
|
|
for (int t = 0; t < ADC_FIFO_CTL_READ_REQ_THRESH_LEN_2; t++) {
|
|
adc_val_buff[adc_con] = xc_adc_data_get();
|
|
adc_con++;
|
|
}
|
|
}
|
|
|
|
__RAM_CODE void adc_intr_callback(void)
|
|
{
|
|
ADC_channelswitch_hw_Intr_Callback();
|
|
}
|