基于STC51单片机的以及DS18B20-恒温控制系统设计程序编写(共7页).doc
精选优质文档-倾情为你奉上基于STC51单片机的以及DS18B20 恒温控制系统设计软件设计:(C语言)#include<reg51.h>#include<stdio.h>#include <math.h>库函数#include <intrins.h>#define uint unsigned int#define uchar unsigned charuint LED_Data1,LED_Data2,LED_Data3,LED_Data4,LED_Data5,LED_Data6,LED_Data7,LED_Data8;#define CH451_DIG0 0x0800 /数码管位0显示#define CH451_DIG1 0x0900 /#define CH451_DIG2 0x0a00 /#define CH451_DIG3 0x0b00 /CH451宏定义#define CH451_DIG4 0x0c00 / #define CH451_DIG5 0x0d00 /#define CH451_DIG6 0x0e00 /#define CH451_DIG7 0x0f00 /数码管位7显示sbit DOUT=P22; /接ch451数据输出sbit LOAD=P23; /串行命令加载,上升延激活sbit DIN =P33; /串行数据输出,接CH451的数据输人位寻址寄存器定义sbit DCLK=P34; /串行数据时钟上升延激活sbit te=P16; /sbit sp=P26;/控制蜂鸣器引脚位定义sbit up=P24;sbit down=P25;int temp;int tt,tl=100,tm=300;uint f;uchar key=0; / 存放键盘中断中读取的键值uint display8=0x0a00,0x0900,0x0b00,0x0800,0x0c00,0x0d00,0x0e00,0x0f00;uchar BCD10=0xbe,0x24,0xea,0xe6,0x74,0xd6,0xde,0xa4,0xfe,0xf6; void delay(uint i) 延时程序while(-i);void speaker(void)uint i,j;for(j=0;j<250;j+)for(i=0;i<2000;i+) sp=0;蜂鸣器发生程序for(i=0;i<3000;i+) sp=1; sp=1; /防止结束时候是低电平void Init_Ds18b20(void) / /DS18B20初始化te=0; /单片机拉低总线 delay(500); te=1; /释放总线,即拉高了总线delay(200); uchar Read_One_Byte() uchar i=0;uchar dat=0;for(i=8;i>0;i-) te=0; _nop_(); _nop_(); 读取字节程序 dat>>=1; te=1; delay(1); if(te) dat|=0x80; delay(60); return (dat);void Write_One_Byte(uchar dat) uchar i=0;for(i=8;i>0;i-)te=0; _nop_(); _nop_(); 写字节程序 te=dat&0x01; delay(60); te=1; dat>>=1; delay(1);uint Get_Tmp() /获取温度float t;uchar a=11,b=0;Init_Ds18b20(); /初始化Write_One_Byte(0xcc); /忽略ROM指令Write_One_Byte(0x44); /温度转换指令Init_Ds18b20(); /初始化Write_One_Byte(0xcc); /忽略ROM指令Write_One_Byte(0xbe); /读读RAM的存储器指令a=Read_One_Byte(); /读取到的第一个字节为温度LSB b=Read_One_Byte(); /读取到的第二个字节为温度MSBtemp=b; /先把高八位有效数据赋于temptemp<<=8; /把以上8位数据从temp低八位移到高八位temp=temp|a; /两字节合成一个整型变量 if(temp>0x800)f=1;temp=(temp)+1;t=temp*0.0625;temp=t*10-0.5;得到真实十进制温度值elsef=0;t=temp*0.0625; temp=t*10+0.5;return(temp);void CH451_Init() / 初始化 DIN=0; DIN=1;void CH451_Write(uint dat) uint i;TR1=0; LOAD=1; for(i=12;i>0;i-) 向CH451写命令(送数据) DCLK=0; if(dat&0x01)=0x01) DIN=1; else DIN=0; DCLK=1; dat=dat>>1; LOAD=0; LOAD=1;TR1=1;void getkey() uchar i; uchar command,keycode; command=0x07; LOAD=0; for(i=0;i<4;i+) DIN=command&1; DCLK=0; 获取键盘 command>>=1; DCLK=1; LOAD=1; keycode=0; for(i=0;i<7;i+) keycode<<=1; keycode|=DOUT; DCLK=0; DCLK=1;获取键盘key=keycode; void tempdis(uint v) uchar A0,A1,A2,A3,fl; A0=v/1000; /千位 A1=v%1000/100; /百位 A2=v%100/10; /十位 A3=v%10; /个位 if(f=1) fl=0x40; else fl=0x00; CH451_Write(display7); CH451_Write(display6+fl); CH451_Write(display5+BCDA0);温度显示 CH451_Write(display4+BCDA1); CH451_Write(display3+BCDA2+1); CH451_Write(display2+BCDA3);void main() /主函数CH451_Init(); CH451_Write(0x201); /CH451功能设定 CH451_Write(0x403); CH451_Write(0x50f);sp=1;while(1)tt=Get_Tmp();if(tt>tm|tt<tl)delay(50);if(tt>tm|tt<tl)speaker();温度高于规定温度启动制冷装置,温度达标后制冷装置关闭if(tt>tm)down=0;else if(tt<tl)up=0; 温度低于规定温度启动加热装置,温度达标后关闭加热装置else up=1;down=1;tempdis(tt);getkey();switch(key)case 0x40:while(key!=0x43&key!=0x48&key!=0x4b&key!=0x4a&key!=0x49)getkey();tempdis(tl);if(key=0x41)delay();if(key=0x41)tl=tl-10;delay(); 键盘扫描判断,上下限温度调节else if(key=0x42)delay();if(key=0x42&&tl<tm)tl=tl+10;delay();break;case 0x43:while(key!=0x40&key!=0x48&key!=0x4b&key!=0x4a&key!=0x49)getkey();tempdis(tm);if(key=0x41)delay();if(key=0x41&&tm>tl)tm=tm-10;delay();else if(key=0x42)delay(); 键盘扫描判断,上下限温度调节if(key=0x42)tm=tm+10;delay();break;case 0x48:break;专心-专注-专业