本帖最后由 masterqqq 于 2020-12-11 11:33 编辑
实时测量环境温湿度OLED+DHT11
#include "debug.h"
#include "OLED_I2C.h"
#include "dht11.h"
/* I2C Mode Definition */
#define HOST_MODE 0
#define SLAVE_MODE 1
/* I2C Communication Mode Selection */
#define I2C_MODE HOST_MODE
//#define I2C_MODE SLAVE_MODE
/* Global define */
#define Size 7
#define RXAdderss 0x02
#define TxAdderss 0x02
/* Global Variable */
u8 TxData[Size] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
u8 RxData[Size];
/*******************************************************************************
* Function Name : IIC_Init
* Description : Initializes the IIC peripheral.
* Input : None
* Return : None
*******************************************************************************/
void IIC_Init( u32 bound, u16 address )
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitTSturcture;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE );
GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOB, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOB, &GPIO_InitStructure );
I2C_InitTSturcture.I2C_ClockSpeed = bound;
I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9;
I2C_InitTSturcture.I2C_OwnAddress1 = address;
I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init( I2C1, &I2C_InitTSturcture );
I2C_Cmd( I2C1, ENABLE );
#if (I2C_MODE == HOST_MODE)
I2C_AcknowledgeConfig( I2C1, ENABLE );
#endif
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Return : None
*******************************************************************************/
int main(void)
{
unsigned char i;
u8 a,b=0,k=0,j=0;
u8 ii=0;
extern const unsigned char BMP1[];
Delay_Init();
I2C_Configuration();
OLED_Init();
USART_Printf_Init(460800);
OLED_Fill(0xFF);
//Delay_Ms(500);
OLED_Fill(0x00);
//Delay_Ms(500);
while(1)
{
char w[5],s[5];
u8 buffer[5];
double hum;
double temp;
if (dht11_read_data(buffer) == 0)
{
hum = buffer[0] + buffer[1] / 10.0;
temp = buffer[2] + buffer[3] / 10.0;
}
sprintf(w,"%d ",buffer[2]);
sprintf(s,"%d ",buffer[0]);
// printf("___{\"temperature\": %d, \"humidness\": %d}___\n", w, s);
for(i=0;i<2;i++)
{
OLED_Showint(1+i*16,0,i);
}
OLED_ShowStr(39,0,w,2);
for(i=0;i<2;i++)
{
OLED_Showint(63+i*16,0,i+2);
}
OLED_ShowStr(103,0,s,2);
//OLED_Drawch(0,0,'5',1);
//ssd1306_refresh_gram();
Delay_Ms(1000);
}
}
|