本帖最后由 ccmm520 于 2018-3-16 16:00 编辑
主要的代码如下:adc.c 代码:
- ADC_HandleTypeDef hadc1;
- DMA_HandleTypeDef hdma_adc1;
- void MX_ADC1_Init(void)
- {
- ADC_ChannelConfTypeDef sConfig;
- /**Configure the global features of the ADC (Clock, Resolution, Data
- Alignment and number of conversion) */
- hadc1.Instance = ADC1;
- hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
- hadc1.Init.Resolution = ADC_RESOLUTION_12B;
- hadc1.Init.ScanConvMode = ENABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConvEdge = ADC_SOFTWARE_START;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.NbrOfConversion = 2;
- hadc1.Init.DMAContinuousRequests = ENABLE;
- hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
- HAL_ADC_Init(&hadc1);
- /**Configure for the selected ADC regular channel its corresponding
- rank in the sequencer and its sample time. */
- sConfig.Channel = ADC_CHANNEL_5;
- sConfig.Rank = 1;
- sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;
- HAL_ADC_ConfigChannel(&hadc1, &sConfig);
- /**Configure for the selected ADC regular channel its corresponding
- rank in the sequencer and its sample time. */
- sConfig.Channel = ADC_CHANNEL_3;
- sConfig.Rank = 2;
- HAL_ADC_ConfigChannel(&hadc1, &sConfig);
- }
- void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if(hadc->Instance==ADC1)
- {
- /* Peripheral clock enable */
- __HAL_RCC_ADC_CLK_ENABLE();
-
- /**ADC1 GPIO Configuration
- PA0-WKUP ------> ADC1_IN5
- PC2 ------> ADC1_IN3 */
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_2;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /* Peripheral DMA init*/
- hdma_adc1.Instance = DMA1_Channel1;
- hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
- hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
- hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
- hdma_adc1.Init.Mode = DMA_CIRCULAR;
- hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM;
- HAL_DMA_Init(&hdma_adc1);
- __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);
- }
- }
- void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
- {
- if(hadc->Instance==ADC1)
- {
- /* Peripheral clock disable */
- __HAL_RCC_ADC_CLK_DISABLE();
-
- /**ADC1 GPIO Configuration
- PA0-WKUP ------> ADC1_IN5
- PC2 ------> ADC1_IN3 */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
- HAL_GPIO_DeInit(GPIOC, GPIO_PIN_2);
- /* Peripheral DMA DeInit*/
- HAL_DMA_DeInit(hadc->DMA_Handle);
- }
- }
复制代码 main.c 代码:
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc);
- /* USER CODE BEGIN 0 */
- uint32_t uhADCxConvertedValue[2];
- unsigned long shijian=0;
- volatile uint8_t dmaflage=0;
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint16_t ad1=0, ad2=0;
- /* USER CODE END 1 */
- /* MCU
- Configuration----------------------------------------------------------
- */
- /* Reset of all peripherals, Initializes the Flash interface and the
- Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_ADC1_Init();
- /* USER CODE BEGIN 2 */
- #if DEBUG_ENABLE
- debugInit(115200);
- #endif
- shijian=HAL_GetTick();
- HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&uhADCxConvertedValue,2);
- printf("HAL_ADC_Start_DMA\r\n");
- while (1)
- {
- if(HAL_GetTick()-shijian>=500)
- {
- if(dmaflage==1)
- {
- dmaflage=0;
- ad1 = uhADCxConvertedValue[0];
- ad2 = uhADCxConvertedValue[1];
- printf("ad1=%d, ad2=%d, Vad1=%0.2fV, Vad2=%0.2fV \r\n",ad1,ad2,
- ad1*3.3/4096,ad2*3.3/4096);
- ad1 = (ad1*330)/4096; //扩大100倍 ,3.3V, 4096=12bit
- ad2 = (ad2*330)/4096;
- printf("V100ad1=%d, V100ad2=%d\r\n",ad1,ad2);
- HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&uhADCxConvertedValue,2);
- ad1 = 0;
- ad2 = 0;
- }
- shijian=HAL_GetTick();
- }
- }
- }
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
- {
- dmaflage=1;
- HAL_ADC_Stop_DMA(&hadc1);
- }
复制代码 dma.c 代码:
- void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- // __HAL_RCC_DMA2_CLK_ENABLE();
- __HAL_RCC_DMA1_CLK_ENABLE();
-
- /* NVIC configuration for DMA Input data interrupt */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 5, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- }
复制代码 stm32l4xx_it.c 代码
- extern DMA_HandleTypeDef hdma_adc1;
- /**
- * @brief This function handles DMA1 channel1 global interrupt.
- */
- void DMA1_Channel1_IRQHandler(void)
- {
- HAL_DMA_IRQHandler(&hdma_adc1);
- }
复制代码 大致的代码就是上面,采用ADC1,PA0通道5,PC2通道3;PA0口直接接3.3V,PC2口直接接地。
问题:
使用上面的代码,转出来的两个通道的电压值都是0.4~0.45V,理论上应该分别采集到的是3.3V和0V的,不知道为什么?是不是我哪里配置不对啊,求大神帮忙~
|
我在stm32l4xx_it.c中也进行了
我也是刚学没多久,而且是参考别人的例子来写的这个callback,请问具体我要怎么操作呢
file:///C:/Users/Administrator/Desktop/QQ%E5%9B%BE%E7%89%8720180315215136.png
采用ADC1,PA0通道5,PC2通道3;PA0口直接接3.3V,PC2口直接接地。MCU是stm32l476RC
转出来的电压值都是0.4~0.45V,理论上应该分别采集到的是3.3V和0V的,不知道为什么?
转换的值不用在中断读取吧,因为DMA的转换返回值是在HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&uhADCxConvertedValue,2);中的uhADCxConvertedValue就转好了,而且是循环连续转换的,我们只要在while(1)中不断获取转换即可吧。
另外,你附件的demo好像我没看出我的错在哪
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
如何读到正确的数据?
评分
查看全部评分
DMA中断调用应该是:
void DMA1_Channel1_IRQHandler(void)
{}
但是你在DMA中断中并没有读取数据。
ST官方例程很多,可以参考:
\Repository\STM32Cube_FW_L4_V1.10.0\Projects\STM32L476G_EVAL\Examples\ADC\ADC_DMA_Transfer
然后__weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)这个是个虚函数,你不能在main在次声明,直接实现就好了.只是规范一下,你能进去代表不影响.
我建议你一个个channel单独试一下,但后看看你硬件的参考电压是多少等等..
评分
查看全部评分
可是你在ADC中断里关闭了DMA
HAL_ADC_Stop_DMA(&hadc1);
我在while(1)里面又开启HAL_ADC_Start_DMA了,应该可以的吧?
DMA重新启动好像还要初始化ADC和DMA
重新初始化与否都可以,刚试了,目前这个问题很纠结。参考例程也看了,还是不知道哪里出了问题
其实你需要处理的数据量不多,建议不使用DMA,直接ADC中断取数据应该能成功
不是了,现在只是简单的转换,后面项目要大数据量转换的
先去掉DMA,调试到ADC正常后再增加DMA,总是比较容易找到问题。