• stm32 读取多路SDADC


    方法一:用SDADC的规则通道一个一个去读,配置SDADC如下

     读入代码比较简单,依次读取SDADC1通道6、通道8和SDADC3通道0,代码如下

    /* USER CODE BEGIN Header_StartDefaultTask */
    volatile uint32_t SDADC_Value[10];
    /**
      * @brief  Function implementing the defaultTask thread.
      * @param  argument: Not used
      * @retval None
      */
    /* USER CODE END Header_StartDefaultTask */
    void StartDefaultTask(void const * argument)
    {
      /* USER CODE BEGIN StartDefaultTask */
      /* Infinite loop */
      for(;;)
      {
          //read SDADC1 Channel6
        HAL_SDADC_ConfigChannel(&hsdadc1,SDADC_CHANNEL_6,SDADC_CONTINUOUS_CONV_OFF);// set channel
        HAL_SDADC_Start(&hsdadc1);//start sdadc
        if(HAL_SDADC_PollForConversion(&hsdadc1,HAL_MAX_DELAY)==HAL_OK)//waiting for convert complete
        {
            SDADC_Value[0] = HAL_SDADC_GetValue(&hsdadc1);// read the value of sdadc
            HAL_SDADC_Stop(&hsdadc1);
        }
    
          //read SDADC1 Channel8
        HAL_SDADC_ConfigChannel(&hsdadc1,SDADC_CHANNEL_8,SDADC_CONTINUOUS_CONV_OFF);
        HAL_SDADC_Start(&hsdadc1);
        if(HAL_SDADC_PollForConversion(&hsdadc1,HAL_MAX_DELAY)==HAL_OK)
        {
            SDADC_Value[1] = HAL_SDADC_GetValue(&hsdadc1);
            HAL_SDADC_Stop(&hsdadc1);
        }
    
          //read SDADC3 Channel0
        HAL_SDADC_ConfigChannel(&hsdadc3,SDADC_CHANNEL_0,SDADC_CONTINUOUS_CONV_OFF);
        HAL_SDADC_Start(&hsdadc3);
        if(HAL_SDADC_PollForConversion(&hsdadc3,HAL_MAX_DELAY)==HAL_OK)
        {
            SDADC_Value[2] = HAL_SDADC_GetValue(&hsdadc3);
            HAL_SDADC_Stop(&hsdadc3);
        }
        osDelay(1);
      }
      /* USER CODE END StartDefaultTask */
    }
    读取规则SDADC

    方法二:用注入通道一次读取多个转换值

    配置SDADC

     配置DMA

     在stm32f3xx_it.c文件中修改

    /**
      * @brief This function handles DMA2 channel3 global interrupt.
      */
    void DMA2_Channel3_IRQHandler(void)
    {
      /* USER CODE BEGIN DMA2_Channel3_IRQn 0 */
    
      /* USER CODE END DMA2_Channel3_IRQn 0 */
      HAL_DMA_IRQHandler(&hdma_sdadc1);
      /* USER CODE BEGIN DMA2_Channel3_IRQn 1 */
      IsFinishDMA2_Channel3 = 1;//DMA 转换完毕
      /* USER CODE END DMA2_Channel3_IRQn 1 */
    }
    
    /**
      * @brief This function handles DMA2 channel5 global interrupt.
      */
    void DMA2_Channel5_IRQHandler(void)
    {
      /* USER CODE BEGIN DMA2_Channel5_IRQn 0 */
    
      /* USER CODE END DMA2_Channel5_IRQn 0 */
      HAL_DMA_IRQHandler(&hdma_sdadc3);
      /* USER CODE BEGIN DMA2_Channel5_IRQn 1 */
      IsFinishDMA2_Channel5 = 1;//DMA 转换完毕
      /* USER CODE END DMA2_Channel5_IRQn 1 */
    }
    stm32f3xx_it.c
    /* USER CODE BEGIN Header_StartDefaultTask */
    static uint32_t     SDADC1_DegitalValue[5];
    static uint32_t     SDADC3_DegitalValue[5];
    /**
      * @brief  Function implementing the defaultTask thread.
      * @param  argument: Not used
      * @retval None
      */
    /* USER CODE END Header_StartDefaultTask */
    void StartDefaultTask(void const * argument)
    {
      /* USER CODE BEGIN StartDefaultTask */
      /* Infinite loop */
      for(;;)
      {
        if(HAL_SDADC_InjectedStart_DMA(&hsdadc3,SDADC3_DegitalValue,5) == HAL_OK)
        {
            while(IsFinishDMA2_Channel5 == 0)
                osDelay(1);//等待DMA转换完成
            IsFinishDMA2_Channel5 = 0;
            HAL_SDADC_InjectedStop_DMA(&hsdadc3);
        }
    
        if(HAL_SDADC_InjectedStart_DMA(&hsdadc1,SDADC1_DegitalValue,5) == HAL_OK)
        {
            while(IsFinishDMA2_Channel3 == 0)
                osDelay(1);//等待DMA转换完成
            IsFinishDMA2_Channel3 = 0;
            HAL_SDADC_InjectedStop_DMA(&hsdadc1);
        }
        osDelay(1);
      }
      /* USER CODE END StartDefaultTask */
    }
    启动SDADC_DMA转换并读取

    转换结果如下:

    源码下载地址:https://github.com/lizhiqiang0204/STM32_SDADC 本例程是用STM32CubeIDE软件编写的

  • 相关阅读:
    client-go和golang源码中的技巧
    使用Prometheus监控snmp
    kubernetes client-go解析
    go 语言的一个赋值操作
    使用Prometheus监控bind9的DNS服务
    验证Prometheus alertmanager邮件发送
    JS 监听浏览器各个标签间的切换
    通过案例理解position:relative和position:absolute
    浏览器的统一指针事件:Pointer Event
    Chrome浏览器读写系统剪切板
  • 原文地址:https://www.cnblogs.com/lizhiqiang0204/p/13858267.html
Copyright © 2020-2023  润新知