文档详情

ds18b20的温控系统设计(含C程序).doc

发布:2017-12-07约9.99千字共11页下载文档
文本预览下载声明
1.1硬件部分图片 软件部分编程 #includereg51.h #includeintrins.h #define uchar unsigned char sbit led1=P1^0; sbit led2=P1^1; sbit led3=P1^2; sbit led4=P1^3; sbit speker=P1^6; sbit ds = P1^7; int tempValue; int count,temp ; uchar cnt,cnt1,cnt2,cnt3; //0-F数码管的编码(共阳极) Unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80 ,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //======================================= void delay(unsigned int i) //延时函数 { unsigned int j; while(i--) { for(j = 0; j 125; j++); } } //====================================== void to_init() //定时器T0 {TMOD=0x01; TH0=(65536-2000)/256; TL0=(65536-2000)%256; EA=1; TR0=1; ET0=1; } //===================================初始化DS18B20 void dsInit() { unsigned int i; ds = 0; i = 100; //拉低约800us, 符合协议要求的480us以上 while(i0) i--; ds = 1; //产生一个上升沿, 进入等待应答状态 i = 4; while(i0) i--; } void dsWait() { unsigned int i; while(ds); while(~ds); //检测到应答脉冲 i = 4; while(i 0) i--; } //====================================向DS18B20读取一位数据 bit readBit() { unsigned int i; bit b; ds = 0; i++; //延时约8us, 符合协议要求至少保持1us ds = 1; i++; i++; //延时约16us, 符合协议要求的至少延时15us以上 b = ds; i = 8; while(i0) i--; //延时约64us, 符合读时隙不低于60us要求 return b; } //==================================读取一字节数据, 通过调用readBit()来实现 unsigned char readByte() { unsigned int i; unsigned char j, dat; dat = 0; for(i=0; i8; i++) { j = readBit(); //最先读出的是最低位数据 dat = (j 7) | (dat 1); } return dat; } //====================================向DS18B20写入一字节数据 void writeByte(unsigned char dat) { unsigned int i; unsigned char j; bit b; for(j = 0; j 8; j++) { b = dat 0x01; dat = 1; if(b) { ds = 0; i++; i++; //拉低约16us, 符号要求15~60us内 ds = 1; i = 8; while(i0
显示全部
相似文档