From 14174ffa946b3a96154dc077911eac4be9105f0c Mon Sep 17 00:00:00 2001 From: "DESKTOP-2E6P1Q6\\Administrator" <1625800082@qq.com> Date: Wed, 24 Sep 2025 17:50:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A1=AC=E4=BB=B6I2C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EWARM/Project.ewp | 6 ++ EWARM/Project.ewt | 6 ++ drivers/drv_i2c.c | 121 ++++++++++++++++++++++++++++++++++++ drivers/drv_i2c.h | 41 ++++++++++++ drivers/mb85rc64/mb85rc64.h | 5 +- drivers/tmp75/tmp75.c | 2 + 6 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 drivers/drv_i2c.c create mode 100644 drivers/drv_i2c.h diff --git a/EWARM/Project.ewp b/EWARM/Project.ewp index ee20536..0b6edc0 100644 --- a/EWARM/Project.ewp +++ b/EWARM/Project.ewp @@ -2230,6 +2230,12 @@ $PROJ_DIR$\..\drivers\drv_adc.h + + $PROJ_DIR$\..\drivers\drv_i2c.c + + + $PROJ_DIR$\..\drivers\drv_i2c.h + $PROJ_DIR$\..\drivers\drv_usart.c diff --git a/EWARM/Project.ewt b/EWARM/Project.ewt index 410f1a9..e8c39c0 100644 --- a/EWARM/Project.ewt +++ b/EWARM/Project.ewt @@ -2901,6 +2901,12 @@ $PROJ_DIR$\..\drivers\drv_adc.h + + $PROJ_DIR$\..\drivers\drv_i2c.c + + + $PROJ_DIR$\..\drivers\drv_i2c.h + $PROJ_DIR$\..\drivers\drv_usart.c diff --git a/drivers/drv_i2c.c b/drivers/drv_i2c.c new file mode 100644 index 0000000..990543a --- /dev/null +++ b/drivers/drv_i2c.c @@ -0,0 +1,121 @@ +/************************************************* +Copyright (c) 2025, 成都赛联科技有限责任公司 +All rights reserved. +@file drv_i2c.C +@brief drv_i2c驱动程序 + +@details +@note +@author dufresne +@date 2025/09/24 + +@version v1.0 2025/09/24 初始版本 +*************************************************/ + +#include "drv_i2c.h" +#include "rtthread.h" +#include + +/* + @ brief 延时函数 + @ param + @ return + @ note 2025-09-15 +*/ +static void delay_us(int us) +{ + rt_thread_udelay(us); +} + +void i2c_init(void) +{ + i2c_gpio_config(); + i2c_config(); +} +/* + @ brief 初始化tmp75芯片GPIO引脚 + @ param + @ return + @ note 2025-09-15 +*/ +void i2c_gpio_config(void) +{ + // 配置引脚时钟 + rcu_periph_clock_enable(RCU_GPIOB); + + gpio_af_set(GPIOB, GPIO_AF_4, I2C_SCL_PIN | I2C_SDA_PIN); + /* 初始化GPIO复用功能模式 */ + gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, I2C_SCL_PIN | I2C_SDA_PIN); + gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, I2C_SCL_PIN | I2C_SDA_PIN); +} + +void i2c_config(void) +{ + // 启用 I2C 外设的时钟 + rcu_periph_clock_enable(RCU_I2C0); + + // 配置 I2C 的时钟参数: + // I2C_PERIPH:这里使用的I2C1 + // I2C_SPEED:通信速率(单位为 Hz,常用为 100000 或 400000),这里使用100000 + i2c_clock_config(I2C_PERIPH, I2C_SPEED, I2C_DTCY_2); + + // 配置 I2C 工作模式和地址: + // I2C_ADDFORMAT_7BITS:使用 7 位地址模式 + // I2C_BUS_ADDRESS:SD2068器件代码0110010 = 0x32 + i2c_mode_addr_config(I2C_PERIPH, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_BUS_ADDRESS); + + // 启用 I2C 外设 + i2c_enable(I2C_PERIPH); + + // 使能 ACK 应答功能,确保在接收数据后自动发送 ACK + i2c_ack_config(I2C_PERIPH, I2C_ACK_ENABLE); +} + +/* + @ brief 读取温度值 + @ param + @ return + @ note 2025-09-15 +*/ +float tmp75_read_temp(void) +{ + uint8_t tempH = 0; + uint8_t tempL = 0; + uint16_t tempCode = 0; + float temp = 0; + + // 起始信号 + i2c_start(); + // 写tmp75地址 + tmp75_write_byte(TMP75_ADDRESS); + // 接收tmp75的ack信息 + tmp75_ack(); + // 发送需读取数据的地址 + tmp75_write_byte(TEMP_REGISTER_ADDRESS); + tmp75_ack(); + + i2c_start(); + // 写tmp75地址 + tmp75_write_byte(TMP75_ADDRESS + 1); // 读地址数据 + tmp75_ack(); + tempH = tmp75_read_byte(); + master_ack(); + tempL = tmp75_read_byte(); + master_noack(); + i2c_stop(); + + tempCode = (tempH << 8) | tempL; + tempCode = tempCode >> 6; + if (tempCode & 0x200) // 负温度 + { + tempCode &= 0x1ff; + temp = ((float)tempCode - 512) / 4; + } + else + { + temp = (float)tempCode / 4; + } + TMP75_SDA_LOW; + TMP75_SCL_LOW; + return (temp); +} diff --git a/drivers/drv_i2c.h b/drivers/drv_i2c.h new file mode 100644 index 0000000..5ecf346 --- /dev/null +++ b/drivers/drv_i2c.h @@ -0,0 +1,41 @@ +///Copyright (c) 2022, 四川汇源光通信有限公司 +///All rights reserved. +///@file drv_i2c.h +///@brief drv_i2c驱动程序 +/// +///@details +///@note +///@author dufresne +///@date 2025/09/24 +/// +///@version v1.0 2025/09/24 初始版本 + +#ifndef DRV_I2C_H +#define DRV_I2C_H + +#include "gd32f4xx_gpio.h" +#include "gd32f4xx_i2c.h" + + + +#define I2C_PERIPH I2C0 +#define I2C_SCL_PIN GPIO_PIN_6 +#define I2C_SDA_PIN GPIO_PIN_7 +#define I2C_SPEED 100000 + +// #define I2C_SCL_HIGH gpio_bit_set(GPIOB, I2C_SCL_PIN) +// #define I2C_SCL_LOW gpio_bit_reset(GPIOB, I2C_SCL_PIN) + +// #define I2C_SDA_HIGH gpio_bit_set(GPIOB, I2C_SDA_PIN) +// #define I2C_SDA_LOW gpio_bit_reset(GPIOB, I2C_SDA_PIN) +// #define I2C_SDA_GET gpio_input_bit_get(GPIOB, I2C_SDA_PIN) + + +void i2c_init(void); +void i2c_gpio_config(void); +void i2c_config(void); + +void temp75_gpio_init(); +float tmp75_read_temp(void); + +#endif \ No newline at end of file diff --git a/drivers/mb85rc64/mb85rc64.h b/drivers/mb85rc64/mb85rc64.h index ee46b3d..ea29f04 100644 --- a/drivers/mb85rc64/mb85rc64.h +++ b/drivers/mb85rc64/mb85rc64.h @@ -17,6 +17,9 @@ #define MB85RC64_ADDRESS_WRITE 0xA0 #define MB85RC64_ADDRESS_READ 0xA1 +//mb85rc64洢ַ +#define MB85RC64_SAVE_ADDRESS_BEGIN 0x0000 + #define MB85RC64_SCL_HIGH gpio_bit_set(GPIOB, GPIO_PIN_6) #define MB85RC64_SCL_LOW gpio_bit_reset(GPIOB, GPIO_PIN_6) @@ -25,8 +28,6 @@ #define MB85RC64_SDA_LOW gpio_bit_reset(GPIOB, GPIO_PIN_7) #define MB85RC64_SDA_GET gpio_input_bit_get(GPIOB, GPIO_PIN_7) -//mb85rc64洢ַ -#define MB85RC64_SAVE_ADDRESS_BEGIN 0x0000 void mb85rc64_gpio_init(); char mb85rc64_page_write(unsigned short int addr, unsigned char* data, int data_len); diff --git a/drivers/tmp75/tmp75.c b/drivers/tmp75/tmp75.c index d3b3e68..b907cf6 100644 --- a/drivers/tmp75/tmp75.c +++ b/drivers/tmp75/tmp75.c @@ -207,6 +207,7 @@ static void master_noack() TMP75_SCL_LOW; delay_us(5); } + /* @ brief ȡ¶ֵ @ param @@ -239,6 +240,7 @@ float tmp75_read_temp(void) tempL = tmp75_read_byte(); master_noack(); i2c_stop(); + tempCode = (tempH << 8) | tempL; tempCode = tempCode >> 6; if (tempCode & 0x200) // ¶