一、IIC的初始化
esp_err_t DAC6574_init(void)
{
int i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = DAC6574_SDA_PIN,
.scl_io_num = DAC6574_SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_DISABLE,
.scl_pullup_en = GPIO_PULLUP_DISABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
i2c_param_config(i2c_master_port, &conf);
i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
if (DAC6574_Set_Vout_write(DAC6574_CONTROL_ALL, 0) == ESP_OK)
{
ESP_LOGI(TAG,"DAC6574 init success\r\n");
return ESP_OK;
}
else
{
ESP_LOGI(TAG,"DAC6574 init failed\r\n");
return ESP_FAIL;
}
}
二、DAC6574的介绍
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/cbc61cc8d7a34fc69b72438aa94bea69.png)
三、DAC6574的驱动编写
#include "DAC6574.h"
static const char *TAG = "DAC6574";
static esp_err_t DAC6574_register_write_8bit(uint8_t reg_addr, uint8_t data);
static esp_err_t DAC6574_register_read_8bit(uint8_t reg_addr, uint8_t *data);
static esp_err_t DAC6574_register_write_16bit(uint8_t reg_addr, uint16_t data);
static esp_err_t DAC6574_register_read_16bit(uint8_t reg_addr, uint16_t *data);
esp_err_t DAC6574_Set_Vout_write(uint8_t reg_addr, float pct)
{
uint16_t Data;
uint16_t readData;
Data = BIN2VAL(pct);
Data = Data << 6;
int flag = 0;
while (flag <= 2)
{
if (DAC6574_register_write_16bit(reg_addr, Data) != ESP_OK)
{
ESP_LOGI(TAG,"DAC6574 offline\r\n");
flag++;
continue;
}
DAC6574_register_read_16bit(reg_addr, &readData);
if (readData != Data)
{
ESP_LOGI(TAG,"DAC6574 set error(Vout_no_set)readData %x data %x\r\n", readData, Data);
flag++;
continue;
}
break;
}
if (flag > 2)
{
ESP_LOGI(TAG,"Set Vout channel error \r\n");
return ESP_FAIL;
}
return ESP_OK;
}
static esp_err_t DAC6574_register_write_8bit(uint8_t reg_addr, uint8_t data)
{
uint8_t write_buf[2] = {reg_addr, data};
return i2c_master_write_to_device(I2C_MASTER_NUM, DAC6574_ADDR, write_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
}
static esp_err_t DAC6574_register_read_8bit(uint8_t reg_addr, uint8_t *data)
{
return i2c_master_write_read_device(I2C_MASTER_NUM, DAC6574_ADDR, ®_addr, 1, data, 1, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
}
static esp_err_t DAC6574_register_write_16bit(uint8_t reg_addr, uint16_t data)
{
uint8_t write_buf[3] = {reg_addr, (uint8_t)(data >> 8), (uint8_t)data};
return i2c_master_write_to_device(I2C_MASTER_NUM, DAC6574_ADDR, write_buf, 3, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
}
static esp_err_t DAC6574_register_read_16bit(uint8_t reg_addr, uint16_t *data)
{
esp_err_t ret;
uint8_t write_buf[2];
ret = i2c_master_write_read_device(I2C_MASTER_NUM, DAC6574_ADDR, ®_addr, 1, write_buf, 2, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
*data = write_buf[1] + (write_buf[0] << 8);
return ret;
}
esp_err_t DAC6574_init(void)
{
int i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = DAC6574_SDA_PIN,
.scl_io_num = DAC6574_SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_DISABLE,
.scl_pullup_en = GPIO_PULLUP_DISABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
i2c_param_config(i2c_master_port, &conf);
i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
if (DAC6574_Set_Vout_write(DAC6574_CONTROL_ALL, 0) == ESP_OK)
{
ESP_LOGI(TAG,"DAC6574 init success\r\n");
return ESP_OK;
}
else
{
ESP_LOGI(TAG,"DAC6574 init failed\r\n");
return ESP_FAIL;
}
}