使用MicroPython的语法编写ESP32通过IIC的通讯方式与STM32103ZET6进行数据传输的代码和STM32与ESP32进行IIC通信的代码
时间: 2023-05-26 18:06:20 浏览: 130
esp32使用IIC接口
ESP32通过I2C与STM32进行数据传输的代码:
```python
import machine
import time
# 使用I2C接口1,SCL引脚为22,SDA引脚为21
i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=100000)
# STM32的I2C设备地址
stm_i2c_addr = 0x48
# 从STM32读取数据
def read_from_stm():
i2c.writeto(stm_i2c_addr, bytes([0x00])) # 发送读命令
data = i2c.readfrom(stm_i2c_addr, 4) # 读取4个字节
value = int.from_bytes(data, 'little') # 将字节转换为整数
return value
# 向STM32写入数据
def write_to_stm(data):
i2c.writeto(stm_i2c_addr, data.to_bytes(4, 'little'))
# 循环发送数据到STM32,每隔1秒发送一次
while True:
value = int(input("请输入要发送的数据:"))
write_to_stm(value)
time.sleep(1)
```
STM32与ESP32进行I2C通信的代码:
```c
#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include "misc.h"
#include "delay.h"
#define STM_I2C_ADDR 0x48
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/* I2C1 configuration */
void I2C1_Configuration(void)
{
/* Enable I2C1 and GPIOB clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* Configure I2C1 pins: SCL and SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; // 外部开漏
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* I2C1 configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Init(I2C1, &I2C_InitStructure);
/* Enable I2C1 */
I2C_Cmd(I2C1, ENABLE);
}
/* Send byte to I2C bus */
void I2C_SendByte(I2C_TypeDef *I2Cx, uint8_t data)
{
I2C_SendData(I2Cx, data);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* Receive byte from I2C bus */
uint8_t I2C_ReceiveByte(I2C_TypeDef* I2Cx)
{
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
uint8_t data = I2C_ReceiveData(I2Cx);
return data;
}
/* Read integer from I2C bus */
int I2C_ReadInt(I2C_TypeDef* I2Cx, uint8_t slave_addr)
{
int data = 0;
// 发送I2C读命令
I2C_GenerateSTART(I2Cx, ENABLE);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, slave_addr, I2C_Direction_Receiver);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
// 读取数据,并转换为整数
uint8_t b0 = I2C_ReceiveByte(I2Cx);
uint8_t b1 = I2C_ReceiveByte(I2Cx);
uint8_t b2 = I2C_ReceiveByte(I2Cx);
uint8_t b3 = I2C_ReceiveByte(I2Cx);
data = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
I2C_GenerateSTOP(I2Cx, ENABLE);
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
return data;
}
/* Write integer to I2C bus */
void I2C_WriteInt(I2C_TypeDef* I2Cx, uint8_t slave_addr, int data)
{
// 发送I2C写命令
I2C_GenerateSTART(I2Cx, ENABLE);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, slave_addr, I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// 将数据写入I2C总线
I2C_SendByte(I2Cx, data & 0xFF);
I2C_SendByte(I2Cx, (data >> 8) & 0xFF);
I2C_SendByte(I2Cx, (data >> 16) & 0xFF);
I2C_SendByte(I2Cx, (data >> 24) & 0xFF);
I2C_GenerateSTOP(I2Cx, ENABLE);
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
}
int main(void)
{
/* Configure clocks */
SystemInit();
/* Configure delay function */
delay_init();
/* Configure I2C1 */
I2C1_Configuration();
/* Main loop */
while(1)
{
// 从ESP32读取数据
int data = I2C_ReadInt(I2C1, STM_I2C_ADDR);
// 发送数据到ESP32
I2C_WriteInt(I2C1, STM_I2C_ADDR, data);
delay_ms(1000);
}
}
```
阅读全文