收藏官网首页
查看: 8600|回复: 1

MQ-135空气质量传感器驱动

321

主题

1054

帖子

4513

积分

论坛元老

Rank: 8Rank: 8

积分
4513
QQ
跳转到指定楼层
楼主
发表于 2017-5-13 20:33:03 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
教您5分钟接入机智云,实现傻瓜式开发
本帖最后由 bigfanofloT 于 2017-5-16 19:42 编辑

一、MQ-135空气质量传感器介绍
参考此帖http://club.gizwits.com/thread-6484-1-1.html
二、硬件连线
根据Nucleo-F767ZI原理图,我们选择arduino UNO接口的A1,即STM32F767ZIT6的PC0,ADC1通道10

三、完整源码
头文件如下:
  1. /**
  2.   ***********************************
  3.   * @file    ADC/ADC_RegularConversion_Polling/Inc/main.h
  4.   * @author  MCD Application Team
  5.   * @version V1.0.2
  6.   * @date    30-December-2016
  7.   * @brief   Header for main.c module
  8.   ***********************************
  9.   * @attention
  10.   *
  11.   * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12.   *
  13.   * Redistribution and use in source and binary forms, with or without modification,
  14.   * are permitted provided that the following conditions are met:
  15.   *   1. Redistributions of source code must retain the above copyright notice,
  16.   *      this list of conditions and the following disclaimer.
  17.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  18.   *      this list of conditions and the following disclaimer in the documentation
  19.   *      and/or other materials provided with the distribution.
  20.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  21.   *      may be used to endorse or promote products derived from this software
  22.   *      without specific prior written permission.
  23.   *
  24.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.   *
  35.   ***********************************
  36.   */

  37. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __MAIN_H
  39. #define __MAIN_H

  40. /* Includes ------------------------------------------------------------------*/
  41. #include "stm32f7xx_hal.h"
  42. #include "STM32F7xx_hal_adc.h"

  43. /* Exported types ------------------------------------------------------------*/
  44. /* Exported constants --------------------------------------------------------*/
  45. /* User can use this section to tailor ADCx instance used and associated
  46.    resources */
  47.    
  48. /* Definition for ADCx clock resources */
  49. #define ADCx                            ADC1
  50. #define ADCx_CLK_ENABLE()               __HAL_RCC_ADC1_CLK_ENABLE()
  51. #define ADCx_CHANNEL_GPIO_CLK_ENABLE()  __HAL_RCC_GPIOC_CLK_ENABLE()

  52. #define ADCx_FORCE_RESET()              __HAL_RCC_ADC_FORCE_RESET()
  53. #define ADCx_RELEASE_RESET()            __HAL_RCC_ADC_RELEASE_RESET()

  54. /* Definition for ADCx Channel Pin */
  55. #define ADCx_CHANNEL_PIN                GPIO_PIN_0//对应板卡A1
  56. #define ADCx_CHANNEL_GPIO_PORT          GPIOC

  57. /* Definition for ADCx's Channel */
  58. #define ADCx_CHANNEL                    ADC_CHANNEL_10
  59. #define SAMPLINGTIME                    ADC_SAMPLETIME_3CYCLES

  60. /* Exported macro ------------------------------------------------------------*/

  61. /* Exported functions ------------------------------------------------------- */
  62. void MQ135_Init(void);
  63. int MQ135_GetValue(void);

  64. #endif /* __MAIN_H */

  65. /*********** (C) COPYRIGHT STMicroelectronics ***END OF FILE**/
复制代码
源文件如下:
  1. /**
  2.   ***********************************
  3.   * @file    ADC/ADC_RegularConversion_Polling/Src/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.2
  6.   * @date    30-December-2016
  7.   * @brief   This example describes how to use Polling mode to convert data.
  8.   ***********************************
  9.   * @attention
  10.   *
  11.   * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12.   *
  13.   * Redistribution and use in source and binary forms, with or without modification,
  14.   * are permitted provided that the following conditions are met:
  15.   *   1. Redistributions of source code must retain the above copyright notice,
  16.   *      this list of conditions and the following disclaimer.
  17.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  18.   *      this list of conditions and the following disclaimer in the documentation
  19.   *      and/or other materials provided with the distribution.
  20.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  21.   *      may be used to endorse or promote products derived from this software
  22.   *      without specific prior written permission.
  23.   *
  24.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.   *
  35.   ***********************************
  36.   */

  37. /* Includes ------------------------------------------------------------------*/
  38. #include "hal_mq135.h"

  39. /** @addtogroup STM32F7xx_HAL_Examples
  40.   * @{
  41.   */

  42. /** @addtogroup ADC_RegularConversion_Polling
  43.   * @{
  44.   */

  45. /* Private typedef -----------------------------------------------------------*/
  46. /* Private define ------------------------------------------------------------*/
  47. /* Private macro -------------------------------------------------------------*/
  48. /* Private variables ---------------------------------------------------------*/
  49. /* ADC handler declaration */
  50. ADC_HandleTypeDef    AdcHandle;



  51. void MQ135_Init(void)
  52. {
  53.   ADC_ChannelConfTypeDef sConfig;
  54.         
  55.   GPIO_InitTypeDef          GPIO_InitStruct;
  56.   /*##-1- Enable peripherals and GPIO Clocks #################################*/
  57.   /* ADC Periph clock enable */
  58.   ADCx_CLK_ENABLE();
  59.   /* Enable GPIO clock ******************/
  60.   ADCx_CHANNEL_GPIO_CLK_ENABLE();
  61.   /*##-2- Configure peripheral GPIO ##########################################*/
  62.   /* ADC Channel GPIO pin configuration */
  63.   GPIO_InitStruct.Pin = ADCx_CHANNEL_PIN;
  64.   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  65.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  66.   HAL_GPIO_Init(ADCx_CHANNEL_GPIO_PORT, &GPIO_InitStruct);
  67.         

  68.   /*##-1- Configure the ADC peripheral #######################################*/
  69.   AdcHandle.Instance          = ADCx;
  70.   
  71.   ADCx_CLK_ENABLE() ;
  72.         ADCx_CHANNEL_GPIO_CLK_ENABLE();
  73.   AdcHandle.Init.ClockPrescaler        = ADC_CLOCKPRESCALER_PCLK_DIV4;
  74.   AdcHandle.Init.Resolution            = ADC_RESOLUTION_12B;
  75.   AdcHandle.Init.ScanConvMode          = DISABLE;                       /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
  76.   AdcHandle.Init.ContinuousConvMode    = DISABLE;                       /* Continuous mode disabled to have only 1 conversion at each conversion trig */
  77.   AdcHandle.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */
  78.   AdcHandle.Init.NbrOfDiscConversion   = 0;
  79.   AdcHandle.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE;        /* Conversion start trigged at each external event */
  80.   AdcHandle.Init.ExternalTrigConv      = ADC_EXTERNALTRIGCONV_T1_CC1;
  81.   AdcHandle.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
  82.   AdcHandle.Init.NbrOfConversion       = 1;
  83.   AdcHandle.Init.DMAContinuousRequests = DISABLE;
  84.   AdcHandle.Init.EOCSelection          = DISABLE;

  85.   if (HAL_ADC_Init(&AdcHandle) != HAL_OK)
  86.   {
  87.     /* ADC initialization Error */

  88.   }

  89.   /*##-2- Configure ADC regular channel ######################################*/

  90.   sConfig.Channel      = ADCx_CHANNEL;                /* Sampled channel number */
  91.   sConfig.Rank         = 1;          /* Rank of sampled channel number ADCx_CHANNEL */
  92.   sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;    /* Sampling time (number of clock cycles unit) */
  93.   sConfig.Offset = 0;                                 /* Parameter discarded because offset correction is disabled */

  94.   if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
  95.   {
  96.     /* Channel Configuration Error */

  97.   }

  98.   if (HAL_ADC_Start(&AdcHandle) != HAL_OK)
  99.   {
  100.     /* Start Conversation Error */

  101.   }

  102.   
  103. }
  104. //just return adc raw value.
  105. int MQ135_GetValue(void)
  106. {
  107.         if (HAL_ADC_Start(&AdcHandle) != HAL_OK)
  108.   {

  109.   }
  110.     return HAL_ADC_GetValue(&AdcHandle);
  111. }
  112.         


  113. /**
  114.   * @}
  115.   */

  116. /**
  117.   * @}
  118.   */

  119. /*********** (C) COPYRIGHT STMicroelectronics ***END OF FILE**/
复制代码

本系列帖子目录:

http://club.gizwits.com/thread-6544-1-1.html

1、机智云QQ群:G1群:104975951 G2群:491509598 G3群:287087942
机智云爱好者-APP开发群: 599735135
QQ群目前非常活跃,欢迎大家参与进来,交流,讨论,答疑,解惑~~
2、机智云微信公众号: 机智云 gizwits、 机智云智能宠物屋go-iot
关注机智云Gizwits官方公众号随时掌握最新资讯和活动信息

4

主题

12

帖子

447

积分

中级会员

Rank: 3Rank: 3

积分
447
沙发
发表于 2020-5-5 15:12:20 | 只看该作者
请问一下这个芯片如果是STM32F103C8的,驱动里面的串口是不是直接对应改一改还有hal库换一下就能直接用?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表