本帖最后由 hbzjt2012 于 2018-7-19 13:13 编辑
今天抽时间写了一下基于STM32核心板的OLED光强显示代码,同时用串口打印调试信息,OLED根据实时光照强度调节背光显示亮度。 (1)硬件组成与介绍 硬件包括:STM32F103核心板 OLED12864 搭载BH1750的GY-30光强模块 (2)此DIY方案中BH1750的功能 BH1750用于实时采集周围环境光照强度,主控根据光强阈值和实时值比较进行OLED背光显示调节。 (3)软件流程介绍。
- int main(void)
- {
- /* add your code here ^_^. */
- SystemInit();
- delay_init();
- USART1_Init();
- OLED_Init();
- BH1750_Init();
- LED_Init();
-
- PowerOn();
-
- while(1)
- {
- ++tempCount;
-
- //读取实时光照强度
- lightData = BH1750_ReadLux();
-
- //OLED显示光照强度信息
- OLED_DisString(4u,0u,(uint8_t *)"ROHM BH1750 DIY");
- OLED_DisString(0u,3u,(uint8_t *)"Light:");
- OLED_DisNum(55u,3u,lightData,4,16,LineYes);
- OLED_DisString(100u,3u,(uint8_t *)"lux");
- OLED_DisString(0u,6u,(uint8_t *)"rohm.eefocus.com");
-
- //根据实时光强调节OLED背光强度
- if(lightData > 0 && lightData < 30)
- {
- OLED_WR_Byte(0x81,WRcmd);//--set contrast control register
- OLED_WR_Byte(0xFF,WRcmd); // Set SEG Output Current Brightness
- }else if(lightData > 30 && lightData < 60)
- {
- OLED_WR_Byte(0x81,WRcmd);//--set contrast control register
- OLED_WR_Byte(0x8F,WRcmd); // Set SEG Output Current Brightness
- }else if(lightData > 60 && lightData < 90)
- {
- OLED_WR_Byte(0x81,WRcmd);//--set contrast control register
- OLED_WR_Byte(0x3F,WRcmd); // Set SEG Output Current Brightness
- }else if(lightData > 90 && lightData < 120)
- {
- OLED_WR_Byte(0x81,WRcmd);//--set contrast control register
- OLED_WR_Byte(0x0F,WRcmd); // Set SEG Output Current Brightness
- }
-
- //串口打印调试信息
- if(tempCount == 3)
- {
- tempCount = 0;
- printf("hello world! Light={%6.2f}lux\r\n",lightData);
- LED_Toggle();
- }
- }
- }
复制代码
串口打印调试信息: 实物与演示: 视频播放地址:OLED智能背光调节
|