【AB32VG1开发板测评】MINI型环境状态检测器 (兼结题报告)
[复制链接]
本帖最后由 jinglixixi 于 2021-9-24 23:12 编辑
环境是我们生存的重要条件,因此对它的了解也是十分必要的。限于时间的原因,这里仅对温度、大气压强及光照强度加以检测,若添加DHT22还可添加对环境湿度的检测。
之所以称之为MINI型主要的原因是因为它所用的显示器件是一款0.91寸的单色OLED屏,并采用I2C接口来工作,其整体构成如图1所示。
图1 整体构成
在整体构成中,检测温度与大气压的是数字式传感器BMP085,检测光照强度的数字式传感器BH1750。由于这2款传感器均以I2C接口工作,故直接采用I/O模拟的方式来驱动。
各器件与AB32VG1的连接关系如下:
1)OLED屏
SCL ---PE.3
SDA---PE.2
2)BMP085
SCL--- PE.0
SDA--- PE.1
3)BH1750
SCL--- PF.0
SDA--- PF.1
1.OLED屏的显示驱动
该显示屏的初始化函数为:
void Lcd_Init(void)
{
OLED_RST_Clr();
rt_thread_mdelay(200);
OLED_RST_Set();
rt_thread_mdelay(200);
LCD_WR_REG(0x11);
rt_thread_mdelay(100);
LCD_WR_REG(0x21);
LCD_WR_REG(0xB1);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_REG(0xB2);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_REG(0xB3);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x05);
LCD_WR_DATA8(0x3A);
LCD_WR_DATA8(0x3A);
LCD_WR_REG(0xB4);
LCD_WR_DATA8(0x03);
LCD_WR_REG(0xC0);
LCD_WR_DATA8(0x62);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x04);
LCD_WR_REG(0xC1);
LCD_WR_DATA8(0xC0);
LCD_WR_REG(0xC2);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x00);
LCD_WR_REG(0xC3);
LCD_WR_DATA8(0x8D);
LCD_WR_DATA8(0x6A);
LCD_WR_REG(0xC4);
LCD_WR_DATA8(0x8D);
LCD_WR_DATA8(0xEE);
LCD_WR_REG(0xC5);
LCD_WR_DATA8(0x0E);
LCD_WR_REG(0xE0);
LCD_WR_DATA8(0x10);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x07);
LCD_WR_DATA8(0x0A);
LCD_WR_DATA8(0x12);
LCD_WR_DATA8(0x27);
LCD_WR_DATA8(0x37);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x10);
LCD_WR_REG(0xE1);
LCD_WR_DATA8(0x10);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x03);
LCD_WR_DATA8(0x0F);
LCD_WR_DATA8(0x06);
LCD_WR_DATA8(0x02);
LCD_WR_DATA8(0x08);
LCD_WR_DATA8(0x0A);
LCD_WR_DATA8(0x13);
LCD_WR_DATA8(0x26);
LCD_WR_DATA8(0x36);
LCD_WR_DATA8(0x00);
LCD_WR_DATA8(0x0D);
LCD_WR_DATA8(0x0E);
LCD_WR_DATA8(0x10);
LCD_WR_REG(0x3A);
LCD_WR_DATA8(0x05);
LCD_WR_REG(0x36);
LCD_WR_DATA8(0x78);
LCD_WR_REG(0x29);
OLED_BLK_Set();
}
清除显示屏的函数为:
void LCD_Clear(u16 Color)
{
u16 i,j;
LCD_Address_Set(0,0,LCD_W-1,LCD_H-1);
for(i=0;i<LCD_W;i++)
{
for (j=0;j<LCD_H;j++)
{
LCD_WR_DATA(Color);
}
}
}
2.BH1750的驱动
读取并显示光照强度检测值的函数为:
void Get_Sunlight_Value()
{
int dis_data=0;
float temp;
char i=0;
unsigned int sd;
Single_Write_BH1750(0x01);
Single_Write_BH1750(0x10);
rt_thread_mdelay(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;
OLED_ShowString(0,2,"Sunlight= lx",16);
OLED_ShowNum(72,2,sd,5,16);
}
图2 运行效果
3. BMP085的驱动
BMP085的初始化函数为:
void BMP085_Init(void)
{
rt_pin_mode(pin_dat, PIN_MODE_OUTPUT);
rt_pin_mode(pin_clk, PIN_MODE_OUTPUT);
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);
}
读取温度与气压的函数为:
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();
rt_thread_mdelay(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();
rt_thread_mdelay(10);
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
}
图3 运行效果
4. 结束语
起初使用RT-Thread Studio编程还有效不习惯,在适应之后才发现它使用起来还是很方便,有些常用的外设通过添加即可辅助完成程序设计这是常规开发工具所达不到的。再次感谢有这样好的机会能同时体验到硬件和软件两方面的新知识。
|