一个稳定可靠的STM32 I2C程序(13页).doc
《一个稳定可靠的STM32 I2C程序(13页).doc》由会员分享,可在线阅读,更多相关《一个稳定可靠的STM32 I2C程序(13页).doc(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-一个稳定可靠的STM32 I2C程序-第 13 页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(
2、unsigned char Address, 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(un
3、signed char slave_addr,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
4、 the sensorsAuthor : * 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
5、#define SENSORS_I2C_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 SENSOR
6、S_I2C_SDA_GPIO_PINSOURCE 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_UserCa
7、llback(errorcode); #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
8、_InitTypeDef I2C_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_GP
9、IO_PIN; GPIO_InitStructure.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_PINSOUR
10、CE, SENSORS_I2C_AF); 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(S
11、ENSORS_I2C_SDA_GPIO_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_AcknowledgedAd
12、dress = 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. * retval N
13、one.static uint32_t I2Cx_TIMEOUT_UserCallback(char value) /* The following code allows I2C error recovery and return to normal communication if the error source doesnt still exist (ie. hardware issue.) */ I2C_InitTypeDef I2C_InitStructure; I2C_GenerateSTOP(SENSORS_I2C, ENABLE); I2C_SoftwareResetCmd(
14、SENSORS_I2C, ENABLE); I2C_SoftwareResetCmd(SENSORS_I2C, DISABLE); 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.
15、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);#ifdef User_debug MPL_LOGI(I2C Restarted.n);#endif return
16、1;int I2C_Reset_User() return I2Cx_TIMEOUT_UserCallback(0); * brief Writes a Byte to a given register to the sensors through the control interface (I2C) * param RegisterAddr: The address (location) of the register to be written. * param RegisterValue: the Byte value to be written into destination re
17、gister. * retval 0 if correct communication, else wrong communicationunsigned long Sensors_I2C_WriteRegister(unsigned char Address, unsigned char RegisterAddr, unsigned short RegisterLen, const unsigned char *RegisterValue) uint32_t result = 0; uint32_t i = RegisterLen; _IO uint32_t I2CTimeout = I2C
18、x_LONG_TIMEOUT; /RegisterValue = RegisterValue + (RegisterLen - 1); /* Wait for the busy flag to be cleared */ WAIT_FOR_FLAG (I2C_FLAG_BUSY, RESET, I2Cx_LONG_TIMEOUT, 1); /* Start the config sequence */ I2C_GenerateSTART(SENSORS_I2C, ENABLE); /* Wait for the start bit to be set */ WAIT_FOR_FLAG (I2C
19、_FLAG_SB, SET, I2Cx_FLAG_TIMEOUT, 2); /* Transmit the slave address and enable writing operation */ I2C_Send7bitAddress(SENSORS_I2C, (Address1), I2C_Direction_Transmitter); /* Wait for address bit to be set */ WAIT_FOR_FLAG (I2C_FLAG_ADDR, SET, I2Cx_FLAG_TIMEOUT, 3); /* clear the ADDR interrupt bit
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 一个稳定可靠的STM32 I2C程序13页 一个 稳定 可靠 STM32 I2C 程序 13
限制150内