📄 ds18b20.c
字号:
//clock=11.0592MHz
#include <REG52.H>
#include <string.h>
#include "stdio.h"
typedef unsigned int word;
sbit DQ = P1^0;
void delay (word useconds)
{
for(;useconds>0;useconds--);
}
//////////////////////////////////////////////////////////////////////////////
// OW_RESET - performs a reset on the one-wire bus and
// returns the presence detect. Reset is 480us, so delay
// value is (480-24)/16 = 28.5 - we use 29. Presence checked
// another 70us later, so delay is (70-24)/16 = 2.875 - we use 3.
//
unsigned char ow_reset(void)
{
unsigned char presence;
DQ = 0; //pull DQ line low
delay(29); // leave it low for 480us
DQ = 1; // allow line to return high
delay(3); // wait for presence
presence = DQ; // get presence signal
delay(25); // wait for end of timeslot
return(presence); // presence signal returned
} // 0=presence, 1 = no part
//////////////////////////////////////////////////////////////////////////////
// READ_BIT - reads a bit from the one-wire bus. The delay
// required for a read is 15us, so the DELAY routine won't work.
// We put our own delay function in this routine in the form of a
// for() loop.
//
unsigned char read_bit(void)
{
unsigned char i;
DQ = 0; // pull DQ low to start timeslot
DQ = 1; // then return high
for (i=0; i<3; i++); // delay 15us from start of timeslot
return(DQ); // return value of DQ line
}
//////////////////////////////////////////////////////////////////////////////
// WRITE_BIT - writes a bit to the one-wire bus, passed in bitval.
//
void write_bit(char bitval)
{
DQ = 0; // pull DQ low to start timeslot
if(bitval==1) DQ =1; // return DQ high if write 1
delay(5); // hold value for remainder of timeslot
DQ = 1;
}// Delay provides 16us per loop, plus 24us. Therefore delay(5) = 104us
//////////////////////////////////////////////////////////////////////////////
// READ_BYTE - reads a byte from the one-wire bus.
//
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i=0;i<8;i++)
{
if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then
// shifts it left
delay(6); // wait for rest of timeslot
}
return(value);
}
//////////////////////////////////////////////////////////////////////////////
// WRITE_BYTE - writes a byte to the one-wire bus.
//
void write_byte(char val)
{
unsigned char i;
unsigned char temp;
for (i=0; i<8; i++) // writes byte, one bit at a time
{
temp = val>>i; // shifts val right 'i' spaces
temp &= 0x01; // copy that bit to temp
write_bit(temp); // write bit in temp into
}
delay(5);
}
float Read_Temperature(void)
{
int idata get[10];
unsigned char temp_lsb,temp_msb;
int idata k,temprature_value;
ow_reset();
write_byte(0xCC); //Skip ROM
write_byte(0x44); // Start Conversion
delay(5);
ow_reset();
write_byte(0xCC); // Skip ROM
write_byte(0xBE); // Read Scratch Pad
for (k=0;k<9;k++)
{
get[k]=read_byte();
}
temp_msb =get[1]; // Sign byte + lsbit
temp_lsb =get[0]; // Temp data plus lsb
if (temp_msb < 0x80)
{
temprature_value=(float)(256*temp_msb+temp_lsb)/16;
}
else
{
temp_msb=temp_msb&0x7;
temprature_value=(float)((~(256*temp_msb+temp_lsb)+1)&0x7FF)*(-1)/16;
}
return temprature_value;
}
/////////////////////////////////////////////////////////////////////////////////////
UartSend(char * CmdTmp)
{
char i;
for(i=0;i<strlen(CmdTmp);i++)
{
SBUF=CmdTmp[i]; //发送数据
while(TI==0); //发送数据完毕时,TI会自动置高
TI=0; //发送数据完毕,将TI清零,准备下一次发送
}
}
void uart_Init()
{
PCON= PCON & 0X7F; //SMOD=0;选择波特率为普通模式
TR1=0; //关闭定时器1,然后进行初始化
TMOD=TMOD | 0x20; //定时器1为自动装入方式 模式2、自动再装入8位计数器
ET1 = 0; //禁止T1中断
SCON=0x40; //串行口工作方式1:10位异步收发
TH1=0xFD;
TL1=0xFD;
//设置数据格式
//低位在前
RI=0; //清串口接收中断标志
TI=0; //清串口发送中断标志
ES=0; //禁止串口中断
TR1=1; //启动定时器1工作
}
void main(void)
{
char idata SendBuffer[50]="";
float tmpdata;
uart_Init();
while (1)
{
tmpdata=Read_Temperature();
sprintf(SendBuffer, "%f", tmpdata );
UartSend("Test DS18B20 : Uart will ouput the temprature if it is right!\r\n");
UartSend(SendBuffer);
UartSend("\r\n");
delay(65535);
delay(65535);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -