我用的是stm32F407VET6板子,接传感器ds18b20和dht11测温湿度,在lcd12864显示数据,lcd上湿度数据可以正常刷新,但温度数据隔很久才刷新一次,而且温度数据延迟很久,给ds18b20加热过四十多秒温度才变化一次,请问有大神帮帮我看一下代码吗?谢谢!
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "dht11.h"
- #include "ds18b20.h"
- __align(4) u8 dtbuf[50]; //字符串缓存数组
- int main(void)
- {
-
- u8 tmp,humi; //dht11温湿度
- short temp; //ds18b20温度
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
- delay_init(168); /
- uart_init(38400); /
- InitDis(); //LCD初始化
-
- DS18B20_Init();
- while(DHT11_Init())
- {
- lcd_DisStr(1,0,"DHT11 Error ");
- delay_ms(200);
- lcd_clear();
- delay_ms(200);
- }
-
- while(1)
- {
- temp = DS18B20_Get_Temp(); //读取ds18b20温度
- DHT11_Read_Data(&tmp,&humi); //读取dht11湿度
-
- //printf("温度: %3.1f C 湿度: %2d %%\r\n",temp,humi); //串口调试用
- lcd_DisStr(1,0," Temp Humi is OK");
- lcd_DisStr(2,0," ");
- sprintf((char *)dtbuf,"温度: %3.1f C ",(double)temp/10); //格式化字符串缓存
- lcd_DisStr(3,0,dtbuf); //LCD显示
- sprintf((char *)dtbuf,"湿度: %2d %% ",humi); //格式化字符串缓存
- lcd_DisStr(4,0,dtbuf); //LCD显示
- }
- }
复制代码 ds18b20.h
- #ifndef __DS18B20_H
- #define __DS18B20_H
- #include "sys.h"
复制代码 ds18b20.c
#include "ds18b20.h"
#include "delay.h"
void DS18B20_Rst(void)
{
DS18B20_IO_OUT(); //SET PG11 OUTPUT
DS18B20_DQ_OUT=0; //à-μíDQ
delay_us(750); //à-μí750us
DS18B20_DQ_OUT=1; //DQ=1
delay_us(15); //15US
}
u8 DS18B20_Check(void)
{
u8 retry=0;
DS18B20_IO_IN();//SET PG11 INPUT
while (DS18B20_DQ_IN&&retry<200)
{
retry++;
delay_us(1);
};
if(retry>=200)return 1;
else retry=0;
while (!DS18B20_DQ_IN&&retry<240)
{
retry++;
delay_us(1);
};
if(retry>=240)return 1;
return 0;
}
u8 DS18B20_Read_Bit(void) // read one bit
{
u8 data;
DS18B20_IO_OUT();//SET PG11 OUTPUT
DS18B20_DQ_OUT=0;
delay_us(2);
DS18B20_DQ_OUT=1;
DS18B20_IO_IN();//SET PG11 INPUT
delay_us(12);
if(DS18B20_DQ_IN)data=1;
else data=0;
delay_us(50);
return data;
}
u8 DS18B20_Read_Byte(void) // read one byte
{
u8 i,j,dat;
dat=0;
for (i=1;i<=8;i++)
{
j=DS18B20_Read_Bit();
dat=(j<<7)|(dat>>1);
}
return dat;
}
void DS18B20_Write_Byte(u8 dat)
{
u8 j;
u8 testb;
DS18B20_IO_OUT();//SET PG11 OUTPUT;
for (j=1;j<=8;j++)
{
testb=dat&0x01;
dat=dat>>1;
if (testb)
{
DS18B20_DQ_OUT=0;// Write 1
delay_us(2);
DS18B20_DQ_OUT=1;
delay_us(60);
}
else
{
DS18B20_DQ_OUT=0;// Write 0
delay_us(60);
DS18B20_DQ_OUT=1;
delay_us(2);
}
}
}
void DS18B20_Start(void)// ds1820 start convert
{
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0x44);// convert
}
u8 DS18B20_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE)
//GPIOC3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP
GPIO_Init(GPIOC, &GPIO_InitStructure)
DS18B20_Rst();
return DS18B20_Check();
}
short DS18B20_Get_Temp(void)
{
u8 temp;
u8 TL,TH;
short tem;
DS18B20_Start (); // ds1820 start convert
DS18B20_Rst();
DS18B20_Check();
DS18B20_Write_Byte(0xcc);// skip rom
DS18B20_Write_Byte(0xbe);// convert
TL=DS18B20_Read_Byte(); // LSB
TH=DS18B20_Read_Byte(); // MSB
if(TH>7)
{
TH=~TH;
TL=~TL;
temp=0;/ 温度为负
}else temp=1;//温度为正
tem=TH; //获取高八位
tem<<=8;
tem+=TL;//低八位
tem=(double)tem*0.625;//转换
if(temp)return tem; //返回温度值
else return -tem;
}
|