/***********************************************************************
 * Copyright (c)  2017 - 2022, Unicmicro Co.,Ltd .
 * All rights reserved.
 * Filename    : continue_mode_single_ch.c
 * Description : continue_mode_single_ch source file
 * Author(s)   : Hackett
 * version     : V1.0
 * Modify date : 2022-07-10
 ***********************************************************************/
#include  "continue_mode_single_ch.h"
#include  "common.h"
#include  "adc.h"
#include  "config.h"
#include  "app.h"

uint8_t g_continuous_complete_flag = 0;

uint16_t g_continuous_value_tab[10] ={0};
uint8_t g_data_index = 0;
extern uint8_t g_ch; 

/************************************************************************
 * Function   	: adc_continuous_single_ch_func
 * Description	: adc_continuous_single_ch_func 回调函数
 * Input 		: uint8_t ch_num ADC通道
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_continuous_single_ch_func(uint8_t ch_num) //单通道连续模式回调 以通道3为例
{
	
	g_ch = adc_channel_cal(ch_num);
	
	g_continuous_value_tab[g_data_index++] = adc_irq_get_data(g_ch);
	if(g_data_index >= 10)
	{
		adc_convert_stop();
		g_continuous_complete_flag =1;
		g_data_index = 0;
	}
	
}

/************************************************************************
 * Function   	: adc_continuous_mode_single_ch_test
 * Description	: adc_continuous_mode_single_ch_test adc单通道连续采样模式
 * Input 		: none
 * Output 		: none
 * Return		: none
 ************************************************************************/
void adc_continuous_mode_single_ch_test(void)
{
	
	uint8_t i = 0;
	
	adc_init(ADC_REF_VDDH,ADC_SAMPL_RATE_50K_SPS,ADC_CONTINUOUS_MODE);		//ADC初始化，选择ADC参考电压源、ADCCLK分频(2~65535)和采样模式

	adc_channel_gpio_config(ADC_CH3_EN ,ADC_PB0);		//配置ADC转换通道:通道3_PB0

	
#ifdef ADC_IRQ_EN										//在app.h开关
	printfS("adc continue mode --- single channel irq_start!!!\r\n");
	adc_irq_init(ADC_IRQ_ENABLE,ADC_IT_CH3_EN,adc_continuous_single_ch_func);	
	
	adc_convert_start();
	
	while(g_continuous_complete_flag == 0); //等待连续10次采样结束
	g_continuous_complete_flag = 0;

	while(i < 10)//打印出所有数据
	{
		printfS("g_continuous_value_tab[%d] = %d, adc_vol = %.3fV\r\n",\
		i,g_continuous_value_tab[i],(float)g_continuous_value_tab[i]/4095*(float)3.3);
		i++;
	}
			
#else
	printfS("adc continue mode --- single channel start!!!\r\n");
	adc_convert_start();
	for(i=0; i<10; i++)
	{
		g_continuous_value_tab[i] = adc_get_data(3);	
		printfS("g_continuous_value_tab[%d] = %d, adc_vol = %.3fV\r\n",\
		i,g_continuous_value_tab[i],(float)g_continuous_value_tab[i]/4095*(float)3.3);
	}
	adc_convert_stop();
	
	
#endif
}
