46 lines
1.6 KiB
C

#include "gpio.h"
#include "driver/i2c.h"
void get_temp(float *temp){
uint8_t data[] = {0};
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (0x40 << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd,0x00,true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100);
i2c_cmd_link_delete(cmd);
vTaskDelay(7);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (0x40 << 1) | I2C_MASTER_READ, true);
i2c_master_read_byte(cmd, data,0);
i2c_master_read_byte(cmd, data+1,1);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100);
i2c_cmd_link_delete(cmd);
uint16_t temp_y = (uint16_t)((data[0] << 8 ) | data[1]);
*temp = (temp_y / 65536.0f) * 165 - 40;
}
void get_humi(float *humi){
uint8_t data[] = {0};
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (0x40 << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd,0x01,true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100);
i2c_cmd_link_delete(cmd);
vTaskDelay(7);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (0x40 << 1) | I2C_MASTER_READ, true);
i2c_master_read_byte(cmd, data,0);
i2c_master_read_byte(cmd, data+1,1);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100);
i2c_cmd_link_delete(cmd);
uint16_t humi_y = (uint16_t)((data[0] << 8 ) | data[1]);
*humi = (humi_y / 65536.0f);
}