#include "bh1750.h" #include "sys.h" //写入 void Single_Write_BH1750(unsigned char REG_Address) { MYIIC1_Start(); MYIIC1_Send_Byte(BHAddWrite); MYIIC1_Send_Byte(REG_Address); MYIIC1_Stop(); } //发送数据 void bh_data_send(u8 command) { do{ MYIIC1_Start(); MYIIC1_Send_Byte(BHAddWrite); }while(MYIIC1_Wait_Ack()); MYIIC1_Send_Byte(command); MYIIC1_Wait_Ack(); MYIIC1_Stop(); } //接受数据 u16 bh_data_read(void) { u16 buf; u8 a; u8 b; MYIIC1_Start(); MYIIC1_Send_Byte(BHAddRead); MYIIC1_Wait_Ack(); b=MYIIC1_Read_Byte(1); a=MYIIC1_Read_Byte(0); buf=b*256+a; MYIIC1_Stop(); return buf; } //初始化 void Init_BH1750(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8; GPIO_Init(GPIOA,&GPIO_InitStruct); Single_Write_BH1750(0x01); delay_ms(180); }