jinglixixi 发表于 2021-9-24 23:02

【AB32VG1开发板测评】MINI型环境状态检测器 (兼结题报告)

本帖最后由 jinglixixi 于 2021-9-24 23:12 编辑

<p style="text-align:justify">环境是我们生存的重要条件,因此对它的了解也是十分必要的。限于时间的原因,这里仅对温度、大气压强及光照强度加以检测,若添加DHT22还可添加对环境湿度的检测。</p>

<p>之所以称之为MINI型主要的原因是因为它所用的显示器件是一款0.91寸的单色OLED屏,并采用I2C接口来工作,其整体构成如图1所示。</p>

<p></p>

<p>图1 整体构成</p>

<p>在整体构成中,检测温度与大气压的是数字式传感器BMP085,检测光照强度的数字式传感器BH1750。由于这2款传感器均以I2C接口工作,故直接采用I/O模拟的方式来驱动。</p>

<p>各器件与AB32VG1的连接关系如下:</p>

<p>1)OLED屏</p>

<p>SCL ---PE.3</p>

<p>SDA---PE.2</p>

<p>2)BMP085</p>

<p>SCL--- PE.0</p>

<p>SDA--- PE.1</p>

<p>3)BH1750</p>

<p>SCL--- PF.0</p>

<p>SDA--- PF.1</p>

<p>1.OLED屏的显示驱动</p>

<p>该显示屏的初始化函数为:</p>

<pre>
<code class="language-cpp">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();
}</code></pre>

<p>清除显示屏的函数为:</p>

<pre>
<code class="language-cpp">void LCD_Clear(u16 Color)
{
    u16 i,j;
    LCD_Address_Set(0,0,LCD_W-1,LCD_H-1);
    for(i=0;i&lt;LCD_W;i++)
    {
            for (j=0;j&lt;LCD_H;j++)
            {
                    LCD_WR_DATA(Color);
            }
    }
}</code></pre>

<p>2.BH1750的驱动</p>

<p>读取并显示光照强度检测值的函数为:</p>

<pre>
<code class="language-cpp">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&lt;3;i++)   dis_data=BUF;
    dis_data=(dis_data &lt;&lt;8)+BUF;
    temp=(float)dis_data/1.2;
    sd=temp;
    OLED_ShowString(0,2,"Sunlight=     lx",16);
    OLED_ShowNum(72,2,sd,5,16);
}</code></pre>

<p></p>

<p>图2&nbsp;运行效果</p>

<p>3. BMP085的驱动</p>

<p>BMP085的初始化函数为:</p>

<pre>
<code class="language-cpp">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);
}</code></pre>

<p>读取温度与气压的函数为:</p>

<pre>
<code class="language-cpp">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 &amp;= 0x0000FFFF;
    return pressure;
}</code></pre>

<p></p>

<p>图3&nbsp;运行效果</p>

<p>&nbsp;</p>

<p>4. 结束语</p>

<p>起初使用RT-Thread Studio编程还有效不习惯,在适应之后才发现它使用起来还是很方便,有些常用的外设通过添加即可辅助完成程序设计这是常规开发工具所达不到的。再次感谢有这样好的机会能同时体验到硬件和软件两方面的新知识。</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

Jacktang 发表于 2021-9-25 10:40

<p>用RT-Thread Studio编程主要习惯,常用就感觉好用</p>

jinglixixi 发表于 2021-9-25 10:49

Jacktang 发表于 2021-9-25 10:40
用RT-Thread Studio编程主要习惯,常用就感觉好用

<p>没错,啥事都要与时俱进呀!</p>
页: [1]
查看完整版本: 【AB32VG1开发板测评】MINI型环境状态检测器 (兼结题报告)