esp32-c3 IIC连续发送数据代码
时间: 2023-09-12 13:03:11 浏览: 174
esp32使用IIC接口
以下是ESP32-C3的IIC连续发送数据的代码示例:
```c
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#define I2C_MASTER_SCL_IO 27 /*!< GPIO number for I2C master clock */
#define I2C_MASTER_SDA_IO 26 /*!< GPIO number for I2C master data */
#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
#define I2C_SLAVE_ADDR 0x68 /*!< ESP32-C3 I2C slave address */
#define I2C_SLAVE_WRITE_BIT 0 /*!< I2C slave write bit */
#define I2C_SLAVE_ACK_CHECK_EN 1 /*!< I2C slave ack check enable */
#define I2C_SLAVE_ACK_CHECK_DIS 0 /*!< I2C slave ack check disable */
#define I2C_SLAVE_ACK_VAL 0 /*!< I2C slave ack value */
#define I2C_SLAVE_NACK_VAL 1 /*!< I2C slave nack value */
/**
* @brief I2C master initialization
*/
static esp_err_t i2c_master_init(void)
{
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master = {
.clk_speed = I2C_MASTER_FREQ_HZ,
},
};
return i2c_param_config(I2C_MASTER_NUM, &conf) == ESP_OK ? i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0) : ESP_FAIL;
}
/**
* @brief I2C slave initialization
*/
static esp_err_t i2c_slave_init(void)
{
i2c_config_t conf = {
.mode = I2C_MODE_SLAVE,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.slave = {
.addr_10bit_en = 0,
.slave_addr = I2C_SLAVE_ADDR,
},
};
return i2c_param_config(I2C_MASTER_NUM, &conf) == ESP_OK ? i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0) : ESP_FAIL;
}
/**
* @brief I2C master continuous send data task
*/
static void i2c_master_task(void *arg)
{
uint8_t data[2] = {0x01, 0x02};
while (1) {
esp_err_t ret;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (I2C_SLAVE_ADDR << 1) | I2C_SLAVE_WRITE_BIT, I2C_SLAVE_ACK_CHECK_EN);
i2c_master_write(cmd, data, 2, I2C_SLAVE_ACK_CHECK_EN);
i2c_master_stop(cmd);
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) {
printf("I2C Master send data success\n");
} else {
printf("I2C Master send data failed\n");
}
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
/**
* @brief I2C slave receive data task
*/
static void i2c_slave_task(void *arg)
{
i2c_slave_set_addr(I2C_MASTER_NUM, I2C_SLAVE_ADDR);
i2c_cmd_handle_t cmd;
while (1) {
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (I2C_SLAVE_ADDR << 1) | I2C_SLAVE_WRITE_BIT, I2C_SLAVE_ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret != ESP_OK) {
printf("I2C Slave receive data failed\n");
}
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
void app_main()
{
i2c_master_init();
i2c_slave_init();
xTaskCreate(i2c_master_task, "i2c_master_task", 2048, NULL, 10, NULL);
xTaskCreate(i2c_slave_task, "i2c_slave_task", 2048, NULL, 10, NULL);
}
```
在该示例中,I2C主任务通过`i2c_master_write`函数向I2C从设备连续发送2个字节的数据。I2C从任务在循环中等待I2C主设备发送数据。当I2C主设备发送数据时,I2C从设备会通过`i2c_master_start`函数开始接收数据。
如果I2C主设备成功发送数据,则会在串行监视器中看到"I2C主发送数据成功"的消息。如果I2C从设备成功接收数据,则会在串行监视器中看到"I2C从接收数据成功"的消息。
阅读全文