【ESK32-360测评】+传感器测量及输出显示
[复制链接]
本帖最后由 jinglixixi 于 2020-8-23 09:17 编辑
1. I2C总线模拟与BH1750光强检测
BH1750是一款基于I2C接口的数字式光强传感器,我们可利用SD卡接口空出的引脚来模拟I2C接口以驱动该传感器,从而进行光强检测。
整体构成
BH1750的连接关系:
SCL --- PC11
SDA --- PC12
输出高低电平的语句为:
#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
GPIO口引脚配置的函数为:
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);
}
读取光强的函数为:
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();
}
光强检测的主函数为:
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);
}
}
经程序下载后,其运行结果如图所示。
图3 BH1750检测结果
2.BMP085气温气压检测
BMP085是一款基于I2C接口的数字式传感器,主要用于测量气温、气压及海拔高度等,我们可以通过模拟I2C接口的方式来驱动该传感器,以进行测量。
整体构成
BMP085与MCU的连接关系如下:
SDA --- PC12
SCL --- PC11
BMP085的初始化函数为:
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);
}
读取气温与气压的函数为:
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);
}
检测的主程序为:
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);
}
}
检测值输出显示
|