📄 18b20.c
字号:
/*****************************************/
/* */
/*****************************************/
#include <reg51.h> // 引用标准库的头文件
#include <absacc.h>
#include <stdio.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
#define skrom 0xcc
#define convt 0x44
#define rdpad 0xbe
uchar playcode[]={0x40,0x4f,0x24,0x06,0x0b,0x12,0x10,0x47,0x00,0x02};
uchar tplsb,tpmsb,temp,j; // 温度值低位、高位字节
sbit DQ = P0^7; // 数据通信线DQ
// DELAY - with an 11.059MHz crystal
// Calling the routine takes about 24μs, and then
// each count takes another 16us//
void delay (int us)
{
int s;
for (s = 0; s < us; s++);
}
unsigned char ow_reset(void)
{
unsigned char presence;
DQ = 0; //pull DQ line low
delay(29); // leave it low for 480μ
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
} // presence = 0, no part = 1
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 15μs from start of timeslot
return(DQ); // return value of DQ line
}
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 16μs per loop, plus 24μs
//Therefore, delay(5) = 104μs
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);
}
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);
}
main()
{
while(1)
{
ow_reset();
write_byte(skrom);
write_byte(convt);
//delay(30000);delay(20000);
for(j=0;j<20;j++)
{ P2=0xf7;
temp=tpmsb/10; //十位显示
P1=playcode[temp];
delay(800);
P2=0xfb; //个位显示
temp=tpmsb%10;
P1=playcode[temp];
delay(800);
P2=0xff;
}
ow_reset();
write_byte(skrom);
write_byte(rdpad);
tplsb=read_byte();
tpmsb=read_byte();
tpmsb=(tpmsb<<4)|(tplsb>>4);
P2=0xf7;
temp=tpmsb/10; //十位显示
P1=playcode[temp];
delay(800);
P2=0xfb; //个位显示
temp=tpmsb%10;
P1=playcode[temp];
delay(800);
P2=0xff;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -