• 大小: 3KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-05
  • 语言: 其他
  • 标签: BMP180  IIC  

资源简介

气压传感器BMP180驱动程序,IIC协议,STM32f103,主频72M,直接可用。

资源截图

代码片段和文件信息

#include “BMP180.h“
#include “delay.h“
//PA6--SCL    PA7--SDA
//I2C总线初始化
//配置SDA信号线为输入模式
u8 BMP_communication_sta = 1;
void BMP180_SDA_Input_Mode()
{
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = BMP180_SDA;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;  

  GPIO_Init(BMP180_I2C_PORT &GPIO_InitStructure);
}

//配置SDA信号线为输出模式
void BMP180_SDA_Output_Mode()
{
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = BMP180_SDA;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(BMP180_I2C_PORT &GPIO_InitStructure);
}

void BMP180_SDA_Output( uint16_t val )
{
if ( val ) {
GPIO_SetBits(BMP180_I2C_PORTBMP180_SDA);
} else {
GPIO_ResetBits(BMP180_I2C_PORTBMP180_SDA);
}
}

//
uint8_t BMP180_SDA_Input()
{
return GPIO_ReadInputDataBit(BMP180_I2C_PORT BMP180_SDA);
}

//
void BMP180_SCL_Output( uint16_t val )
{
if ( val ) {
GPIO_SetBits(BMP180_I2C_PORTBMP180_SCL);
} else {
GPIO_ResetBits(BMP180_I2C_PORTBMP180_SCL);
}
}
//延时程序
void BMP180_delay1(unsigned int n)
{
unsigned int i;
for ( i=0;i}
//I2C总线启动
void BMP180_I2CStart(void)
{
BMP180_SDA_Output(1);BMP180_delay1(500);
BMP180_SCL_Output(1);BMP180_delay1(500);
BMP180_SDA_Output(0);BMP180_delay1(500);
BMP180_SCL_Output(0);BMP180_delay1(500);
}

//I2C总线停止
void BMP180_I2CStop(void)
{
BMP180_SCL_Output(0); BMP180_delay1(500);
BMP180_SDA_Output(0); BMP180_delay1(500);
BMP180_SCL_Output(1); BMP180_delay1(500);
BMP180_SDA_Output(1); BMP180_delay1(500);

}

//等待应答
unsigned char BMP180_I2CWaitAck(void)
{
unsigned short cErrTime = 5;
BMP180_SDA_Input_Mode(); 
BMP180_delay1(500);
BMP180_SCL_Output(1);
BMP180_delay1(500);
while(BMP180_SDA_Input())
{
cErrTime--;
BMP180_delay1(500);
if (0 == cErrTime)
{
BMP180_SDA_Output_Mode();
BMP_communication_sta = 0;
BMP180_I2CStop();
return 0;
}
}
BMP180_SDA_Output_Mode();
BMP180_SCL_Output(0);
BMP180_delay1(500); 
return 1;
}

//发送应答位
void BMP180_I2CSendAck(void)
{
BMP180_SDA_Output(0);
BMP180_delay1(500);
BMP180_delay1(500);
BMP180_SCL_Output(1); 
BMP180_delay1(500);
BMP180_SCL_Output(0); 
BMP180_delay1(500);

}

//
void BMP180_I2CSendNotAck(void)
{
BMP180_SDA_Output(1);
BMP180_delay1(500);
BMP180_SCL_Output(1); 
BMP180_delay1(500);
BMP180_SCL_Output(0); 
BMP180_delay1(500);

}


//通过I2C总线发送一个字节数据
void BMP180_I2CSendByte(unsigned char cSendByte)
{
unsigned char  i = 8;
while (i--)
{
BMP180_SCL_Output(0);
BMP180_delay1(500); 
BMP180_SDA_Output(cSendByte & 0x80); 
BMP180_delay1(500);
cSendByte += cSendByte;
BMP180_delay1(500); 
BMP180_SCL_Output(1);
BMP180_delay1(500); 
}
BMP180_SCL_Output(0);
BMP180_delay1(500); 
}

//从I2C总线接收一个字节数据
unsigned char BMP180_I2CReceiveByte(void)
{

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       8079  2018-05-05 12:39  BMP180\BMP180.c

     文件        512  2018-05-05 12:38  BMP180\BMP180.h

     目录          0  2018-09-16 16:40  BMP180

----------- ---------  ---------- -----  ----

                 8591                    3


评论

共有 条评论