#申请原创#
BMP085是一款数字式温度大气压传感器,其接口方式为I2C,在OLED屏的配合下就可实现对环境温度及大气压的检测与显示,其效果如下图所示。 检测与显示效果 在检测电路中,这2个外挂器件与开发板的连接关系为: BMP085: SCL ---PC4 SDA--- PC5 OLED: SCL---PC20 SDA---PC21
这2个外挂器件的驱动控制均采用GPIO口模拟I2C的方式,其中BMP085以模拟方式发送字节数据的函数为: void BMP085_Send_Byte(char txd)
{
char t;
OUTPUT_MODE_SET();
SCL_Clr1();
for(t=0;t<8;t++)
{
if((txd & 0x80)>>7)
SDA_Set1();
else
SDA_Clr1();
txd<<=1;
delay_us(5);
SCL_Set1();
delay_us(5);
SCL_Clr1();
delay_us(5);
}
}
以模拟方式接受字节数据的函数为: char BMP085_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
INPUT_MODE_SET();
for(i=0;i<8;i++)
{
SCL_Clr1();
delay_us(5);
SCL_Set1();
receive<<=1;
if(SDA_IN()==1) receive++;
delay_us(5);
}
if(!ack)
BMP085_NAck();
else
BMP085_Ack();
return receive;
}
读取温度的函数为: long bmp085ReadTemp(void)
{
BMP085_Start();
BMP085_Send_Byte(BMP085_SlaveAddress);
while(BMP085_Wait_Ack());
BMP085_Send_Byte(0xF4);
while(BMP085_Wait_Ack());
BMP085_Send_Byte(0x2E);
while(BMP085_Wait_Ack());
BMP085_Stop();
SYSTICK_DelayMs(10);
return (long) Multiple_read(0xF6);
}
读取大气压的函数为: long bmp085ReadPressure(void)
{
pressure = 0;
BMP085_Start();
BMP085_Send_Byte(BMP085_SlaveAddress);
while(BMP085_Wait_Ack()){}
BMP085_Send_Byte(0xF4);
while(BMP085_Wait_Ack()){}
BMP085_Send_Byte(0x34);
while(BMP085_Wait_Ack()){}
BMP085_Stop();
SYSTICK_DelayMs(10);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
实现图示效果的主程序为: int main ( void )
{
SYS_Initialize ( NULL );
SYSTICK_TimerStart();
PORT_REGS->GROUP[2].PORT_DIRSET = 1<<18;
PORT_REGS->GROUP[2].PORT_DIRSET = 1<<20;
PORT_REGS->GROUP[2].PORT_DIRSET = 1<<21;
PORT_REGS->GROUP[2].PORT_DIRSET = 1<<4;
PORT_REGS->GROUP[2].PORT_DIRCLR = 1<<5;
PORT_REGS->GROUP[2].PORT_PINCFG[5] = 0x0;
OLED_Init();
OLED_Clear();
OLED_Clear();
OLED_ShowString(0,0,"t= C",16);
OLED_ShowString(0,2,"p= KPa",16);
Init_BMP085();
SYSTICK_DelayMs(1000);
SYSTICK_DelayMs(100);
while(1)
{
bmp085Convert();
OLED_ShowNum(24,0,temperature1/10,3,16);
OLED_ShowNum(24,2,pressure/100,5,16);
SYSTICK_DelayMs(500);
}
}
ATSAMD51EVK开发板除了MCC不好用之外,用起来还是比较得心应手的!
|