环境对我们的生活质量有着重要的作用,本打算设计一个家居控制系统,无奈温湿度传感器DHT22对时钟序列的要求比较高,在M471上难以驱动。为此,只能调整设计目标完成一个环境状态检测器。 该检测器的结构框图见图1所示。 图1 结构框图 其中传感器BMP085的作用是检测温度和大气压,是一个数字式传感器;而传感器BH1750则用于光照强度检测,也是一种数字式传感器。由于是数字式传感器,因而省去了数字标定方面的处理。其涉及的显示器件则是LCD5110液晶屏,其线路连接及显示效果如图2所示。 图2 线路连接及显示效果 在引脚连接方面,其连接关系如下: 1)LCD5110屏 VCC--- VREF GND--- VSS SCE--- PA2 RST--- PA1 D/C--- PA0 SDIN--- PA3 SCLK--- PA4 LED+--- PA5 2)BMP085 VCC --- 3.3V GND --- GND SCL --- PC3 SDA --- PC2 3)BH1750 VCC --- 3.3V GND --- GND SCL --- PC4 SDA --- PC5 出于使用灵活性的考虑,尽管所用的传感器均为I2C接口的传感器,这里则是采用I/O口模拟的方式来驱动这2个器件。 1. LCD5110屏屏显示驱动 OLED屏的作用主要在于字符、数值字符串显示,以及中文内容的显示,这方面的内容参见: LCD5110屏显示 网址:https://bbs.21ic.com/icview-3153790-1-1.html 2.BMP085温度大气压 为使用BMP085进行检测,其初始化函数为: void Init_BMP085()
{
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
}
BMP085发送与接收字节数据的函数为: void BMP085_Send_Byte(char txd)
{
char t;
IIC_OUTPUT_MODE_SET();
SCL_Clr();
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
SDA_Set();
else
SDA_Clr();
txd<<=1;
delayMicroseconds(2);
SCL_Set();
delayMicroseconds(2);
SCL_Clr();
delayMicroseconds(2);
}
}
char BMP085_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
IIC_INPUT_MODE_SET();
for(i=0;i<8;i++ )
{
SCL_Clr();
delayMicroseconds(2);
SCL_Set();
receive<<=1;
if(IIC_SDA_IN) receive++;
delayMicroseconds(1);
}
if (!ack)
BMP085_NAck();
else
BMP085_Ack();
return receive;
}
BMP085读取温度与大气压的函数为: 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();
delay(10);
return (long) Multiple_read(0xF6);
}
long bmp085ReadPressure(void)
{
long 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();
delay(10);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
实现数据变换处理的函数为: void bmp085Convert()
{
unsigned int ut;
unsigned long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp085ReadTemp();
up = bmp085ReadPressure();
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = ((b5 + 8) >> 4);
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
pressure = p+((x1 + x2 + 3791)>>4);
}
3.BH1750光照强度 为使用BH1750进行字节数据的发送,其功能函数为: void BH1750_SendByte(char data)
{
char i;
pinMode(SDA,OUTPUT);
digitalWrite(SCL, LOW);
delayMicroseconds(2);
for (i=0; i<8; i++) //8位计数器
{
if(data&0x80) digitalWrite(SDA, HIGH);
else digitalWrite(SDA, LOW);
data <<= 1;
digitalWrite(SCL, HIGH); //拉高时钟线
delayMicroseconds(2); //延时
digitalWrite(SCL, LOW); //拉低时钟线
delayMicroseconds(2); //延时
}
}
char BH1750_RecvByte()
{
char i;
char data = 0;
IIC_INPUT_MODE_SET();
for (i=0;i<8;i++)
{
SCL_Clr();
TIMER_Delay(TIMER0, 2);
SCL_Set();
data <<= 1;
SCL_Set();
if(PC5==1) data|=0x01;
TIMER_Delay(TIMER0, 1);
}
SCL_Clr();
return data;
}
获取并输出光强值的函数为: void Get_Sunlight_Value()
{
int dis_data=0;
float temp;
char i=0;
unsigned int sd;
Single_Write_BH1750(0x01);
Single_Write_BH1750(0x10);
delay(180);
Multiple_Read_BH1750();
for(i=0;i<3;i++) dis_data=BUF[0];
dis_data=(dis_data<<8)+BUF[1];
temp=(float)dis_data/1.2;
sd=temp;
Serial.println(sd);
}
4.实现显示效果的主程序为: int main()
{
uint32_t i,j;
SYS_Init();
GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);
GPIO_SetMode(PH, BIT4, GPIO_MODE_INPUT);
PB14 = 1;
TIMER_Delay(TIMER0, 500);
LCD5110_Init();
BH1750_Init();
BMP085_Init();
Init_BMP085();
LCD_prints(0,0,"temp:");
LCD_prints(0,1,"press:");
LCD_prints(0,2,"light:");
while(1)
{
Get_Sunlight_Value();
bmp085Convert();
LCD_printN(5,0,temperature/10,3);
LCD_printN(5,1,pressure/100,5);
PB14 = 0;
TIMER_Delay(TIMER0, 500000);
PB14 = 1;
TIMER_Delay(TIMER0, 500000);
}
}
|