LEDPWM.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <stdio.h>
  2. #include "ohos_init.h"
  3. #include "iot_errno.h"
  4. #include "iot_gpio.h"
  5. #include "iot_gpio_ex.h"
  6. #include "iot_i2c.h"
  7. #include <math.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <stdint.h>
  11. #include "ohos_init.h"
  12. #include <stdlib.h>
  13. #include <cmsis_os2.h>
  14. #include <stdbool.h>
  15. #define WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA 6
  16. #define WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL 6
  17. #define WIFI_IOT_I2C_IDX_1 1
  18. #define BH1750_ADDR 0x23
  19. int BoardInit(void);
  20. int E53SC1ReadData(float *data);
  21. static void GpioInit(void)
  22. {
  23. printf("GPIO init...\r\n");
  24. IoTGpioInit(0);
  25. IoTGpioSetFunc(0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA); // GPIO_0复用为I2C1_SDA
  26. IoTGpioInit(1);
  27. IoTGpioSetFunc(1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL); // GPIO_1复用为I2C1_SCL
  28. IoTI2cInit(WIFI_IOT_I2C_IDX_1, 400000); /* baudrate: 400kbps */
  29. printf("GPIO init...OK\r\n");
  30. }
  31. static int InitBH1750(void)
  32. {
  33. int ret;
  34. uint8_t send_data[1] = {0x01};
  35. ret = IoTI2cWrite(WIFI_IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x00, send_data, 1);
  36. if (ret != 0) {
  37. printf("===== Error: I2C write ret = 0x%x! =====\r\n", ret);
  38. return -1;
  39. }
  40. return 0;
  41. }
  42. static int StartBH1750(void)
  43. {
  44. int ret;
  45. uint8_t send_data[1] = {0x10};
  46. ret = IoTI2cWrite(WIFI_IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x00, send_data, 1);
  47. if (ret != 0) {
  48. printf("===== Error: I2C write ret = 0x%x! =====\r\n", ret);
  49. return -1;
  50. }
  51. return 0;
  52. }
  53. int BoardInit(void)
  54. {
  55. int ret;
  56. GpioInit();
  57. ret = InitBH1750();
  58. // if (ret != 0) {
  59. // return -1;
  60. // }
  61. return 0;
  62. }
  63. int BH1750ReadData(float* data)
  64. {
  65. int ret;
  66. int result;
  67. ret = StartBH1750(); // 启动传感器采集数据
  68. if (ret != 0) {
  69. printf("Start BH1750 failed!\r\n");
  70. return -1;
  71. }
  72. usleep(180000);
  73. uint8_t recv_data[2] = {0};
  74. ret = IoTI2cRead(WIFI_IOT_I2C_IDX_1, (BH1750_ADDR << 1) | 0x01, recv_data, 2); // 读取传感器数据
  75. if (ret != 0) {
  76. return -1;
  77. }
  78. printf("recevie data is :%x,%x\r\n",recv_data[0],recv_data[1]);
  79. *data = (float)(((recv_data[0] << 8) + recv_data[1]) / 1.2); //合成数据,即光照数据
  80. return 0;
  81. }
  82. static void LEDPWM(void)
  83. {
  84. int ret;
  85. int temp;
  86. float Lux;
  87. ret = BoardInit();
  88. if (ret != 0) {
  89. printf("Board Init failed!\r\n");
  90. return;
  91. }
  92. osDelay(300);
  93. temp = BH1750ReadData(&Lux);
  94. if (temp != 0) {
  95. printf("Read Data failed!\r\n");
  96. return;
  97. }
  98. printf("Lux data:%.2f\r\n", Lux);
  99. }
  100. APP_FEATURE_INIT(LEDPWM);