[RVB2601 creative application development] + light intensity detection
[Copy link]
With the basic use of GPIO port, in addition to driving the OLED screen, it can also be used to drive the corresponding digital sensor. Here, the light intensity sensor is driven, and the display function of the OLED screen is used to realize light intensity detection. The display effect is shown in Figure 1.
Figure 1 Display effect
Since there are relatively few power pins provided, the pins used by the light intensity sensor BH1750 are shown in Figure 2, and the corresponding correspondence is:
CLK---PA2
DIN---PA5
Figure 2 Pin connection
Compared with the driving of OLED screen, the driving of light intensity sensor is more complicated. It not only involves the pins working in output mode, but sometimes also needs to work in input mode, and the mode switching should be performed in a timely manner.
To this end, the statements for outputting high and low levels to the pins and reading the pin status are defined as:
#define SCL_Clr1() csi_gpio_pin_write(&clk, GPIO_PIN_LOW)
#define SCL_Set1() csi_gpio_pin_write(&clk, GPIO_PIN_HIGH)
#define SDA_Clr1() csi_gpio_pin_write(&din, GPIO_PIN_LOW)
#define SDA_Set1() csi_gpio_pin_write(&din, GPIO_PIN_HIGH)
#define IIC_SDA_IN1 csi_gpio_pin_read(&din)
The corresponding pin working mode configuration function is:
void IIC_INPUT_MODE()
{
csi_gpio_pin_init(&din, PA5);
csi_gpio_pin_dir(&din, GPIO_DIRECTION_INPUT);
}
void IIC_OUTPUT_MODE()
{
csi_gpio_pin_init(&din, PA5);
csi_gpio_pin_dir(&din, GPIO_DIRECTION_OUTPUT);
}
The function of simulating I2C to send byte data using the GPIO port is:
void BH1750_SendByte(char data)
{
char i;
IIC_OUTPUT_MODE();
SCL_Clr1();
udelay(2);
for (i=0;i<8;i++)
{
if(data&0x80) SDA_Set1();
else SDA_Clr1();
data <<= 1;
SCL_Set1();
udelay(2);
SCL_Clr1();
udelay(2);
}
}
The function to read and display the detection value is:
void Get_Sunlight_Value()
{
int dis_data=0;
float temp;
unsigned int sd;
Single_Write_BH1750(0x01);
Single_Write_BH1750(0x10);
udelay(1000 * 180);
Multiple_Read_BH1750();
dis_data=BUF[0];
dis_data=(dis_data<<8)+BUF[1];
temp=(float) dis_data/1.2;
sd=temp;
OLED_ShowNum(20,6,sd,5,16);
}
The main program to achieve the graphic display effect is:
int main(void)
{
csi_error_t ret;
csi_rtc_time_t last_time, base_time, alarm_time;
board_yoc_init();
oled_pinmux_init();
OLED_Init();
OLED_Clear();
OLED_ShowString(20,0,"CH2601",16);
OLED_ShowString(20,2,"OLED & RTC",16);
BH1750_Init();
udelay(1000 * 1000);
OLED_ShowString(20,2,"OLED & BH1750",16);
OLED_ShowString(20,4,"Sunlight=",16);
OLED_ShowString(68,6,"lx",16);
while (1) {
Get_Sunlight_Value();
udelay(1000 * 1000);
}
}
Due to the limited power pins, the display task needs to be handed back to the LCD screen later to free up the power pins for connecting other peripherals.
|