学前班
最后登录1970-1-1
在线时间 小时
注册时间2017-7-10
|
1火花
如题
- #include "I2C.h"
- static void I2C_GPIO_Config(void){
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
-
- GPIO_InitTypeDef GPIO_InitStruct;
-
- GPIO_PinAFConfig(I2C_PORT,I2C_SCL_SOURCE,GPIO_AF_I2C1);
- GPIO_PinAFConfig(I2C_PORT,I2C_SDA_SOURCE,GPIO_AF_I2C1);
-
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
-
- GPIO_InitStruct.GPIO_Pin = I2C_SCL | I2C_SDA;
-
- GPIO_Init(I2C_PORT,&GPIO_InitStruct);
- }
- static void I2C_Mode_Config(void){
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
-
- I2C_InitTypeDef I2C_IbitStruct;
-
- I2C_DeInit(I2C_BASIC);
- I2C_IbitStruct.I2C_ClockSpeed = I2C_SPEED;
- I2C_IbitStruct.I2C_Mode = I2C_Mode_I2C;
- I2C_IbitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
- I2C_IbitStruct.I2C_OwnAddress1 = OWN_ADRESS;
- I2C_IbitStruct.I2C_Ack = I2C_Ack_Enable;
- I2C_IbitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
-
- I2C_Init(I2C_BASIC,&I2C_IbitStruct);
-
- I2C_Cmd(I2C1,ENABLE);
-
- I2C_AcknowledgeConfig(I2C_BASIC, ENABLE);
- }
- void I2C_Config(void){
- I2C_GPIO_Config();
- I2C_Mode_Config();
- }
复制代码- #ifndef _I2C_H_
- #define _I2C_H_
- #include "stm32f4xx.h"
- #define I2C_PORT GPIOB
- #define I2C_BASIC I2C1
- #define I2C_SCL GPIO_Pin_6
- #define I2C_SCL_SOURCE GPIO_PinSource6
- #define I2C_SDA GPIO_Pin_7
- #define I2C_SDA_SOURCE GPIO_PinSource7
- #define I2C_SPEED 10000
- #define OWN_ADRESS 0xEE
- void I2C_Config(void);
- #endif /*_I2C_H_*/
复制代码- #include "BMP180.h"
- static short BMP180_Read_Data(unsigned char ADDR){
- unsigned char msb,lsb;
- short Data;
-
- while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
-
- I2C_AcknowledgeConfig(BMP180_I2C, ENABLE);
-
- I2C_GenerateSTART(BMP180_I2C,ENABLE);
-
- while(!I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_MODE_SELECT)){
- }
-
- I2C_Send7bitAddress(BMP180_I2C,BMP180_Write_ADDR,I2C_Direction_Transmitter);
-
- while(!I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){
- }
-
- I2C_SendData(BMP180_I2C,ADDR);
-
- while(!I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_TRANSMITTED)){
- }
-
- I2C_GenerateSTART(BMP180_I2C,ENABLE);
-
- while(!I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_MODE_SELECT)){
- }
-
- I2C_Send7bitAddress(BMP180_I2C,BMP180_Read_ADDR,I2C_Direction_Transmitter);
-
- while(!I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)){
- }
-
- msb = I2C_ReceiveData(BMP180_I2C);
-
- while(!I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED)){
- }
-
- I2C_NACKPositionConfig(BMP180_I2C,I2C_NACKPosition_Next);
-
- lsb = I2C_ReceiveData(BMP180_I2C);
-
- while(!I2C_CheckEvent(BMP180_I2C,I2C_EVENT_MASTER_BYTE_RECEIVED)){
- }
-
- I2C_NACKPositionConfig(BMP180_I2C,I2C_NACKPosition_Current);
-
-
- Data = (msb << 8);
- Data |= lsb;
- return Data;
- }
复制代码- #include "stm32f4xx.h"
- #include "I2C.h"
- /*íaéèoê*/
- #define BMP180_I2C I2C1
-
- /*BMP80μØÖ·*/
- #define BMP180_Write_ADDR 0xEE
- #define BMP180_Read_ADDR 0xEF
复制代码
|
|