/*********************************************************************
* 文件:D18B20.C *
* 功能:D18B20驱动实现 *
* 日期:20110803 *
* 备注:12M晶振 *
* 有事烧纸(QQ980445301)练手作品 *
**********************************************************************/
#include <D18B20.H>
/*********************************************************************
延时
延时精确值(据UV4汇编所生成src计算):
参数us 延时(不)计调用时间
0 7 10
1 12 15
2 17 20
and so on...
The equivalent polynomial function of this function is: 5*us+10(μs)
**********************************************************************
MOV R7,#050H ;传参 1周期
ACALL _wait ;调用 2周期
_wait: ;函数入口
USING 0
?C0001:
MOV R6,AR7
DEC R7 ;1周期
MOV A,R6
JNZ ?C0001 ;2周期
RET ;函数返回 2周期
**********************************************************************/
void wait(unsigned char idata us) //延时分辨率: 5μs
{
while(us--);
}
/*********************************************************************
返回温度
**********************************************************************/
unsigned int get_Temp()
{
init_D18B20(); //初始化器件
write_Byte(0xCC); //Skip ROM(跳过ROM)
write_Byte(0xBE); //读缓存命令
read_Temp(); //读取温度
return temperature;
}
/*********************************************************************
初始化器件
详见datasheet: Figure 11 Page 18.
**********************************************************************/
void init_D18B20()
{
D18B20 = 1;
_nop_(); //hold on a μs
D18B20 = 0;
wait(95); //delay 485 μs (480~960μs)
D18B20 = 1;
wait(20); //delay 110 μs (18B20 waits 15~60μs)
if(D18B20 == 0) //主机释放总线后60μs 18B20发出应答0
{
flag = 1; //detected
}
else
{
flag = 0; //not detect
}
wait(15); //delay 85 μs
_nop_();
_nop_();
D18B20 = 1;
}
/*********************************************************************
写入单字节
Write Time Slots
A write time slot is initiated when the host pulls the data line from
a high logic level to a low logic level.(*1)
There are two types of write time slots:
Write 1 time slots and Write 0 time slots.
All write time slots must be a minimum of 60 μs in duration with
a minimum of a 1-μs recovery time between individual write cycles.(*2)
The DS18B20 samples the DQ line in a window of 15 μs to 60 μs after
the DQ line falls.(*3)
If the line is high, a Write 1 occurs.
If the line is low, a Write 0 occurs.
For the host to generate a Write 1 time slot,
the data line must be pulled to a logic low level and then released,
allowing the data line to pull up to a high level within 15 μs after
the start of the write time slot.
For the host to generate a Write 0 time slot,
the data line must be pulled to a logic low level and remain
low for 60 μs.
**********************************************************************/
void write_Byte(unsigned char idata wr) //write lsb first.
{
unsigned char idata i;
for(i = 8; i; i--)
{
D18B20 = 0; //下降沿产生写时间隙(*1)
_nop_();
D18B20 = wr & 0x01; //prepare for next bit
wait(4); //delay 30 μs. 18B20在下降沿后15~60μs取样(*3)
D18B20 = 1; //为下一循环的下降沿做准备
wr >>= 1; //next bit.使连续写2位间的间隙大于1us(*2)
}
}
/*********************************************************************
读取单字节
Read Time Slots
The host generates read time slots when data is to be read from
the DS18B20. A read time slot is initiated when the host pulls the
data line from a logic high level to logic low level.(*1)
The data line must remain at a low logic level for a minimum of 1 μs;(*2)
output data from the DS18B20 is valid for 15 μs after the falling
edge of the read time slot.(*3)
The host therefore must stop driving the DQ pin low in order to read
its state 15 μs from the start of the read slot.(*4)
By the end of the read time slot, the DQ pin will pull back high via
the external pullup resistor.
All read time slots must be a minimum of 60 μs in duration
with a minimum of a 1-μs recovery time between individual read slots.
**********************************************************************/
unsigned char read_Byte() //read lsb first.
{
unsigned char idata i, u = 0;
for(i = 8; i; i--)
{
D18B20 = 0; //下降沿产生读时间隙(*1)
u >>= 1; //prepare for next bit.(*2)
D18B20 = 1; //释放总线(开漏输出特性)(*4)
wait(1); //delay 15 μs
if(D18B20 == 1) //下降沿产生后15μs内是18B20输出时间(*3)
{
u |= 0x80; //modify to 1
}
}
return u;
}
/*********************************************************************
读出多个字符(参数j:读取数量)
**********************************************************************/
void read_Bytes(unsigned char idata j)
{
unsigned char idata i;
p = temp_buff; //复位指针
for(i = j; i; i--)
{
*p = read_Byte();
p++; //下一个(肿么又係指针...)
}
}
/*********************************************************************
CRC校验(啥原理?)(cyclic redundancy check)
**********************************************************************/
unsigned char CRC(unsigned char j)
{
unsigned char idata i, crc_data = 0;
for(i = 0; i < j; i++) //查表校验
{
crc_data = crcTable[crc_data ^ temp_buff[i]];
}
return crc_data; //0:正确
}
/*********************************************************************
读取温度
**********************************************************************/
void read_Temp()
{
read_Bytes(9); //读出全部缓存
if(CRC(9) == 0) //校验正确后计算温度值
{
/*************************************************************
The scratchpad is organized as eight bytes of memory.
The first two bytes contain the LSB and the MSB
of the measured temperature information, respectively.
**************************************************************/
temperature = temp_buff[1] * 0x100 + temp_buff[0];
}
}
/*********************************************************************
配置报警限定值和分辨率
**********************************************************************/
void config()
{
init_D18B20(); //初始化器件
write_Byte(0xCC); //Skip ROM(跳过ROM)
write_Byte(0x4E); //Write Scratchpad(写暂存存储器)
write_Byte(0x19); //高�
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
DS1302+D18B20+LCD1602+STC89C52模块化编程.7z (50个子文件)
DS1302+D18B20+LCD1602
D18B20
D18B20.plg 428B
D18B20.C 9KB
D18B20.OBJ 4KB
D18B20.LST 13KB
D18B20.H 5KB
D18B20.LIB 4KB
D18B20.uvopt 54KB
D18B20_uvopt.bak 54KB
D18B20_uvproj.bak 13KB
D18B20.__b 41B
D18B20.uvproj 13KB
LCD1602
LCD1602.LST 9KB
LCD1602.C 6KB
LCD1602_uvproj.bak 13KB
LCD1602.OBJ 3KB
LCD1602_uvopt.bak 54KB
LCD1602.uvproj 13KB
LCD1602.__b 43B
LCD1602.plg 195B
LCD1602.uvopt 54KB
LCD1602.H 5KB
DS1302
DS1302.LIB 3KB
DS1302.LST 7KB
DS1302.__b 41B
DS1302.C 4KB
DS1302_uvproj.bak 13KB
DS1302.uvopt 54KB
DS1302.H 3KB
DS1302.plg 1KB
DS1302_uvopt.bak 54KB
DS1302.OBJ 3KB
DS1302.uvproj 13KB
main
DS1302.LIB 3KB
clock.M51 37KB
STARTUP.OBJ 749B
LCD1602.LIB 3KB
clock_uvopt.bak 55KB
clock.uvopt 56KB
STARTUP.A51 6KB
clock 19KB
clock.uvproj 14KB
main.LST 7KB
clock.hex 7KB
D18B20.LIB 4KB
STARTUP.LST 14KB
clock.plg 190B
main.OBJ 13KB
clock_uvproj.bak 13KB
clock.lnp 109B
main.c 5KB
共 50 条
- 1
cmheia
- 粉丝: 3
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页