- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "bh1750.h"
- #include "oled.h"
-
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- delay_init(); //延时函数初始化
- uart_init(115200); //串口初始化为115200
- Light_Init();
- bh_data_send(BHPowOn);
- bh_data_send(BHReset);
- bh_data_send(BHModeH1); //选择的是分辨率是4lx,测量时间是16ms的
- delay_ms(100);
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- // NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 LED_Init(); //LED端口初始化
- // delay_ms(8000);
-
- OLED_Clear();
- OLED_Init(); //初始化OLED
- while(1)
- {
- // OLED_Clear();
- printf("%d \n",bh_data_read());
- OLED_SHOW_BH1750(); //OLED动态显示BH1750光照数值
- }
- }
复制代码
- #include "delay.h"
- #include "bh1750.h"
- #include "oled.h"
- typedef unsigned char BYTE;
- //BYTE BUF[8]; //接收数据缓存区
- //iic接口初始化
- void Single_Write_BH1750(uchar REG_Address)
- {
- IIC_Start(); //起始信号
- IIC_Send_Byte(BHAddWrite); //发送设备地址+写信号
- IIC_Send_Byte(REG_Address); //内部寄存器地址,
- // BH1750_SendByte(REG_data); //内部寄存器数据,
- IIC_Stop(); //发送停止信号
- }
- void Light_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能A端口时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB10,11
- GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11);
- Single_Write_BH1750(0x01);
- }
- /*************************************************************************************/
- void bh_data_send(u8 command)
- {
- do{
- IIC_Start(); //iic起始信号
- IIC_Send_Byte(BHAddWrite); //发送器件地址
- }while(IIC_Wait_Ack()); //等待从机应答
- IIC_Send_Byte(command); //发送指令
- IIC_Wait_Ack(); //等待从机应答
- IIC_Stop(); //iic停止信号
- }
- u16 bh_data_read(void)
- {
- u16 buf;
- IIC_Start(); //iic起始信号
- IIC_Send_Byte(BHAddRead); //发送器件地址+读标志位
- IIC_Wait_Ack(); //等待从机应答
- buf=IIC_Read_Byte(1); //读取数据
- buf=buf<<8; //读取并保存高八位数据
- buf+=0x00ff&IIC_Read_Byte(0); //读取并保存第八位数据
- IIC_Stop(); //发送停止信号
- return buf;
- }
- void OLED_SHOW_BH1750(void)
- {
- OLED_ShowChar(0,0,'L',16);
- OLED_ShowChar(8,0,'U',16);
- OLED_ShowChar(18,0,'X',16);
- OLED_ShowChar(25,0,':',16);
-
- if(bh_data_read() > 65200)
- {
- OLED_ShowChar(32,0,'E',16);
- OLED_ShowChar(40,0,'R',16);
- OLED_ShowChar(48,0,'R',16);
- OLED_ShowChar(56,0,'O',16);
- OLED_ShowChar(64,0,'R',16);
- delay_ms(500);
- OLED_Clear();
- }
- else if(bh_data_read() > 10000 && bh_data_read() < 65200)
- {
- OLED_ShowNum(32,0,bh_data_read() % 100000 / 10000,1,16);
- OLED_ShowNum(40,0,bh_data_read() % 10000 / 1000 ,1,16);
- OLED_ShowNum(48,0,bh_data_read() % 1000 / 100 ,1,16);
- OLED_ShowNum(56,0,bh_data_read() % 100 / 10 ,1,16);
- OLED_ShowNum(64,0,bh_data_read() % 10 ,1,16);
- }
-
- else if(bh_data_read() > 1000 && bh_data_read() < 10000)
- {
- OLED_ShowChar(32,0,' ',16);
- OLED_ShowNum(40,0,bh_data_read() % 10000 / 1000 ,1,16);
- OLED_ShowNum(48,0,bh_data_read() % 1000 / 100 ,1,16);
- OLED_ShowNum(56,0,bh_data_read() % 100 / 10 ,1,16);
- OLED_ShowNum(64,0,bh_data_read() % 10 ,1,16);
- }
- else if(bh_data_read() > 100 && bh_data_read() < 1000)
- {
- OLED_ShowChar(32,0,' ',16);
- OLED_ShowChar(40,0,' ',16);
- OLED_ShowNum(48,0,bh_data_read() % 1000 / 100 ,1,16);
- OLED_ShowNum(56,0,bh_data_read() % 100 / 10 ,1,16);
- OLED_ShowNum(64,0,bh_data_read() % 10 ,1,16);
- }
- else if(bh_data_read() > 10 && bh_data_read() < 100)
- {
- OLED_ShowChar(32,0,' ',16);
- OLED_ShowChar(40,0,' ',16);
- OLED_ShowChar(48,0,' ',16);
- OLED_ShowNum(56,0,bh_data_read() % 100 / 10 ,1,16);
- OLED_ShowNum(64,0,bh_data_read() % 10 ,1,16);
- }
- else if(bh_data_read() < 10)
- {
- OLED_ShowChar(32,0,' ',16);
- OLED_ShowChar(40,0,' ',16);
- OLED_ShowChar(48,0,' ',16);
- OLED_ShowChar(56,0,' ',16);
- OLED_ShowNum(64,0,bh_data_read() % 10 ,1,16);
- }
- // else break;
-
-
- delay_ms(100);
- }
复制代码
Keil代码下载:
STM32_BH1750_OLED动态显示.7z
(192.77 KB, 下载次数: 48)
|