https://v.youku.com/v_show/id_XM ... j.8428770.3416059.1
- #include "bme680.h"
- uint8_t bme680_data[20]={0};
- uint8_t aqi_accuracy;
- int16_t temperature,altitude;
- uint16_t humidity,aqi;
- uint32_t pressure,gas;
- void Bme680_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = TXD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = RXD;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
- USART_Init(USART2, &USART_InitStructure);
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);
- USART_Cmd(USART2,ENABLE);
- }
- void Bme680_Init(void)
- {
- USART_SendData(USART2,0xa5);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==Bit_RESET);
- USART_SendData(USART2,0x55);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==Bit_RESET);
- USART_SendData(USART2,0x3f);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==Bit_RESET);
- USART_SendData(USART2,0x39);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==Bit_RESET);
- Delay_ms(100);
-
- USART_SendData(USART2,0xa5);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==Bit_RESET);
- USART_SendData(USART2,0x56);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==Bit_RESET);
- USART_SendData(USART2,0x02);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==Bit_RESET);
- USART_SendData(USART2,0xfd);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==Bit_RESET);
- Delay_ms(100);
- }
- void Bme680_Analysis(void)
- {
- temperature=(bme680_data[4]<<8)|bme680_data[5];
- // temperature=temperature/100; //unit:0.01℃
-
- humidity=(bme680_data[6]<<8)|bme680_data[7];
- // humidity=humidity/100; //unit:0.01%
-
- pressure=(bme680_data[8]<<16)|(bme680_data[9]<<8)|bme680_data[10]; //unit:1Pa
-
- aqi_accuracy=bme680_data[11]>>4;
-
- aqi=((bme680_data[11]&0x0f)<<8)|bme680_data[12];
-
- gas=(bme680_data[13]<<24)|(bme680_data[14]<<16)|(bme680_data[15]<<8)|bme680_data[16];
- //unit:1ohm
-
- altitude=(bme680_data[17]<<8)|bme680_data[18]; //unit:1m
- }
复制代码
全部资料51hei下载地址:
程序.7z
(276.04 KB, 下载次数: 208)
|