CH2O.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "CH2O.h"
  16. #include "iot_adc.h"
  17. #include "iot_errno.h"
  18. #include "iot_gpio.h"
  19. #include "iot_gpio_ex.h"
  20. #include "iot_pwm.h"
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #define CAL_PPM 20 //校准环境中PPM值
  26. #define RL 1 // RL阻值
  27. #define WIFI_IOT_IO_NAME_GPIO_8 8
  28. #define WIFI_IOT_PWM_PORT_PWM1 1
  29. #define WIFI_IOT_IO_FUNC_GPIO_8_PWM1_OUT 5
  30. #define SAFE_LED_GPIO 14
  31. #define WARN_LED_GPIO 7
  32. static float R0; //元件在洁净空气中的阻值
  33. /***************************************************************
  34. * 函数名称: MQ2Init
  35. * 说 明: 初始化MQ2Init
  36. * 参 数: 无
  37. * 返 回 值: 无
  38. ***************************************************************/
  39. void BoardInit(void)
  40. {
  41. IoTGpioInit(SAFE_LED_GPIO);
  42. IoTGpioSetFunc(SAFE_LED_GPIO, IOT_GPIO_FUNC_GPIO_14_GPIO);
  43. IoTGpioSetDir(SAFE_LED_GPIO, IOT_GPIO_DIR_OUT); //设置为输出模式
  44. IoTGpioInit(WARN_LED_GPIO);
  45. IoTGpioSetFunc(WARN_LED_GPIO, IOT_GPIO_FUNC_GPIO_7_GPIO);
  46. IoTGpioSetDir(WARN_LED_GPIO, IOT_GPIO_DIR_OUT); //设置为输出模式
  47. IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_8); //初始化GPIO_8
  48. IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_PWM1_OUT); //设置GPIO_8引脚复用功能为PWM
  49. IoTGpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, IOT_GPIO_DIR_OUT); //设置GPIO_8引脚为输出模式
  50. IoTPwmInit(WIFI_IOT_PWM_PORT_PWM1); //初始化PWM1端口
  51. }
  52. /***************************************************************
  53. * 函数名称: GetVoltage
  54. * 说 明: 获取电压值函数
  55. * 参 数: 无
  56. *
  57. * 返 回 值: 无
  58. ***************************************************************/
  59. static float GetVoltage(void)
  60. {
  61. unsigned int ret;
  62. unsigned short data;
  63. ret = IoTAdcRead(5, &data, IOT_ADC_EQU_MODEL_8, IOT_ADC_CUR_BAIS_DEFAULT, 0xff);
  64. if (ret != IOT_SUCCESS)
  65. {
  66. printf("ADC Read Fail\n");
  67. }
  68. return (float)data * 1.8 * 4 / 4096.0;
  69. }
  70. /***************************************************************
  71. * 函数名称: GetMQ2PPM
  72. * 说 明: 获取PPM函数
  73. * 参 数: ppm 烟雾浓度
  74. * 返 回 值: 0 成功; -1 失败
  75. ***************************************************************/
  76. int GetMQ2PPM(float* ppm)
  77. {
  78. unsigned int ret;
  79. unsigned short data;
  80. float voltage, RS;
  81. ret = IoTAdcRead(6, &data, IOT_ADC_EQU_MODEL_8, IOT_ADC_CUR_BAIS_DEFAULT, 0xff);
  82. if (ret != 0) {
  83. printf("ADC Read Fail\n");
  84. return -1;
  85. }
  86. voltage = (float)data * 1.8 * 4 / 4096.0;
  87. printf("voltage:%2.2fV\n",voltage);
  88. RS = (5 - voltage) / voltage * RL; //计算RS
  89. *ppm = 613.9f * pow(RS / R0, -2.074f); //计算ppm
  90. return 0;
  91. }
  92. /***************************************************************
  93. * 函数名称: MQ2PPMCalibration
  94. * 说 明: 传感器校准函数
  95. * 参 数: 无
  96. * 返 回 值: 无
  97. ***************************************************************/
  98. void MQ2PPMCalibration(void)
  99. {
  100. float voltage = GetVoltage();
  101. float RS = (5 - voltage) / voltage * RL;
  102. R0 = RS / pow(CAL_PPM / 613.9f, 1 / -2.074f);
  103. }
  104. /***************************************************************
  105. * 函数名称: BeepStatusSet
  106. * 说 明: 蜂鸣器报警与否
  107. * 参 数: status,ENUM枚举的数据
  108. * OFF,蜂鸣器
  109. * ON,开蜂鸣器
  110. * 返 回 值: 无
  111. ***************************************************************/
  112. void BeepStatusSet(SwitchStatus status)
  113. {
  114. if (status == ON) {
  115. IoTPwmStart(WIFI_IOT_PWM_PORT_PWM1, 50, 4000); //输出PWM波
  116. }
  117. if (status == OFF) {
  118. IoTPwmStop(WIFI_IOT_PWM_PORT_PWM1);
  119. }
  120. }
  121. /***************************************************************
  122. * 函数名称: LedSafeStatusSet
  123. * 说 明: LED_Safe状态设置
  124. * 参 数: status,ENUM枚举的数据
  125. * OFF,关
  126. * ON,开
  127. * 返 回 值: 无
  128. ***************************************************************/
  129. void LedSafeStatusSet(SwitchStatus status)
  130. {
  131. if (status == ON) {
  132. IoTGpioSetOutputVal(SAFE_LED_GPIO, 1); // 设置GPIO_7输出高电平点亮灯
  133. }
  134. if (status == OFF) {
  135. IoTGpioSetOutputVal(SAFE_LED_GPIO, 0); // 设置GPIO_7输出低电平关闭灯
  136. }
  137. }
  138. /***************************************************************
  139. * 函数名称: LedWarnStatusSet
  140. * 说 明: LED_Warn状态设置
  141. * 参 数: status,ENUM枚举的数据
  142. * OFF,关
  143. * ON,开
  144. * 返 回 值: 无
  145. ***************************************************************/
  146. void LedWarnStatusSet(SwitchStatus status)
  147. {
  148. if (status == ON) {
  149. IoTGpioSetOutputVal(WARN_LED_GPIO, 1); // 设置GPIO_14输出高电平点亮灯
  150. }
  151. if (status == OFF) {
  152. IoTGpioSetOutputVal(WARN_LED_GPIO, 0); // 设置GPIO_14输出低电平关闭灯
  153. }
  154. }