【平头哥RVB2601创意应用开发】四 从SGP30环境传感器都取eCO2和TVOC
本帖最后由 anni_zzg 于 2022-5-22 21:55 编辑<p><strong>一。硬件电路连接</strong></p>
<p> 环境传感器我直接从淘宝购买的SGP30模块,它所提供的接口如下:</p>
<p> </p>
<p>CH2601开发板的I2C接口:</p>
<p></p>
<p>I2C总线可以连接多个I2C器件,但要保证他们的I2C地址各不相同。所以我接SGP30和SHT3X并接到一起,再连接到CH2601开发板的I2C接口。</p>
<p><strong>二。 SGP30的主要参数。</strong></p>
<p> 1. SGP30的7位地址,从SGP30的手册可以看到为0X58<br />
<br />
</p>
<p> 2. 值的范围,精度及采样频率。<br />
</p>
<p>3. 通信时序<br />
</p>
<p>4. 主要测试命令。</p>
<p></p>
<p>5.注意事项。</p>
<p> <span style="color:#e74c3c;">在发出Measure_air_quality命令后,一定要等10MS以上,让数据准备好再读,</span>不然虽然可以读到数据,但是读到的数据是不准确的,开始读的是400 和0,后边的值就超出了范围,我后来修改了延时后,终于可以搞定了。</p>
<p><strong>三。代码实现:</strong></p>
<pre>
<code>#define SGP30_ADDR 0x58
int SGP30_Init(void)
{
unsigned char write_data,num;
write_data = 0x20; write_data = 0x03;
num = csi_iic_master_send(&master_iic, SGP30_ADDR, write_data, 2, 100);
if (num != 2) {
printf("csi_iic_master_send error\n");
return -1;
}
return 0;
}
static uint8_t sgp30_checksum(const uint8_t* buf, uint32_t len)
{
const uint8_t Polynomial = 0x31;
uint8_t Initialization = 0XFF;
uint8_t i = 0, k = 0;
while(i < len)
{
Initialization ^= buf;
for(k = 0; k < 8; k++)
{
if(Initialization & 0X80)
Initialization = (Initialization << 1) ^ Polynomial;
else
Initialization = (Initialization << 1);
}
}
return Initialization;
}
int SGP30_read_result(void)
{
unsigned int co2,tvoc;
unsigned char write_data,buff,num;
write_data = 0x20; write_data = 0x08;
num = csi_iic_master_send(&master_iic, SGP30_ADDR, write_data, 2, 100);
if (num != 2) {
printf("csi_iic_master_send error\n");
return -1;
}
mdelay(20);
num = csi_iic_master_receive(&master_iic, SGP30_ADDR, buff, 6, 200);
if(num !=6)
{
printf("csi_iic_master_receive error\n");
return -1;
}
if(sgp30_checksum(buff,2)==buff)
{
co2 = (buff<<8)| buff;//co2拼接
tvoc = (buff<<8)| buff;//tvoc拼接
sprintf(oled_buf,"co2=%3dtvoc=%4d",co2,tvoc);
show_string(0, 48, oled_buf);
//printf(strcat(oled_buf,"\n"));
}
}</code></pre>
<p><strong> 四。 视频展示。</strong></p>
<p>a51c6230082f9935290b1b0da6a485f4</p>
<p> </p>
<p> </p>
作品很精彩,感谢分享! <p>貢獻自己微薄之力!</p>
页:
[1]