GD32用ADC扫描模式+DMA传输 查询方式等不到EOC标志置位

33 篇文章 2 订阅

【本文地址https://blog.csdn.net/Stack_/article/details/115947037,未经许可不得转载,转载须注明出处】


按兆易创新官方文档 GD32F30x_用户手册_Rev2.5.pdf 编写ADC扫描规则组并开启DMA传输的程序。

在软件触发转换之后,继续运行其它代码,隔一段时间查询一次EOC标志。查询到EOC标志位恒为0,但是DMA确已将数据搬运到定义的数组缓冲区中。



在这里插入图片描述



所以这种方式不可行,改为查询FTFIF位,程序正常运行。



在这里插入图片描述




猜想应该是DMA响应并转运ADC转换值寄存器数据,相当于程序读取了转换值寄存器。由于EOC标志位读后自动清零的特性,导致用查询方法查询不到其置位。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个使用DMA方式传输ADC值的示例代码,用于在STM32微控制器上使用RS485通信协议传输ADC值。 ```c #include "stm32f10x.h" #include "stdio.h" #define RS485_GPIO GPIOA #define RS485_USART USART1 #define RS485_TX_PIN GPIO_Pin_9 #define RS485_RX_PIN GPIO_Pin_10 #define RS485_TX_ENABLE_PIN GPIO_Pin_8 #define ADC_GPIO GPIOA #define ADC_PIN GPIO_Pin_0 #define ADC_CHANNEL ADC_Channel_0 ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; USART_InitTypeDef USART_InitStructure; u16 ADC_Value; u8 RS485_TxBuffer[2]; void RCC_Configuration(void) { /* ADC and GPIO Clock Enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART and DMA Clock Enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* RS485 GPIO Configuration */ GPIO_InitStructure.GPIO_Pin = RS485_TX_PIN | RS485_RX_PIN | RS485_TX_ENABLE_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(RS485_GPIO, &GPIO_InitStructure); /* ADC GPIO Configuration */ GPIO_InitStructure.GPIO_Pin = ADC_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(ADC_GPIO, &GPIO_InitStructure); } void ADC_Configuration(void) { /* ADC Configuration */ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* Enable ADC DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC */ ADC_Cmd(ADC1, ENABLE); /* ADC Calibration */ ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); } void DMA_Configuration(void) { /* DMA Configuration */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(USART1->DR)); DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RS485_TxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 2; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); } void USART_Configuration(void) { /* USART Configuration */ USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(RS485_USART, &USART_InitStructure); /* Enable USART */ USART_Cmd(RS485_USART, ENABLE); /* Enable USART Tx Enable Pin */ GPIO_WriteBit(RS485_GPIO, RS485_TX_ENABLE_PIN, Bit_SET); /* Enable USART DMA Tx Request */ USART_DMACmd(RS485_USART, USART_DMAReq_Tx, ENABLE); } void RS485_Send(u8 *data, u16 length) { /* Enable RS485 Tx Enable Pin */ GPIO_WriteBit(RS485_GPIO, RS485_TX_ENABLE_PIN, Bit_RESET); /* Configure DMA */ DMA_Configuration(); /* Load Data into DMA Buffer */ RS485_TxBuffer[0] = data[0]; RS485_TxBuffer[1] = data[1]; /* Enable DMA Channel */ DMA_Cmd(DMA1_Channel1, ENABLE); /* Wait for DMA Transmission to Complete */ while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET); /* Disable DMA Channel */ DMA_Cmd(DMA1_Channel1, DISABLE); /* Disable RS485 Tx Enable Pin */ GPIO_WriteBit(RS485_GPIO, RS485_TX_ENABLE_PIN, Bit_SET); } int main(void) { /* RCC Configuration */ RCC_Configuration(); /* GPIO Configuration */ GPIO_Configuration(); /* ADC Configuration */ ADC_Configuration(); /* USART Configuration */ USART_Configuration(); while(1) { /* Wait for ADC Conversion to Complete */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); /* Read ADC Value */ ADC_Value = ADC_GetConversionValue(ADC1); /* Convert ADC Value to Data */ RS485_TxBuffer[0] = ADC_Value >> 8; RS485_TxBuffer[1] = ADC_Value & 0xFF; /* Send Data */ RS485_Send(RS485_TxBuffer, 2); /* Delay */ for(u32 i = 0; i < 100000; i++); } } ``` 在上面的代码中,我们使用了STM32的USART、ADCDMA模块。使用DMA方式传输数据可以减少CPU的负载,从而提高系统性能。在主循环中,我们等待ADC转换完成并读取ADC值。然后,我们将ADC值转换为两个字节的数据,并使用RS485协议将数据发送出去。在发送数据之前,我们需要将RS485的Tx Enable引脚设置为低电平,以启用发送模式。发送完成后,我们将Tx Enable引脚设置为高电平,以禁用发送模式。 请注意,此示例代码仅用于演示目的。在实际应用中,您需要根据您的需求进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值