|
|
|
|
#include "ds18b20.h"
|
|
|
|
|
#include "delay.h"
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>ѧϰʹ<EFBFBD>ã<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD>;
|
|
|
|
|
//ALIENTEK<EFBFBD><EFBFBD>ӢSTM32<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//DS18B20<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>@ALIENTEK
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳:www.openedv.com
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:2012/9/12
|
|
|
|
|
//<EFBFBD>汾<EFBFBD><EFBFBD>V1.0
|
|
|
|
|
//<EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؾ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//Copyright(C) <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾ 2009-2019
|
|
|
|
|
//All rights reserved
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//<EFBFBD><EFBFBD>λDS18B20
|
|
|
|
|
void DS18B20_Rst(void)
|
|
|
|
|
{
|
|
|
|
|
DS18B20_IO_OUT(); //SET PG11 OUTPUT
|
|
|
|
|
DS18B20_DQ_OUT=0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>DQ
|
|
|
|
|
delay_us(750); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>750us
|
|
|
|
|
DS18B20_DQ_OUT=1; //DQ=1
|
|
|
|
|
delay_us(15); //15US
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD>ȴ<EFBFBD>DS18B20<EFBFBD>Ļ<EFBFBD>Ӧ
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1:δ<EFBFBD><EFBFBD><EFBFBD>DS18B20<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>DS18B20<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD>λ
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>1/0
|
|
|
|
|
u8 DS18B20_Read_Bit(void)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>DS18B20<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
u8 DS18B20_Read_Byte(void)
|
|
|
|
|
{
|
|
|
|
|
u8 i,j,dat;
|
|
|
|
|
dat=0;
|
|
|
|
|
for (i=1;i<=8;i++)
|
|
|
|
|
{
|
|
|
|
|
j=DS18B20_Read_Bit();
|
|
|
|
|
dat=(j<<7)|(dat>>1);
|
|
|
|
|
}
|
|
|
|
|
return dat;
|
|
|
|
|
}
|
|
|
|
|
//дһ<EFBFBD><EFBFBD><EFBFBD>ֽڵ<EFBFBD>DS18B20
|
|
|
|
|
//dat<EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>ʼ<EFBFBD>¶<EFBFBD>ת<EFBFBD><EFBFBD>
|
|
|
|
|
void DS18B20_Start(void)
|
|
|
|
|
{
|
|
|
|
|
DS18B20_Rst();
|
|
|
|
|
DS18B20_Check();
|
|
|
|
|
DS18B20_Write_Byte(0xcc); // skip rom
|
|
|
|
|
DS18B20_Write_Byte(0x44); // convert
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>DS18B20<EFBFBD><EFBFBD>IO<EFBFBD><EFBFBD> DQ ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>DS<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0:<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
u8 DS18B20_Init(void)
|
|
|
|
|
{
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
|
|
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ʹ<EFBFBD><EFBFBD>PORTG<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PORTG.11 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
|
|
GPIO_SetBits(GPIOA,GPIO_Pin_11); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1
|
|
|
|
|
|
|
|
|
|
DS18B20_Rst();
|
|
|
|
|
|
|
|
|
|
return DS18B20_Check();
|
|
|
|
|
}
|
|
|
|
|
//<EFBFBD><EFBFBD>ds18b20<EFBFBD>õ<EFBFBD><EFBFBD>¶<EFBFBD>ֵ
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD>0.1C
|
|
|
|
|
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>ֵ <EFBFBD><EFBFBD>-550~1250<EFBFBD><EFBFBD>
|
|
|
|
|
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; //<EFBFBD>¶<EFBFBD>Ϊ<EFBFBD><EFBFBD>
|
|
|
|
|
}else temp=1; //<EFBFBD>¶<EFBFBD>Ϊ<EFBFBD><EFBFBD>
|
|
|
|
|
tem=TH; //<EFBFBD><EFBFBD><EFBFBD>ø߰<EFBFBD>λ
|
|
|
|
|
tem<<=8;
|
|
|
|
|
tem+=TL; //<EFBFBD><EFBFBD><EFBFBD>õװ<EFBFBD>λ
|
|
|
|
|
tem=(float)tem*0.625; //ת<EFBFBD><EFBFBD>
|
|
|
|
|
if(temp)return tem; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>ֵ
|
|
|
|
|
else return -tem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|