我使用STM32F103C8T6开发板加SGP30传感器检测CO2与TVOC的浓度,只实现最简单的数据读取,但是读出来的数据各位全为1,不是有效数据,代码如下:
sgp30.h
#ifndef __SGP30_H
#define __SGP30_H
#include "myiic.h"
void Read_Air_Quality(u16 *co2, u16 *tvoc);
void SGP30_Init(void);
#endif
sgp30.c
#include "sgp30.h"
#include "delay.h"
#include "OLED_IIC.h"
void SGP30_Init(void)
{
IIC_Init();
IIC_Start();
IIC_Send_Byte(0x58);
IIC_Wait_Ack();
IIC_Send_Byte(0x20);
IIC_Wait_Ack();
IIC_Send_Byte(0x03);
IIC_Wait_Ack();
IIC_Stop();
IIC_Start();
IIC_Send_Byte(0x58);
IIC_Wait_Ack();
IIC_Send_Byte(0x20);
IIC_Wait_Ack();
IIC_Send_Byte(0x08);
IIC_Wait_Ack();
IIC_Stop();
}
void Read_Air_Quality(u16 *co2, u16 *tvoc)
{
u8 co2_high = 0;
u8 co2_low = 0;
u8 tvoc_high = 0;
u8 tvoc_low = 0;
delay_ms(15000);
IIC_Start();
IIC_Send_Byte(0x59);
IIC_Wait_Ack();
co2_high = IIC_Read_Byte(0);
co2_low = IIC_Read_Byte(0);
IIC_Read_Byte(0);
tvoc_high = IIC_Read_Byte(0);
tvoc_low = IIC_Read_Byte(0);
IIC_Read_Byte(0);
IIC_Stop();
*co2 = (co2_high<<8)|co2_low;
*tvoc = (tvoc_high<<8)|tvoc_low;
}
main.c
#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
#include <stdio.h>
#include <string.h>
#include "OLED_IIC.h"
#include "dht11.h"
#include "usart.h"
#include "sgp30.h"
/************************************************
STM32F103C8T6
12864_IIC_OLED_STM32驱动
OLED显示驱动模块: SSD1306
SGP30传感器
IIC: SCL, SDA
对应接口: PB0, PB1
SGP30:VCC, SCL, SDA, GND
对应接口: 3V, PB6, PB7, GND
CH340:TXD, RXD, GND
对应接口: PA10, PA9, GND
************************************************/
int main(void)
{
u16 co2;
u16 tvoc;
delay_init(); //延时初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(9600);
SGP30_Init();
OLED_Init(); //初始化OLED
initial_olcd(); //初始化
clear_screen(); //清屏
delay_ms(50000);
delay_ms(50000);
delay_ms(50000);
while(1)
{
Read_Air_Quality(&co2, &tvoc);
USART_SendData(USART1, 1);
delay_ms(12);
USART_SendData(USART1, co2>>8);
delay_ms(12);
USART_SendData(USART1, 2);
delay_ms(12);
USART_SendData(USART1, co2&0x00ff);
delay_ms(12);
USART_SendData(USART1, 3);
delay_ms(12);
USART_SendData(USART1, tvoc>>8);
delay_ms(12);
USART_SendData(USART1, 4);
delay_ms(12);
USART_SendData(USART1, tvoc&0x00ff);
delay_ms(12);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)!=SET);
USART_RX_STA = 0;
display_number_16x8(1,1,co2);
display_number_16x8(3,1,tvoc);
}
}
请问有大神可以指点一下我这个小渣渣吗~
|