首页 > 其他分享 >GD32F407 ADC+DMA

GD32F407 ADC+DMA

时间:2023-03-06 16:34:07浏览次数:48  
标签:DMA dma GD32F407 init ADC ADC0 config adc

#define VDDA_APPLI                     ((uint32_t) 3300)    /* Value of analog voltage supply Vdda (unit: mV) */
#define RANGE_12BITS                   ((uint8_t) 12)    //移位/* Max digital value with a full range of 12 bits */

#define COMPUTATION_DIGITAL_12BITS_TO_VOLTAGE(ADC_DATA)	 ( ((ADC_DATA) * VDDA_APPLI ) >> RANGE_12BITS)

uint16_t g_adc_dma_data[DRI_ADC_TIMES * DRI_ADC_NUMBER] = {0};

int dri_dma1_channel0_init(void)
{
	dma_single_data_parameter_struct dma_init;
	
	rcu_periph_clock_enable(RCU_DMA1);
	
	dma_deinit(DMA1, DMA_CH0);
	dma_init.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
	dma_init.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
	dma_init.memory0_addr = (uint32_t)g_adc_dma_data;
	dma_init.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
	dma_init.periph_memory_width = DMA_PERIPH_WIDTH_16BIT;	
	dma_init.priority = DMA_PRIORITY_HIGH;
	dma_init.circular_mode = DMA_CIRCULAR_MODE_ENABLE;
	dma_init.direction = DMA_PERIPH_TO_MEMORY;
	dma_init.number = DRI_ADC_TIMES * DRI_ADC_NUMBER;
	
	dma_single_data_mode_init(DMA1, DMA_CH0, &dma_init);
	
	/* enable DMA circulation mode */
    dma_circulation_enable(DMA1, DMA_CH0);
	
	return 0;
}

int dri_adc0_init(void)
{
	/* enable ADC clock */
	rcu_periph_clock_enable(RCU_ADC0);
	/* config ADC clock */
	adc_clock_config(ADC_ADCCK_PCLK2_DIV6);
	
	
	rcu_periph_clock_enable(RCU_GPIOA);
	/* config the GPIO as analog mode */
#if OPT_CHECK_EN > 0u
	gpio_mode_set(DRI_ADC_OPT_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, DRI_ADC_OPT_PIN);
#endif	
#if RF_CHECK_EN > 0u
	gpio_mode_set(DRI_ADC_RF_PORT, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, DRI_ADC_RF_PIN);
#endif
	
	/*reset all*/
	adc_deinit();
	/*!< ADC synchronization mode disabled.All the ADCs work independently */
	adc_sync_mode_config(ADC_SYNC_MODE_INDEPENDENT);
	/*连续模式*/
	adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
	/*扫描模式*/
	adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE); 
	/* ADC channel length(num) config */
	adc_channel_length_config(ADC0,ADC_ROUTINE_CHANNEL, 2);
	/* ADC data alignment config */
	adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
	/* ADC resolusion 12B */
	adc_resolution_config(ADC0,ADC_RESOLUTION_12B);
	/*禁用外部触发,只能使用软件触发*/
	adc_external_trigger_source_config(ADC0, ADC_ROUTINE_CHANNEL, ADC_EXTTRIG_ROUTINE_T0_CH0);
	adc_external_trigger_config(ADC0, ADC_ROUTINE_CHANNEL, ENABLE);
	/* ADC routine channel config */
#if OPT_CHECK_EN > 0u
		adc_routine_channel_config(ADC0, 0 , DRI_ADC_CHENNEL_OPTICAL, ADC_SAMPLETIME_15);
#endif
#if RF_CHECK_EN > 0u
		adc_routine_channel_config(ADC0, 1 , DRI_ADC_CHENNEL_RF, ADC_SAMPLETIME_15);
#endif

	adc_end_of_conversion_config(ADC0, ADC_EOC_SET_CONVERSION);

	adc_dma_request_after_last_enable(ADC0);
	adc_dma_mode_enable(ADC0);
	
	/* enable ADC interface */
	adc_enable(ADC0);
	dri_timer_delay_us(1000);
	/* wait for ADC stability */
	/* ADC calibration and reset calibration */
	adc_calibration_enable(ADC0);
	
	return 0;
}

void dri_adc0_dma1_init(void)
{	
	dri_adc0_init();
	dri_dma1_channel0_init();
	
	/*使能DMA通道*/
	dma_channel_enable(DMA1, DMA_CH0); 
	/*软件触发*/
	adc_software_trigger_enable(ADC0, ADC_ROUTINE_CHANNEL);
}

static uint32_t dri_adc_sample_filter(uint32_t ulADCChannel, uint16_t *pusSample)
{	
	uint16_t i = 0;	
	uint16_t strtcnt = 0;
	uint32_t filterValue = 0;
	
#if RF_CHECK_EN && OPT_CHECK_EN
    if(ulADCChannel == DRI_ADC_CHENNEL_OPTICAL)
		strtcnt = 0;
	else 
		strtcnt = 1;
#else
    strtcnt = 0;
#endif	
	
	for(i = strtcnt; i < DRI_ADC_TIMES * DRI_ADC_NUMBER; i += DRI_ADC_NUMBER)
	{
	    filterValue += pusSample[i];
	}
	ADC_DEBUG("filterValue %d\r\n", filterValue);
	filterValue /= DRI_ADC_TIMES;

	return filterValue;
}

uint16_t dri_adc_convert(uint32_t ulADCChannel, uint8_t ucMode)
{	
	uint32_t ulSampleAvg = 0;
    uint16_t fVoltage    = 0;
	
	ulSampleAvg = dri_adc_sample_filter(ulADCChannel, g_adc_dma_data);
	switch(ucMode)
	{
		case DRI_ADC_OUTPUT_VOLTAGE:
            fVoltage = COMPUTATION_DIGITAL_12BITS_TO_VOLTAGE(ulSampleAvg);
            break;
        default:
            fVoltage = (uint16_t)ulSampleAvg;
            break;
	}

    return fVoltage;
}

 

标签:DMA,dma,GD32F407,init,ADC,ADC0,config,adc
From: https://www.cnblogs.com/blog-xh/p/17184366.html

相关文章

  • Flutter Developer Roadmap All In One
    FlutterDeveloperRoadmapAllInOneFlutter&DartLearningtobecomeamodernFlutterdeveloperin2023https://roadmap.sh/flutter(......
  • 写完mapper报错Result Maps collection does not contain value for com.itheima.mapp
        改了这两个地方就报错,后来发现是resources目录层级建错了。记得resources里的目录应该用/来分隔开而不是.......
  • ContentProvider+BroadCast的使用
    内容提供者的官方定义如下:  首先是ContentProvider的使用,翻译成中文就是内容提供者,作用就是比如我在从微信往qq传一张图片,我需要一个内容提供者,这里我的微信里面的图......
  • 《DFZU2EG_4EV MPSoC之嵌入式Vitis开发指南》第二十一章 AXI DMA环路测试​
    AXIDMA环路测试​DMA(DirectMemoryAccess,直接存储器访问)是计算机科学中的一种内存访问技术。它允许某些计算机内部的硬件子系统可以独立地直接读写系统内存,而不需中央处......
  • Appstore和Androidmarket开发者账号申请的那点事
    作为一个开发者,不可避免的问题就是申请store的账户了,相信很多个人开发者一开始的时候都很迷茫,今天给大家收集整理下appstore和androidmarket的开发者申请流程,希望对大家有所......
  • 串口DMA接收到数据后,DMA停止对外发送数据
    hal库中原始函数,会把收发DMA一块儿关闭,这样会造成在DMA发送的同时,有DMA接收到数据调用以上函数时,会停止DMA发送;1/**2*@briefStoptheDMATransfer.3*......
  • 韦东山2440-学习笔记-ADC触摸屏
    1.程序员需要关注的硬件特性1.1ADC的特性输出数据用多少位描述10bit模拟输入最大值0-3.3V采样/转换速度每秒转换500*1000次1.2看本SoCAD......
  • 8.4-DMA方式
    DMA方式的基本原理数据的传送不经过CPU,由DMA控制器实现内存和外设,外设和外设之间的直接快速传递用于需要高速大批量数据传送系统中 DMA传输计算机系统构成DMA控制......
  • 3DMAX安装失败怎么办?安装3DMAX失败提示错误怎么解决?
    3DMAX安装失败怎么办?安装3DMAX失败提示错误怎么解决?有很多同学想把3DMAX卸载后重新安装,但是发现3DMAX安装到一半就失败了或者显示3DMAX已安装或者安装未完成,大多数情况下......
  • EventBroadcaster
    Kubernetes的事件(Event)是一种资源对象,用于展示集群内发生的情况,Kubernetes系统中的各个组件会将运行时发生的各种事件上报给KubernetesAPIServer。例如,调度器做了什么决......