[ESK32-360 Review] + Sensor Measurement and Output Display
[Copy link]
This post was last edited by jinglixixi on 2020-8-23 09:17
1. I2C bus simulation and BH1750 light intensity detection
BH1750 is a digital light intensity sensor based on the I2C interface. We can use the vacant pins of the SD card interface to simulate the I2C interface to drive the sensor for light intensity detection.
Overall composition
BH1750 connection relationship:
SCL---PC11
SDA---PC12
The statements for outputting high and low levels are:
#define SCL_Set() HT_GPIOC->SRR = GPIO_PIN_11
#define SCL_Clr() HT_GPIOC->RR = GPIO_PIN_11
#define SDA_Set() HT_GPIOC->SRR = GPIO_PIN_12
#define SDA_Clr() HT_GPIOC->RR = GPIO_PIN_12
The function for configuring the GPIO port pins is:
void IIC_Init(void)
{
HT32F_DVB_ClockConfig();
GPIO_PullResistorConfig(HT_GPIOC, GPIO_PIN_12, GPIO_PR_DISABLE);
GPIO_DriveConfig(HT_GPIOC, GPIO_PIN_12, GPIO_DV_8MA);
GPIO_DirectionConfig(HT_GPIOC, GPIO_PIN_12, GPIO_DIR_OUT);
GPIO_PullResistorConfig(HT_GPIOC, GPIO_PIN_11, GPIO_PR_DISABLE);
GPIO_DriveConfig(HT_GPIOC, GPIO_PIN_11, GPIO_DV_8MA);
GPIO_DirectionConfig(HT_GPIOC, GPIO_PIN_11, GPIO_DIR_OUT);
}
The function for reading light intensity is:
void Get_Sunlight_Value()
{
int dis_data=0;
float temp;
char i=0;
unsigned int sd;
Single_Write_BH1750(0x01);
Single_Write_BH1750(0x10);
Delayms(180);
Multiple_Read_BH1750();
for(i=0;i<3;i++)
dis_data=gHelloString[0];
dis_data=(dis_data<<8)+gHelloString[1];
temp=(float)dis_data/1.2;
sd=temp;
gHelloString[0] = sd/10000+'0';
gHelloString[1] = sd% 10000/1000+'0';
gHelloString[2] = sd % 1000/100+'0';
gHelloString[3] = sd % 100/10+'0';
gHelloString[4] = sd % 10+'0';
UxART_TxTest();
}
The main function of light intensity detection is:
int main(void)
{
SYSTICK_ClockSourceConfig(SYSTICK_SRC_STCLK);
SYSTICK_SetReloadValue(SystemCoreClock / 8 / 1000);
SYSTICK_IntConfig(ENABLE);
HT32F_DVB_ClockConfig
UxART_Configuration();
IIC_Init();
while(1)
{
Get_Sunlight_Value();
Delayms(1000);
}
}
After the program is downloaded, its running results are shown in the figure.
Figure 3 BH1750 test results
2. BMP085 temperature and pressure detection
BMP085 is a digital sensor based on I2C interface, which is mainly used to measure temperature, air pressure and altitude. We can drive the sensor by simulating I2C interface to perform measurement.
Overall composition
The connection relationship between BMP085 and MCU is as follows:
SDA---PC12
SCL---PC11
The initialization function of BMP085 is:
void Init_BMP085()
{
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
}
The function for reading temperature and pressure is:
void bmp085Convert()
{
unsigned int ut;
unsigned long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp085ReadTemp();
up = bmp085ReadPressure();
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = ((b5 + 8) >> 4);
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
pressure = p+((x1 + x2 + 3791)>>4);
}
The main procedure of the detection is:
int main(void)
{
SYSTICK_ClockSourceConfig(SYSTICK_SRC_STCLK);
SYSTICK_SetReloadValue(SystemCoreClock / 8 / 1000);
SYSTICK_IntConfig(ENABLE);
UxART_Configuration();
IIC_Init();
Init_BMP085();
while(1)
{
bmp085Convert();
ConvTemperature();
ConvPressure();
ConvAltitude();
Delayms(1000);
}
}
Detection value output display
|