提示:硬件IIC/I2C 调试心得
提示:以下是本篇文章正文内容,下面案例可供参考
一、硬件配置
- 上拉 2.2k 电阻
- EEPROM24c02
- stm32 ze
二、软件配置
- GPIO 复用开漏模式
- IIC/I2C 主机模式
三、读写流程
- 写一个字节;
- 读一个字节 ;
- 写一个数组;
- 读一个数组;
note: 细节写在函数下面了
1.头文件
head file :
#ifndef _BSP_I2C_H
#define _BSP_I2C_H
#include "stm32f10x.h"
/**************** device macro **************/
#define EEPROM_ADDR 0xA0
#define ArrayLength(Array) (sizeof(Array)/sizeof(Array[0]))
/**************** GPIO Config raw macro **************/
#define mI2Cx I2C1
#define freq 400000
#define ownAddr 0x7f
#define I2C1_Periph_PORT GPIOB
#define I2C1_SCL_PIN GPIO_Pin_6
#define I2C1_SDA_PIN GPIO_Pin_7
#define I2C1_GPIO_CLK_Cmd RCC_APB2PeriphClockCmd
#define I2C1_GPIO_PORT_CLK RCC_APB2Periph_GPIOB
#define I2C1_CLK_Cmd RCC_APB1PeriphClockCmd
#define I2C1_PORT_CLK RCC_APB1Periph_I2C1
/**************** GPIO Config end **************/
/**************** I2C function **************/
void EE_I2C_SendByte(uint8_t dataAddr,uint8_t byte);
uint8_t EE_I2C_ReceiveByte(uint8_t dataAddr);
/* attention: only write 8byte */
void EE_I2C_SendArray(uint8_t dataAddr,uint8_t *data,uint8_t txBufSize);
void EE_I2C_ReadArray(uint8_t dataAddr,uint8_t *data,uint8_t bufSize);
//wait for write complete
void EEPROM_WaitForWriteEnd(void);
void bsp_I2C_Config(void);
#endif /* _BSP_I2C_H */
note: 头文件不做赘述
2.源文件
source file: GPIO / IIC 配置
/* version: 1.0
* auther : ylb
* date : 2022.3.27
* purpose: hardware IIC
*/
/************* pravate include ***********/
#include "bsp_I2C.h"
#include "bsp_rgb_led.h"
#include "bsp_us