jinglixixi 发表于 2020-10-5 20:48

【CH579M-R1】+BH1750光强检测

<p>BH1750是一款数字式光强度检测传感器,它采用I2C接口工作。在使用时,其与MCU的连接关系为:</p>

<p>SDA---PB8</p>

<p>SCL---PB7</p>

<p>定义SDA引脚输入输出功能的语句为:</p>

<p>#define IIC_SDA_IN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GPIOB_ReadPortPin( GPIO_Pin_8 )</p>

<p>#define IIC_INPUT_MODE_SET()&nbsp; &nbsp; &nbsp; GPIOB_ModeCfg( GPIO_Pin_8, GPIO_ModeIN_PU )</p>

<p>#define IIC_OUTPUT_MODE_SET()&nbsp; GPIOB_ModeCfg( GPIO_Pin_8, GPIO_ModeOut_PP_5mA )</p>

<p>&nbsp;</p>

<p>配置引脚输出高低电平的引脚为:</p>

<p>#define SCL_Set()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIOB_SetBits( GPIO_Pin_7 )</p>

<p>#define SCL_Clr()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIOB_ResetBits( GPIO_Pin_7 )</p>

<p>#define SDA_Set()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIOB_SetBits( GPIO_Pin_8 )</p>

<p>#define SDA_Clr()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GPIOB_ResetBits( GPIO_Pin_8 )</p>

<p>&nbsp;</p>

<p>#define IIC_WRITE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp;&nbsp;&nbsp;</p>

<p>#define IIC_READ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp;&nbsp;</p>

<p>#define&nbsp;&nbsp;&nbsp;&nbsp; SlaveAddress&nbsp;&nbsp; 0x46&nbsp;</p>

<p>&nbsp;</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);
        mDelaymS(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_ShowNum(72,2,sd,5,16);
}</code></pre>

<p>采集光强值得主程序为:</p>

<pre>
<code class="language-cpp">int main()
{
        mDelaymS(100);
        OLED_Init();
        OLED_Clear();
        OLED_ShowString(0,0,"CH579M  TEST",16);
        OLED_ShowString(0,2,"BH1750_OLED",16);
        mDelaymS(1000 );
        IIC_BH1750_Init();
       OLED_ShowString(0,2,"Sunlight=",16);
       while(1)
       {
                Get_Sunlight_Value();
                mDelaymS( 500 );
       }
}</code></pre>

<p>经编译、下载,其运行效果如图1和图2所示。</p>

<p></p>

<p>图1 初始界面</p>

<p>&nbsp;</p>

<p></p>

<p>图2 检测界面</p>

<p class="MsoNoSpacing" style="text-align:justify">&nbsp;</p>

okhxyyo 发表于 2020-10-9 16:00

<p><a href="https://bbs.eeworld.com.cn/thread-1140005-1-1.html" target="_blank">沁恒CH579M-R1开发板测评</a></p>

<p>汇总贴:<a href="https://bbs.eeworld.com.cn/thread-1140005-1-1.html">https://bbs.eeworld.com.cn/thread-1140005-1-1.html</a></p>

下一秒123 发表于 2024-4-15 11:54

咋连线的

jinglixixi 发表于 2024-4-17 13:43

下一秒123 发表于 2024-4-15 11:54
咋连线的

<p>BH1750是一款数字式光强度检测传感器,它采用I2C接口工作。在使用时,其与MCU的连接关系为:</p>

<p>SDA---PB8</p>

<p>SCL---PB7</p>
页: [1]
查看完整版本: 【CH579M-R1】+BH1750光强检测