《一个稳定可靠的stm32 i2c程序》由会员分享,可在线阅读,更多相关《一个稳定可靠的stm32 i2c程序(16页珍藏版)》请在金锄头文库上搜索。
1、/*File : i2c.hPurpose : */#ifndef _I2C_H_#define _I2C_H_/* Includes */* Defines */#define SENSORS_I2C I2C2#define I2C_SPEED 400000#define I2C_OWN_ADDRESS 0x00#define I2C_Config() I2cMaster_Init(); /* Prototypes */void I2cMaster_Init(void);unsigned long Sensors_I2C_WriteRegister(unsigned char Address
2、, unsigned char RegisterAddr, unsigned short RegisterLen, const unsigned char *RegisterValue);unsigned long Sensors_I2C_ReadRegister(unsigned char Address, unsigned char RegisterAddr, unsigned short RegisterLen, unsigned char *RegisterValue);int Sensors_I2C_WriteRegister_swap(unsigned char slave_add
3、r,unsigned char reg_addr,unsigned char len, unsigned char *data_ptr);int Sensors_I2C_ReadRegister_swap(unsigned char slave_addr,unsigned char reg_addr,unsigned char len, unsigned char *data_ptr);int I2C_Reset_User();#endif / _I2C_H_/*File : i2c.cPurpose : I2c 3 to communicate with the sensorsAuthor
4、: * Includes */#include #include stm32l1xx.h#include i2c.h#include gpio.h#include log.h#include discover_board.h#include main.h/* Defines */#define I2Cx_FLAG_TIMEOUT (uint32_t) 900)#define I2Cx_LONG_TIMEOUT (uint32_t)(300 * I2Cx_FLAG_TIMEOUT)#define SENSORS_I2C_SCL_GPIO_PORT GPIOB#define SENSORS_I2C
5、_SCL_GPIO_CLK RCC_AHBPeriph_GPIOB#define SENSORS_I2C_SCL_GPIO_PIN GPIO_Pin_10#define SENSORS_I2C_SCL_GPIO_PINSOURCE GPIO_PinSource10 #define SENSORS_I2C_SDA_GPIO_PORT GPIOB#define SENSORS_I2C_SDA_GPIO_CLK RCC_AHBPeriph_GPIOB#define SENSORS_I2C_SDA_GPIO_PIN GPIO_Pin_11#define SENSORS_I2C_SDA_GPIO_PIN
6、SOURCE GPIO_PinSource11#define SENSORS_I2C_RCC_CLK RCC_APB1Periph_I2C2#define SENSORS_I2C_AF GPIO_AF_I2C2#define WAIT_FOR_FLAG(flag, value, timeout, errorcode) I2CTimeout = timeout; while(I2C_GetFlagStatus(SENSORS_I2C, flag) != value) if(I2CTimeout-) = 0) return I2Cx_TIMEOUT_UserCallback(errorcode);
7、 #define CLEAR_ADDR_BIT I2C_ReadRegister(SENSORS_I2C, I2C_Register_SR1); I2C_ReadRegister(SENSORS_I2C, I2C_Register_SR2); /* Globals */* Prototypes */* Function */static uint32_t I2Cx_TIMEOUT_UserCallback(char value);void I2cMaster_Init(void) GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_
8、InitStructure; /* Enable I2Cx clock */ RCC_APB1PeriphClockCmd(SENSORS_I2C_RCC_CLK, ENABLE); /* Enable I2C GPIO clock */ RCC_AHBPeriphClockCmd(SENSORS_I2C_SCL_GPIO_CLK | SENSORS_I2C_SDA_GPIO_CLK, ENABLE); /* Configure I2Cx pin: SCL -*/ GPIO_InitStructure.GPIO_Pin = SENSORS_I2C_SCL_GPIO_PIN; GPIO_Init
9、Structure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; /* Connect pins to Periph */ GPIO_PinAFConfig(SENSORS_I2C_SCL_GPIO_PORT, SENSORS_I2C_SCL_GPIO_PINSOURCE, SENSORS_I2C_A
10、F); GPIO_Init(SENSORS_I2C_SCL_GPIO_PORT, &GPIO_InitStructure); /* Configure I2Cx pin: SDA -*/ GPIO_InitStructure.GPIO_Pin = SENSORS_I2C_SDA_GPIO_PIN; /* Connect pins to Periph */ GPIO_PinAFConfig(SENSORS_I2C_SDA_GPIO_PORT, SENSORS_I2C_SDA_GPIO_PINSOURCE, SENSORS_I2C_AF); GPIO_Init(SENSORS_I2C_SDA_GP
11、IO_PORT, &GPIO_InitStructure); I2C_DeInit(SENSORS_I2C); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C_OWN_ADDRESS; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; /* Enable the I2C peripheral */ I2C_Cmd(SENSORS_I2C, ENABLE); /* Initialize the I2C peripheral */ I2C_Init(SENSORS_I2C, &I2C_InitStructure);/* * brief Basic management of the timeout situation. * param None. * ret