PM2_5.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "PM2_5.h"
  2. #include "iot_errno.h"
  3. #include "iot_gpio.h"
  4. #include "iot_gpio_ex.h"
  5. #include "iot_uart.h"
  6. #include "stdio.h"
  7. #define RESET_GPIO 7
  8. #define LED_SAFE_GPIO 8
  9. #define LED_WARN_GPIO 14
  10. #define SET_GPIO 13
  11. #define WIFI_IOT_UART_IDX_1 1
  12. uint8_t RxBuffer[3];
  13. void BoardInit(void)
  14. {
  15. IoTGpioInit(RESET_GPIO);
  16. IoTGpioSetFunc(RESET_GPIO, IOT_GPIO_FUNC_GPIO_7_GPIO);
  17. IoTGpioSetDir(RESET_GPIO, IOT_GPIO_DIR_OUT); // 设置为输出模式
  18. IoTGpioSetOutputVal(RESET_GPIO, 1);
  19. IoTGpioInit(LED_SAFE_GPIO);
  20. IoTGpioSetFunc(LED_SAFE_GPIO, IOT_GPIO_FUNC_GPIO_8_GPIO);
  21. IoTGpioSetDir(LED_SAFE_GPIO, IOT_GPIO_DIR_OUT); // 设置为输出模式
  22. IoTGpioInit(LED_WARN_GPIO);
  23. IoTGpioSetFunc(LED_WARN_GPIO, IOT_GPIO_FUNC_GPIO_14_GPIO);
  24. IoTGpioSetDir(LED_WARN_GPIO, IOT_GPIO_DIR_OUT); // 设置为输出模式
  25. IoTGpioInit(SET_GPIO);
  26. IoTGpioSetFunc(SET_GPIO, IOT_GPIO_FUNC_GPIO_13_GPIO);
  27. IoTGpioSetDir(SET_GPIO, IOT_GPIO_DIR_OUT); // 设置为输入模式
  28. IoTGpioSetOutputVal(SET_GPIO, 1);
  29. uint32_t ret;
  30. IotUartAttribute uart_attr = {
  31. // baud_rate: 9600
  32. .baudRate = 9600,
  33. // data_bits: 8bits
  34. .dataBits = 8,
  35. .stopBits = 1,
  36. .parity = 0,
  37. };
  38. // Initialize uart driver
  39. ret = IoTUartInit(WIFI_IOT_UART_IDX_1, &uart_attr);
  40. if (ret != IOT_SUCCESS) {
  41. printf("Failed to init uart! Err code = %d\n", ret);
  42. return;
  43. }
  44. }
  45. static unsigned char read_date[32];
  46. /***************************************************************
  47. * 函数名称: PM2DReadData
  48. * 说 明: 获取GPS经纬度信息
  49. * 参 数: 无
  50. * 返 回 值: 无
  51. ***************************************************************/
  52. void PM2DReadData(PM2D5Data* ReadData)
  53. {
  54. //通过串口1接收数据
  55. IoTUartRead(WIFI_IOT_UART_IDX_1, read_date, 32);
  56. // for(int i = 0; i<32; i++)
  57. // {
  58. // printf("%d------%x\r\n",i,read_date[i]);
  59. // }
  60. if(read_date[0]== 0x42 && read_date[1]== 0x4d)
  61. {
  62. ReadData->pm1_0=read_date[2]<<8|read_date[3];
  63. ReadData->pm2_5=read_date[4]<<8|read_date[5];
  64. ReadData->pm10=read_date[6]<<8|read_date[7];
  65. }
  66. }
  67. /***************************************************************
  68. * 函数名称: LedWarnStatusSet
  69. * 说 明: LED_Warn状态设置
  70. * 参 数: status,ENUM枚举的数据
  71. * OFF,关
  72. * ON,开
  73. * 返 回 值: 无
  74. ***************************************************************/
  75. void LedWarnStatusSet(SwitchStatus status)
  76. {
  77. if (status == ON) {
  78. IoTGpioSetOutputVal(LED_WARN_GPIO, 1); // 设置输出高电平点亮灯
  79. }
  80. if (status == OFF) {
  81. IoTGpioSetOutputVal(LED_WARN_GPIO, 0); // 设置输出低电平关闭灯
  82. }
  83. }
  84. /***************************************************************
  85. * 函数名称: LedSafeStatusSet
  86. * 说 明: LED_Safe状态设置
  87. * 参 数: status,ENUM枚举的数据
  88. * OFF,关
  89. * ON,开
  90. * 返 回 值: 无
  91. ***************************************************************/
  92. void LedSafeStatusSet(SwitchStatus status)
  93. {
  94. if (status == ON) {
  95. IoTGpioSetOutputVal(LED_SAFE_GPIO, 1); // 设置输出高电平点亮灯
  96. }
  97. if (status == OFF) {
  98. IoTGpioSetOutputVal(LED_SAFE_GPIO, 0); // 设置输出低电平关闭灯
  99. }
  100. }