资源简介
基于51和DS18B20的温度采集,用到了DS18B20的search rom指令,可以自动搜索序列号。

代码片段和文件信息
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define MAXNUM 4
sbit DS=P2^1;
sbit RS=P3^0;
sbit RW=P3^1;
sbit EN=P3^2;
sbit P34=P3^4;
sbit P35=P3^5;
sbit P36=P3^6;
sbit P37=P3^7;
uchar ID[4][8]={0};
uchar funum=0;
uchar code tab3[16]={“searching rom...“};
uchar code tab4[14]={“search rom OK!“};
uchar code tab5[13]={“The total is:“};
uchar code tab6[10]={“0123456789“};
uchar code tab7[14]={“No( ): . C“};
void delay_ms(uchar i) //延时(j*2+1+2)*i+5 个机器周期
{ uchar j; //12MHz时,延时 0.5*i ms
do{j=248;
do{j--;}while(j);
i--;
}while(i);
}
void delay_2us(uchar i) //延时 2*i+5 us
{
while(--i);
}
void LCD_com(uchar com) //向LCD写指令
{
RS=0;
RW=0;
P1=com;
EN=1;
EN=0;
delay_2us(20);
}
void LCD_data(uchar dat)//向LCD写数据
{
RS=1;
RW=0;
P1=dat;
EN=1;
EN=0;
delay_2us(20);
}
void LCD_init() //初始化LCD
{
LCD_com(0x38); //8位数据总线,显示两行,5*7点阵
LCD_com(0x0c); //显示功能打开,无光标
LCD_com(0x01); //清屏,光标归位,地址计数器AC置0
delay_ms(4); //延时2ms,因为0x01清屏需要1.64ms
}
uchar DS_init(void) //18B20复位,初始化函数
{
uchar presence;
DS=0; delay_2us(250);
DS=1; delay_2us(30);
presence=DS; delay_2us(250);
return (presence); //返回0则初始化成功,否则失败
}
uchar read_byte(void) //读1字节
{
uchar ijdat=0;
for(i=1;i<=8;i++)
{DS=0; _nop_();
DS=1; delay_2us(2);
j=DS; delay_2us(30);
dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里
}
return(dat);
}
uchar read_2bit(void) //读2位
{
uchar i=0j=0;
DS=0; _nop_();
DS=1; delay_2us(2);
j=DS; delay_2us(30);
DS=0; _nop_();
DS=1; delay_2us(2);
i=DS; delay_2us(30);
i=j*2+i;
return(i);
}
void write_byte(uchar dat) //写1字节
{
uchar i;
for(i=0;i<8;i++)
{DS=0;
DS = dat&0x01;
delay_2us(50);
DS = 1;
dat>>=1;
}
}
void write_bit(bit dat) //写1位
{
DS=0;
DS=dat;
delay_2us(50);
DS = 1;
}
void match_rom(uchar j) //匹配ROM
{
char k;
write_byte(0x55); //发送匹配ROM命令
for(k=0;k<8;k++)
write_byte(ID[j][k]); //发送18B20的序列号,先发送低字节
}
uint convert(uchar j) //读取寄存器中存储的温度数据
{
float tt;
uint temp;
uchar ab;
while(DS_init()); //发送指令,使18b20开始转换温度
match_rom(j);
write_byte(0x44);
while(DS_init()); //发送指令,读取温度
match_rom(j);
write_byte(0xbe);
a=read_byte(); //读低8位
b=read_byte(); //读高8位
fu=0;
if(b&0xf8) fu=1; //判断温度正负
temp=b;
temp<<=8; //两个字节组合为1个字
temp=temp|a;
if(fu) temp=-temp;
tt=temp*0.0625; //温度在寄存器中是12位,分辨率是0.0625
temp=tt*100+0.5; //乘100表示小数点后只取1位,加0.5是四舍五入
return temp;
}
uchar search_rom(void)//遍历搜索单线上所连的所有18b20的序列号
{
uchar kl=0chongtuweimna;
uchar zhan[(MAXNUM+1)]={0};
do
{
DS_init();
write_by
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2011-06-05 00:16 第三版\
目录 0 2011-06-15 21:01 第三版\LCD\
文件 151126 2011-06-11 02:35 第三版\LCD\001.DSN
文件 3271 2011-06-15 21:00 第三版\LCD\001.PWI
文件 17535 2011-06-05 01:42 第三版\LCD\111
文件 7362 2011-06-05 01:42 第三版\LCD\111.hex
文件 38 2011-06-05 01:42 第三版\LCD\111.lnp
文件 20891 2011-06-05 01:42 第三版\LCD\111.M51
文件 184 2011-06-15 21:01 第三版\LCD\111.plg
文件 57100 2011-06-15 21:01 第三版\LCD\111.uvopt
文件 13234 2011-05-31 23:28 第三版\LCD\111.uvproj
文件 57100 2011-06-15 19:26 第三版\LCD\111_uvopt.bak
文件 0 2011-05-31 10:00 第三版\LCD\111_uvproj.bak
文件 6518 2011-06-05 01:42 第三版\LCD\222.c
文件 11962 2011-06-05 01:42 第三版\LCD\222.LST
文件 18957 2011-06-05 01:42 第三版\LCD\222.OBJ
文件 19 2011-05-31 19:29 第三版\LCD\ExtDll.iex
文件 145645 2011-06-05 01:43 第三版\LCD\Last Loaded 001.DBK
- 上一篇:仿360强力删除
- 下一篇:AES加密算法Dll
相关资源
- 单片机与温度传感器DS18B20的程序设计
- 毕业论文之温度传感器DS18B20(源码
- stm32 ds18b20 温度传感器 测试通过
- 51单片机读取温度数据存储到SD卡中并
- proteus_24C08—有注释(proteus里面使用的
- STM32F103 DS18B20 V3.5.0固件库驱动程序工
- DS18B20分布式多点测温的Proteus仿真
- DUMPROM工具
- 单片机 DS18B20 温度控制系统
- 课程设计蔬菜大棚自动控制系统,包
- 用PIC16F877实现EEPROM读写程序
- Linux From Scratch 中文手册
- Macromedia Fireworks V8.0 简体中文版
- Synthesis Characterization and in vitro Antitu
- Research on pharmacological effects of lycorin
- chroma 8000 TI编程
- 技嘉Z77P-D3 添加NVME M2硬盘启动.rom
- 千锋elasticsearch视频教程带笔记
-
Macromedia Flash MX Actionsc
ript for Fun an - Eclipse编程技术与附CD-ROM光盘
- The Research on Smart Drill-in Fluid Design
- Axure RP Extension for Chrome0.6.2亲测可用
- Electricity and hydrogen co-production from me
- PLX eeprom
- Promise SuperTrak EX8350产品手册
- Promise SuperTrak EX8300产品手册
- es(elasticsearch)整合SpringCloudSpringBo
-
A web-ba
sed interview platform with geospat - Cyclic Voltammetry and Thermodynamic Propertie
- 西部数据硬盘与Promise科技SATA150相互兼
评论
共有 条评论