# include "sys. H"
# include "delay. H"
# include "usart. H"
# include "led. H"
# include "ds18b20. H"
# include "oled. H"
Double temp.
Uint8_t disbuf [16].
Int main (void)
{
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2);
Delay_init (168);
Uart_init (115200);
LED_Init ();
OLED_Init ();
DS18B20_Init ();
While (1)
{
Temp=DS18B20_Get_Temp ();
Temp/=10;
If (temp<20)
{
LED0=0;
}
If (temp> 40)
{
LED1=0;
}
Sprintf ((char *) disbuf, Temp: % 1 f C, Temp);
OLED_ShowString (0, 0, disbuf, 24);
OLED_ShowString (100, 0, "C");
OLED_Refresh_Gram ();
Delay_ms (100);
}
}
Ds18b20. H
# define DS18B20_IO_IN () {GPIOB - & gt; MODER&=~ (3 & lt; <7 * (2)); GPIOB - & gt; MODER |=0 & lt; <7 * 2; }
# define DS18B20_IO_OUT () {GPIOB - & gt; MODER&=~ (3 & lt; <7 * (2)); GPIOB - & gt; MODER |=1 & lt; <7 * 2; }
# define DS18B20_DQ_OUT PBout (7)
# define DS18B20_DQ_IN PBin (7)
U8 DS18B20_Init (void);
Short DS18B20_Get_Temp (void);
Void DS18B20_Start (void);
Void DS18B20_Write_Byte u8 (dat);
U8 DS18B20_Read_Byte (void);
U8 DS18B20_Read_Bit (void);
U8 DS18B20_Check (void);
Void DS18B20_Rst (void);
# endif
Ds18b20. C
# include "ds18b20. H"
# include "delay. H"
U8 DS18B20_Check (void)
{
U8 retry=0;
DS18B20_IO_IN ();//SET PG11 INPUT
While (DS18B20_DQ_IN & amp; & Retry<200)
{
retry++;
Delay_us (1);
};
If (retry>=200) return 1;
The else retry=0;
while (! DS18B20_DQ_IN & amp; & 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=https://bbs.csdn.net/topics/1;
The else data=https://bbs.csdn.net/topics/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& 0 x01;
Dat=dat> 1;
If (testb)
{
DS18B20_DQ_OUT=0;//Write 1
Delay_us (2);
DS18B20_DQ_OUT=1;
Delay_us (60);
}
The 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 (0 XCC);//the skip ROM
X44 DS18B20_Write_Byte (0);//the convert
}
U8 DS18B20_Init (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOB, ENABLE);//e1? UGPIOGe plus or minus? O
//GPIOG9
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure. GPIO_OType=GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init (GPIOB, & amp; 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 (0 XCC);//the skip ROM
The xbe DS18B20_Write_Byte (0);//the convert
TL=DS18B20_Read_Byte ();//LSB
TH=DS18B20_Read_Byte ();//the MSB
If (TH> 7)
{
TH=~ TH;
TL=~ TL;
Temp=0;
} the else temp=1;
Tem=TH;
Tem<=8;
Tem +=TL;
Tem=(double) tem * 0.625;
If (temp) return tem;
The else return - tem;
}
CodePudding user response:
I think is a problem of data types, DS18B20 procedures look no problem, thought transformation may be consumed,CodePudding user response:
Mistake is DS18B20 reading, and read one of the biggest may be out of the pulse duration