1
0

A02_MQ5.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2020  Jinan Bosai Network 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 <stdio.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <math.h>
  19. #include "ohos_init.h"
  20. #include "cmsis_os2.h"
  21. #include "MQ5.h"
  22. #define TASK_STACK_SIZE 1024*8
  23. #define TASK_PRIO 25
  24. static void ExampleTask(void)
  25. {
  26. int ret;
  27. float ppm;
  28. BoardInit();
  29. //Sensor calibration
  30. usleep(1000000);
  31. MQ5PPMCalibration();
  32. while (1) {
  33. printf("=======================================\r\n");
  34. printf("*************A02_MQ5_example***********\r\n");
  35. printf("=======================================\r\n");
  36. //get MQ5 ppm
  37. ret = GetMQ5PPM(&ppm);
  38. if (ret != 0) {
  39. printf("ADC Read Fail\n");
  40. return ;
  41. }
  42. printf("ppm:%.3f \n", ppm);
  43. if (ppm > 30) {
  44. BeepStatusSet(ON);
  45. LedStatusSet(ON);
  46. } else {
  47. BeepStatusSet(OFF);
  48. LedStatusSet(OFF);
  49. }
  50. usleep(1000000);
  51. }
  52. }
  53. /**
  54. * @brief Main Entry of the A02_MQ5 Example
  55. *
  56. */
  57. static void ExampleEntry(void)
  58. {
  59. osThreadAttr_t attr;
  60. attr.name = "ExampleTask";
  61. attr.attr_bits = 0U;
  62. attr.cb_mem = NULL;
  63. attr.cb_size = 0U;
  64. attr.stack_mem = NULL;
  65. attr.stack_size = TASK_STACK_SIZE;
  66. attr.priority = TASK_PRIO;
  67. if (osThreadNew((osThreadFunc_t)ExampleTask, NULL, &attr) == NULL) {
  68. printf("Failed to create ExampleTask!\n");
  69. }
  70. }
  71. APP_FEATURE_INIT(ExampleEntry);