空调温度控制程序//*******************ds18b20温度检测***********************************
#include "ds18b20.h"
sbit DQ=P2^1;
void DS18B20_delay(unsigned int i)//延时函数
{
while(i--);
}
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
DS18B20_delay...
//*******************ds18b20温度检测***********************************
#include "ds18b20.h"
sbit DQ=P2^1;
void DS18B20_delay(unsigned int i)//延时函数
{
while(i--);
}
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1; //DQ复位
DS18B20_delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
DS18B20_delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
DS18B20_delay(10);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
DS18B20_delay(5);
}
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
DS18B20_delay(5);
}
return(dat);
}
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
DS18B20_delay(5);
DQ = 1;
dat>>=1;
}
DS18B20_delay(5);
}
void TemperatureConver(void)
{
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
}
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned char b=0;
int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();
b=ReadOneChar();
//b<<=4;
// b+=(a&0xf0)>>4;
//t=b;
t=b;
t=t<<8|a;
tt=t*0.0625;
//tt=tt*10+0.5;
t= tt*10+0.5; //放大10倍输出并四舍五入
return(t);
}
//*******************1602显示***********************************
// 引用外部头文件
#include
#include "reg52.h"
#include
#include "lcd.h"
sbit LCD_EN=P2^7;
sbit LCD_RS=P2^5;
sbit LCD_RW=P2^6;
LCD_BusyCheck(void)
{
LCD_RS = 0;
LCD_RW= 1;
LCD_EN= 1;
_nop_();
_nop_();
_nop_();
_nop_();
if(P0 & 0x80)
{
LCD_EN = 0;
return 1;
}
else
{
LCD_EN=0;
return 0;
}
}
void LCD_WriteCmd(unsigned char val)
{
while (LCD_BusyCheck());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = val;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN = 1;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN = 0;
}
void LCD_WriteData(unsigned char val)
{
while (LCD_BusyCheck());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = val;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN = 1;
_nop_();
_nop_();
_nop_();
_nop_();
LCD_EN = 0;
}
void LCD_ClearScreen(void)
{
LCD_WriteCmd(CMD_CLEAR_SCREEN);
}
void LCD_Init(void)
{
LCD_EN=0;
// LCD_WriteCmd(CMD_CLEAR_SCREEN);
// _nop_();
LCD_WriteCmd(CMD_WORK_MODE);
_nop_();
// LCD_WriteCmd(CMD_HOME);
// _nop_();
LCD_WriteCmd(CMD_INPUT_MODE3);
_nop_();
LCD_WriteCmd(CMD_DISPLAY_MODE1);
_nop_();
LCD_WriteCmd(CMD_DDRAM_ADDR);
}
void LCD_ShowChar(unsigned char line, unsigned char col, unsigned char chr)
{
unsigned char addr;
addr = CMD_DDRAM_ADDR + 0x40*line + col;
LCD_WriteCmd(addr);
LCD_WriteData(chr+'0');
}
void LCD_ShowString(unsigned char line, unsigned char col, unsigned char *str)
{
unsigned char addr;
LCD_WriteCmd(CMD_INPUT_MODE3);
// LCD_WriteCmd(CMD_DDRAM_ADDR);
addr = CMD_DDRAM_ADDR + 0x40*line + col;
LCD_WriteCmd(addr);
while(*str)
{
LCD_WriteData(*str++);
}
}
void LCD_GoXY(unsigned char line, unsigned char col)
{
unsigned char addr;
addr = CMD_DDRAM_ADDR + 0x40*line + col;
LCD_WriteCmd(addr);
}
/************************主程序*******************************************/
#include
#include "ds18b20.h"
#include "lcd.h"
sbit LED_RED=P3^6;
sbit LED_YELLOW=P3^7;
sbit k1 =P1^5;
sbit up=P1^6;
sbit down=P1^7;
char count=0;
int settempture;
int settemp;
int temp1;
signed int tempture=85;
void display();
void waring();
void delay(unsigned char ms);
void init();
void set();
void setdis();
settempture=15;
void main()
{
init();
while(1)
{
TemperatureConver();
tempture=ReadTemperature();
tempture/=10;
display();
waring();
setdis();
set();
}
}
void init()
{
LCD_Init();
TemperatureConver();
tempture=ReadTemperature();
tempture/=10;
while(tempture==85)
{
TemperatureConver();
tempture=ReadTemperature();
tempture/=10;
}
}
void delay(unsigned char ms)
{
unsigned char i;
for(ms;ms>0;ms--)
for(i=110;i>0;i--);
}
void waring()
{
if(tempturesettempture)
{
LED_RED=1;
LED_YELLOW=0;
}
else
{
LED_RED=1;
LED_YELLOW=1;
}
}
void display() //温度显示
{
if(tempture>=0)
{
LCD_ShowString(0,2,"Now: ");
LCD_ShowChar(0,7,tempture/10);
LCD_ShowChar(0,8,tempture%10); }
else
{
temp1=1-tempture,
LCD_ShowString(0,2,"Now:");
LCD_ShowString(0,6,"-");
LCD_ShowChar(0,7,temp1/10);
LCD_ShowChar(0,8,temp1%10); }
}
void setdis() //设定温度显示
{
if(settempture>45)
{
LCD_ShowString(1,2," Error! ");
}
else if(settempture>=0)
{
LCD_ShowString(1,2,"Set: ");
LCD_ShowChar(1,7,settempture/10);
LCD_ShowChar(1,8,settempture%10);
}
else
{
settemp =0-settempture;
if(settemp>10)
{
LCD_ShowString(1,2," Error! ");
}
else{
LCD_ShowString(1,2,"Set:");
LCD_ShowString(1,6,"-");
LCD_ShowChar(1,7,settemp/10);
LCD_ShowChar(1,8,settemp%10);
}
}
}
void set()
{
if(k1==0)
{
delay(10);
if(k1==0)
{
while(!k1);
count++;
if(count>=2)
{
count=0;
}
}
}
switch (count)
{
case 0:
settempture=settempture;
LCD_WriteCmd(0x0c);//光标不显示,光标不闪烁
break;
case 1: //调温
settempture=settempture+0;
LCD_WriteCmd(0x06); /*显示光标移动设置*/
delay(50000);
LCD_WriteCmd(0x0f);//光标显示,光标闪烁
if(up==0)
{
delay(10);
if(up==0)
{
while(!up);
settempture++;
setdis();
}
}
if(down==0)
{
delay(10);
if(down==0)
{
while(!down);
settempture--;
setdis();
}
}
break;
}
}
本文档为【空调温度控制程序】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。