使用vivado2020.2,用zybo板,气压温度湿度传感器Grove-Temp&Humi&Barometer Sensor (BME280),实时时钟日历模块Pmod RTCC,128x32像素单色OLED屏Pmod OLED开发一个软件,要求:利用气压温度湿度传感器测量环境,并和实时日历一起显示在128x32像素单色OLED屏上,我不会创建工程,请详细说明并给出详细代码
时间: 2023-12-02 12:04:10 浏览: 59
鸿蒙开发板,OLED显示温湿度
5星 · 资源好评率100%
好的,我会尽力解答您的问题。首先,您需要创建一个 Vivado 2020.2 工程,并将 Zybo 板卡添加到工程中。接下来,您需要添加所需的 Pmod 模块(BME280、Pmod RTCC 和 Pmod OLED)并将它们与 Zybo 板卡连接。
然后,您需要编写一个软件程序来读取 BME280 传感器的数据和 Pmod RTCC 的实时日期和时间,并将它们显示在 Pmod OLED 屏上。以下是一些示例代码,供参考:
```c
#include "xparameters.h"
#include "xgpio.h"
#include "xiic.h"
#include "xil_printf.h"
#include "xil_io.h"
#include "sleep.h"
#define OLED_ADDR 0x3C // OLED屏的I2C地址
#define BME280_ADDR 0x76 // BME280传感器的I2C地址
#define RTCC_ADDR 0x6F // Pmod RTCC的I2C地址
// OLED屏初始化函数
void oled_init(XIic* iic)
{
u8 init_cmds[] = {
0xAE, // display off
0xD5, 0x80, // set display clock divide ratio/oscillator frequency
0xA8, 0x1F, // set multiplex ratio(1 to 32)
0xD3, 0x00, // set display offset
0x40, // set display start line
0x8D, 0x14, // set charge pump enable/disable
0x20, 0x00, // set memory addressing mode (0x00 = horizontal, 0x01 = vertical)
0xA1, // set segment re-map
0xC8, // set com output scan direction
0xDA, 0x12, // set com pins hardware configuration
0x81, 0xCF, // set contrast control
0xD9, 0xF1, // set pre-charge period
0xDB, 0x40, // set vcomh
0xA4, // set entire display on/off
0xA6, // set normal/inverse display
0xAF, // display on
};
XIic_Send(iic, OLED_ADDR, &init_cmds, sizeof(init_cmds), XIIC_STOP);
}
// OLED屏写入命令函数
void oled_write_cmd(XIic* iic, u8 cmd)
{
u8 buf[] = { 0x00, cmd };
XIic_Send(iic, OLED_ADDR, &buf, sizeof(buf), XIIC_STOP);
}
// OLED屏写入数据函数
void oled_write_data(XIic* iic, u8* data, u8 len)
{
u8 buf[len+1];
buf[0] = 0x40;
memcpy(buf+1, data, len);
XIic_Send(iic, OLED_ADDR, &buf, sizeof(buf), XIIC_STOP);
}
// BME280传感器初始化函数
void bme280_init(XIic* iic)
{
u8 ctrl_hum = 0x01; // oversampling x1
u8 ctrl_meas = (0x05 << 5) | (0x05 << 2); // oversampling x5, normal mode
u8 config = (0x05 << 5); // filter coefficient 16, standby time 125ms
u8 cmds[] = {
0xF2, ctrl_hum, // set humidity oversampling
0xF4, ctrl_meas, // set temperature/pressure oversampling and mode
0xF5, config, // set filter coefficient and standby time
};
XIic_Send(iic, BME280_ADDR, &cmds, sizeof(cmds), XIIC_STOP);
}
// BME280传感器读取温度函数
s32 bme280_read_temperature(XIic* iic)
{
u8 buf[3];
u8 cmd = 0xFA; // temperature register address
XIic_Send(iic, BME280_ADDR, &cmd, sizeof(cmd), XIIC_REPEATED_START);
XIic_Recv(iic, BME280_ADDR, buf, sizeof(buf), XIIC_STOP);
s32 adc_T = (buf[0] << 16) | (buf[1] << 8) | buf[2];
s32 var1 = ((((adc_T >> 3) - ((s32)calib_dig_T1 << 1))) * ((s32)calib_dig_T2)) >> 11;
s32 var2 = (((((adc_T >> 4) - ((s32)calib_dig_T1)) * ((adc_T >> 4) - ((s32)calib_dig_T1))) >> 12) * ((s32)calib_dig_T3)) >> 14;
return (var1 + var2) * 5 / 1024 / 100; // temperature in degrees Celsius
}
// BME280传感器读取湿度函数
u32 bme280_read_humidity(XIic* iic)
{
u8 buf[2];
u8 cmd = 0xFD; // humidity register address
XIic_Send(iic, BME280_ADDR, &cmd, sizeof(cmd), XIIC_REPEATED_START);
XIic_Recv(iic, BME280_ADDR, buf, sizeof(buf), XIIC_STOP);
s32 adc_H = (buf[0] << 8) | buf[1];
s32 var1 = (adc_H - ((s32)calib_dig_H1 << 4) - (((s32)calib_dig_H3 * ((s32)calib_dig_T1 - ((s32)calib_dig_T2 * 14 >> 4))) >> 10));
s32 var2 = (((s32)calib_dig_H2) * (((s32)calib_dig_T1 - ((s32)calib_dig_T2 * 14 >> 4)) * var1 >> 12 >> 10 + ((s32)calib_dig_H4 << 20 >> 31)));
s32 var3 = var1 * var2 >> 14;
s32 var4 = ((s32)calib_dig_H5) * (((var3 >> 16) * (var3 >> 16) >> 7) >> 10);
s32 var5 = var3 * ((s32)calib_dig_H6) >> 13;
return (var4 + var5 >> 10) * 10 / 1024; // relative humidity in percent
}
// BME280传感器读取气压函数
u32 bme280_read_pressure(XIic* iic)
{
u8 buf[3];
u8 cmd = 0xF7; // pressure register address
XIic_Send(iic, BME280_ADDR, &cmd, sizeof(cmd), XIIC_REPEATED_START);
XIic_Recv(iic, BME280_ADDR, buf, sizeof(buf), XIIC_STOP);
s32 adc_P = (buf[0] << 16) | (buf[1] << 8) | buf[2];
s32 var1 = (((s32)calib_dig_T1) * ((s32)calib_dig_P6)) >> 7;
s32 var2 = (((s32)calib_dig_T2) * ((s32)adc_T)) >> 15;
s32 var3 = (((s32)var1) + ((s32)var2) + ((s32)calib_dig_P1)) >> 1;
s32 var4 = (((s32)calib_dig_P5) * (((s32)var3) >> 1)) >> 12;
s32 var5 = (((s32)calib_dig_P4) * ((s32)var3)) >> 13;
s32 var6 = (((s32)adc_T) * ((s32)adc_T)) >> 16;
s32 var7 = (((s32)calib_dig_P3) * ((s32)var6)) >> 16;
s32 var8 = (((s32)calib_dig_P2) * ((s32)adc_T)) >> 16;
s32 var9 = (((s32)var7) + ((s32)var8) + ((s32)calib_dig_P9)) >> 1;
s32 var10 = (((s32)var5) + ((s32)var4) + ((s32)var9) + ((s32)calib_dig_P8)) >> 1;
return ((u32)((s32)var10)) * 10 / 1024; // pressure in Pa
}
// Pmod RTCC读取日期和时间函数
void rtcc_read_datetime(XIic* iic, u8* datetime)
{
u8 cmd = 0x00; // start at seconds register
XIic_Send(iic, RTCC_ADDR, &cmd, sizeof(cmd), XIIC_REPEATED_START);
XIic_Recv(iic, RTCC_ADDR, datetime, 7, XIIC_STOP);
}
int main()
{
XIic iic;
u8 datetime[7];
char buf[16];
s32 temperature;
u32 humidity, pressure;
int status;
// 初始化IIC总线
status = XIic_Initialize(&iic, XPAR_XIIC_0_DEVICE_ID);
if (status != XST_SUCCESS) {
xil_printf("IIC initialization failed\n");
return -1;
}
// 初始化OLED屏和BME280传感器
oled_init(&iic);
bme280_init(&iic);
// 循环读取数据并显示
while (1) {
// 读取温度、湿度和气压
temperature = bme280_read_temperature(&iic);
humidity = bme280_read_humidity(&iic);
pressure = bme280_read_pressure(&iic);
// 读取日期和时间
rtcc_read_datetime(&iic, datetime);
// 显示温度、湿度、气压和日期时间
snprintf(buf, sizeof(buf), "Temp: %dC", temperature);
oled_write_data(&iic, buf, strlen(buf));
snprintf(buf, sizeof(buf), "Humidity: %d%%", humidity);
oled_write_data(&iic, buf, strlen(buf));
snprintf(buf, sizeof(buf), "Pressure: %dPa", pressure);
oled_write_data(&iic, buf, strlen(buf));
snprintf(buf, sizeof(buf), "%02X/%02X/%02X %02X:%02X:%02X", datetime[6], datetime[5], datetime[4], datetime[2], datetime[1], datetime[0]);
oled_write_data(&iic, buf, strlen(buf));
// 等待1秒钟
sleep(1);
}
return 0;
}
```
上述代码仅供参考,您需要根据自己的具体情况进行调整。同时,您还需要根据自己的具体需求进行屏幕显示的布局和风格设置。
阅读全文