单片机控制的温度传感器C语言程序_第1页
单片机控制的温度传感器C语言程序_第2页
单片机控制的温度传感器C语言程序_第3页
单片机控制的温度传感器C语言程序_第4页
单片机控制的温度传感器C语言程序_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include <at89x52.h>#define DO P3_7 #define SEG P0 /数码管自左至右依次为1234位#define MS2L 0x18 /1ms的延时参数#define MS2H 0xfc#define uchar unsigned char#define uint unsigned intuchar code comm4 = 0x01,0x02,0x04,0x08;uchar code seg10 = 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;uchar code seg_add_dici

2、mal10 = 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10; uchar code seg_dicimal2 =0xc0, 0x92;uchar k = 0;uchar temp_l = 0;uchar temp_h = 0;uchar tempsign = 0;uchar hundreds = 0;uchar tens = 0;uchar ones = 0;uchar low_four = 0;/*延时以ms为单位的t时间*/void Delay(uint t) uint i; while(t-) for(i=0; i<125;

3、i+) /*us级延时,延时时间约42*i*/void Delayus(uchar i) while(-i); /*产生复位脉冲,等待应答信号*/void Resetpaulse() DO = 0; /拉低约600us Delayus(150); Delayus(150); DO = 1;/产生上升沿,延时约1560us Delayus(30); while(DO); /等待应答信号 /*读取数据一位*/bit Readbit() uint i = 0; bit b = 0; DO = 0; /产生读时隙 i+; /维持低电平至少1us DO = 1; /1us以上后拉高 Delayus(2)

4、; /延时8us,DO下降沿15内ds18b20输出的数据才有效 b = DO; /读取数据 Delayus(40); /每个读时隙至少持续60us return(b); /*读取一个字节*/uchar Readbyte() uchar byte_read = 0; uchar i, j; for(i=0; i<8; i+) j = Readbit(); byte_read = (j<<i) | byte_read; /低位读起 return(byte_read); /*写一个字节*/void Writebyte(uchar byte_to_write) uchar i =

5、0; uchar j = 0; bit write_bit = 0; for(j=0; j<8; j+) write_bit = (byte_to_write & 0x01); if(write_bit = 1) /写1 DO = 0; /产生写时隙 Delayus(3); /延时15us DO = 1; /写1 Delayus(40); /延时,写时隙不得低于60us else DO = 0; /产生写时隙 Delayus(50); /延时,保持低约60us120us DO = 1; i+; byte_to_write = byte_to_write >> 1; /

6、*配置ds18b20,9位分辨率 */void Configurate() EA = 0; Resetpaulse(); /发出复位脉冲,每次操作都从复位开始 Delay(1); Writebyte(0xcc); /skip room命令 Writebyte(0x4e); Writebyte(0x7f); Writebyte(0x80); Writebyte(0x1f); EA = 1;/*启动温度转换*/void StartConvert() Resetpaulse(); / 发出复位脉冲,每次操作都从复位开始 Delay(1); EA = 0; Writebyte(0xcc); /skip

7、 room命令 Writebyte(0x44); /启动温度转换命令 EA = 1;/*读取温度值*/void ReadTempreture() EA = 0; Resetpaulse(); / 发出复位脉冲,每次操作都从复位开始 Delay(1); Writebyte(0xcc); /skip room命令 Writebyte(0xbe); /读取暂存器命令 temp_l = Readbyte(); /存储温度低字节值 (整数部分低四位和小数部分) temp_h= Readbyte(); /存储温度高字节值 (其中高五位为符号位) EA = 1;/*数据转换*/void DigitalCon

8、vert() uchar total = 0; tempsign = (temp_h >> 7) & 0x01; /得出符号位 if(tempsign = 0) /正数的处理方法 total = (temp_h << 4)&0xf0) | (temp_l >> 4)&0x0f); /取整数位 low_four = (temp_l>>3) & 0x01; /取小数位,9位分辨率,低字节第3位为小数位,只有0、1两种取值 hundreds = total / 100; /计算百、十、个位 tens = (total%1

9、00)/10; ones = (total%100)%10; else /负数处理 求负数补码规则是,按位取反,得到反码,加1即得补码(符号位不变) tempsign = 1; total = (temp_l >> 4) & 0x0f); /取整数部分低4位 total |= (temp_h << 4) & 0xf0); /整数部分高三位和符号位 low_four = (temp_l >> 3) & 0x01; /取小数位 if(low_four = 0) /这里total位uchar型变量,所以根据最低位来确定整数取补规则 tota

10、l = total + 1; /最低位为0的时候,取反加1后有进位,所以这里total要取反加1 else total = total;/最低位为1的时候,取反加1没有进位,所以total直接取反即可 tens = (total%100)/10; ones = (total%100)%10; /*中断处理*/void Display()interrupt 1 EA = 0; TL0 = MS2L; TH0 = MS2H; DigitalConvert(); /数据转换 if(tempsign = 0)/正数 if(k = 0) if(hundreds = 0) /高位0消隐 P1 = 0; e

11、lse P1 = commk; SEG = seghundreds; else if(k = 1) if(tens = 0 && hundreds = 0) /高位0消隐 P1 = 0; else P1 = commk; SEG = segtens; else if(k = 2)/显示个位,因为个位有小数点,所以又定义了一个数组分开来显示 P1 = commk; SEG = seg_add_dicimalones; /要加上小数点else /显示小数,0或5 P1 = commk; SEG = seg_dicimallow_four; else if(tempsign = 1) /负数 if(k = 0)/显示符号位 P1 = commk; SEG = 0xbf; else if(k = 1) if(tens = 0) P1 = 0; else P1 = commk; SEG = segtens; else if(k = 2)/显示个位,因为各位有小数点,所以分开来显示 P1 = commk; SEG = seg_add_dicimalones; /要加上小数点 else /显示小数,0或5 P1 = commk; SEG = seg_dicimallow_four; k+; if(k = 4) k = 0

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论