18b20程序
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit DQ = P2^2; //数据口define interface
sbit duma = P2^6; //数码管段选
sbit wema = P2^7; //数码管位选
uint temp; //温度值 variable of temperature
//不带小数点
unsigned char code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//带小数点
unsigned char code table1[] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
/*************精确延时函数*****************/
void delay(unsigned char i)
{
while(--i);
}
/******************************************
此延时函数针对的是12Mhz的晶振
delay(0):延时518us 误差:518-2*256=6
delay(1):延时7us (原帖写"5us"是错的)
delay(10):延时25us 误差:25-20=5
delay(20):延时45us 误差:45-40=5
delay(100):延时205us 误差:205-200=5
delay(200):延时405us 误差:405-400=5
*******************************************/
/*****************DS18B20******************/
void Init_Ds18b20(void) //DS18B20初始化send reset and initialization command
{
DQ = 1; //DQ复位,不要也可行。
delay(1); //稍做延时
DQ = 0; //单片机拉低总线
delay(250); //精确延时,维持至少480us
DQ = 1; //释放总线,即拉高了总线
delay(100); //此处延时有足够,确保能让DS18B20发出存在脉冲。
}
uchar Read_One_Byte() //读取一个字节的数据read a byte date
//读数据时,数据以字节的最低有效位先从总线移出
{
uchar i = 0;
uchar dat = 0;
for(i=8;i>0;i--)
{
DQ = 0; //将总线拉低,要在1us之后释放总线
//单片机要在此下降沿后的15us内读数据才会有效。
_nop_(); //至少维持了1us,表示读时序开始
dat >>= 1; //让从总线上读到的位数据,依次从高位移动到低位。
DQ = 1; //释放总线,此后DS18B20会控制总线,把数据传输到总线上
delay(1); //延时7us,此处参照推荐的读时序图,尽量把控制器采样时间放到读时序后的15us内的最后部分
if(DQ) //控制器进行采样
{
dat |= 0x80; //若总线为1,即DQ为1,那就把dat的最高位置1;若为0,则不进行处理,保持为0
}
delay(10); //此延时不能少,确保读时序的长度60us。
}
return (dat);
}
void Write_One_Byte(uchar dat)
{
uchar i = 0;
for(i=8;i>0;i--)
{
DQ = 0; //拉低总线
_nop_(); //至少维持了1us,表示写时序(包括写0时序或写1时序)开始
DQ = dat&0x01;
//18B20单线温度检测的应用样例程序 #include<REG52.H> #include<math.h> #include<INTRINS.H> #define uchar unsigned char #define uint unsigned int; /*...
2页 1下载券 18b20 7页 1下载券 基于DS18B20STC89C52单片... 6页 免费 18B20程序 6页 1下载券 18B20 16页 免费18b20程序...
//DS18B20的读写程序,数据脚P2.7 // //温度传感器18B20汇编程序,采用器件默认的12位转化 // //最大转化时间750微秒,显示温度-55到+125度,显示精度 // ...
} } /***/ /* */ /*DS18B20的CRC8校验程序 */ /* */ /***/ /*uchar CRC8() { uchar i,x; uchar crcbuff...
DS18B20源程序_信息与通信_工程科技_专业资料。程序 16.3.1 使用 TX-1C 实验板上的 DS18B20 温度传感器设计温控系统 C 语言源代码 (part4.3 P349) #...
/***/ /* CP51-3学习板测试程序 */ /* 作者:孙士培 */ /* 2009年6月8日 */ /* 邮箱:sunshipei@126.com */ /* QQ: 214972659 281931512 */ /*...
ds18b20详解及程序_专业资料。ds18b20的详细资料和程序最近都在学习和写单片机的程序, 今天有空又模仿 DS18B20 温度测量显示实验写了一个 与 DS18B20 基于单...
51单片机与ds18b20程序_信息与通信_工程科技_专业资料。DS18B20程序#include <reg52.h> #include <intrins.h> #define uchar unsigned char #define uint ...
ds18b20程序详解 4页 免费 DS18B20测温程序完全解读... 10页 免费 DS18B20时序详解 6页 免费 DS18B20读写时序分析 8页 免费 DS18B20详细时序 4页 免费...
热门文档