datasheet
超过460,000+ 应用技术资源下载
null

51 单片机温度传感器

  • 1星
  • 日期: 2015-05-08
  • 大小: 2.99KB
  • 所需积分:1分
  • 下载次数:1
  • favicon收藏
  • rep举报
  • 分享
  • free评论
标签: ds18B20

51单片机 DS18B20 温度传感器

#include"reg52.h" #include"stdio.h" #define uchar unsigned char #define uint unsigned int sbit ds=P2^2; sbit dula=P2^6; sbit wela=P2^7; sbit beep=P2^3; uint temp; float f_temp; uint warn_11=270; uint warn_12=250; uint warn_h1=300; uint warn_h2=320; sbit led0=P1^0; sbit led1=P1^1; sbit led2=P1^2; sbit led3=P1^3; unsigned char code table[]={0x3f,0x60,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } void dsreset(void) { uint i; ds=0; i=103; while(i>0)i--; ds=1; i=4; while(i>0)i--; } bit tempreadbit() { uint i; bit dat; ds=0; i++; ds=1; i++; i++; dat=ds; i=8; while(i>0)i--; return(dat); } uchar tempread(void) { uchar i,j,dat; dat=0; for(i=1;i<=8;i++) { j=tempreadbit(); dat=(j<<7)|(dat>>1); } return(dat); } void tempwritebyte(uchar dat) { uint i; uchar j; bit testb; for(j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1; if(testb) { ds=0; i++; i++; ds=1; i=8; while(i>0)i--; } else { ds=0; i=8; while(i>0) i--; ds=1; i++; i++; } } } void tempchange(void) { dsreset(); delay(1); tempwritebyte(0xcc); tempwritebyte(0x44); } uint get_temp() { uchar a,b; dsreset(); delay(1); tempwritebyte(0xcc); tempwritebyte(0xbe); a=tempread(); b=tempread(); temp=b; temp<<=8; temp=temp|a; f_temp=temp*0.0652; temp=f_temp*10+0.5; f_temp=f_temp+0.05; return temp; } void display(uchar num,uchar dat) { uchar i; dula=0; P0=table[dat]; dula=1; dula=0; wela=0; i=0xff; i=i&(~((0x01)<<(num))); P0=i; wela=1; wela=0; delay(1); } void dis_temp(uint t) { uchar i; i=t/100; display(0,i); i=t%100/10; display(1,i+10); i=t%100%10; display(2,i); } void warn(uint s,uchar led) { uchar i; i=s; beep=0; P1=~(led); while(i--) { dis_temp(get_temp()); } beep=1; P1=0xff; i=s; while(i--) { dis_temp(get_temp()); } } void deal(uint t) { uchar i; if((t>warn_12)&&(t<=warn_11)) { warn(40,0x01); } else if(t<=warn_12) { warn(10,0x30); } else if((t=warn_h1)) { warn(10,0x0c); } else { i=40; while(i--) { dis_temp(get_temp()); } } } void inti_com(void) { TMOD=0x20; PCON=0x00; SCON=0x50; TH1=0xfd; TL1=0xfd; TR1=1; } void comm(char *parr) { do { SBUF=*parr++; while(!TI); TI=0; }while(*parr); } void main() { uchar buff[4],i; dula=0; wela=0; inti_com(); while(1) { tempchange(); for(i=10;i>0;i--) { dis_temp(get_temp()); } deal(temp); sprintf(buff,"%f",f_temp); for(i=10;i>0;i--) { dis_temp(get_temp()); } comm(buff); for(i=10;i>0;i--) { dis_temp(get_temp()); } } }
更多简介内容

评论

下载专区


TI最新应用解决方案

工业电子 汽车电子

$(function(){ var appid = $(".select li a").data("channel"); $(".select li a").click(function(){ var appid = $(this).data("channel"); $('.select dt').html($(this).html()); $('#channel').val(appid); }) })