作业一:编写自动温控程序作业一:编写自动温控程序,实现如下功能当气温高于200?

作业一:编写自动温控程序作业一:编写自动温控程序,实现如下功能当气温高于200?,第1张

下面是一个示例自动控温程序的伪代码,实现了您所述的三种模式控制:

scss

Copy code

// 获取当前气温

temperature = get_current_temperature()

// 判断当前气温所处的模式

if (temperature >200) {

// 开启制冷空调

turn_on_air_conditioning(mode="cool"胡盯老)

} else if (temperature <100) {

// 开启制热空调

turn_on_air_conditioning(mode="heat")

} else {

// 开启送风模式

turn_on_air_conditioning(mode="fan")

}

在该示例代码中,我们首先获取当前的气温,然后根据裤升气温的高低来判断当前所处的模式。如果气温高于200℃,则开启制冷空调,如果气温低于100℃,则开启制热空调,否则开启送风模式。

需要注意的是,该程序仅为伪代码示例,则缺实际编写时需要根据具体的硬件设备和编程语言等因素进行适当的修改和调整。同时,在实际应用中还需要考虑到一些其他因素,如温度变化的速率、空调的功率、温度传感器的精度等,以实现更精确和可靠的自动控温功能。

我在10年做了一个差不多升锋仔的,也是报警控制的,你借鉴一下吧:

软件设计:

有两个文件,DS18B20.c和DS18B20.h,将这两个文件添加到工程里即可。

DS18B20.c:

/******************************************************************

程序名称:DS18B20温度测量、报警系统

简要说明:DS18B20温度计,温度测量范围0~99.9摄氏度

可设置上限报警温度、下限报警温度

即高于上限值或者低于下限值时蜂鸣器报警

默认上限报警温度为32℃、默认下限报警温度为10℃

报警值可设置范围:最低上限报警值等于当前下限报警值

最高下限报警值等于当前上限报警值

将下限报警值调为0时为关闭下限报警功能

******************************************************************/

#include <AT89X52.h>

#include "DS18B20.h"

#define uint unsigned int

#define uchar unsigned char   //宏定义

#define SET  P3_1    //定义调整键

#define DEC  P3_2    //定义减少键

#define ADD  P3_3    //定义增加键

#define BEEP P3_7    //定义蜂鸣器

#define JDQ P3_5

bit shanshuo_st    //闪烁间隔标志

bit beep_st     //蜂基神鸣器间隔标志

sbit DIAN = P2^7        //小数点

uchar x=0      //计数器

signed char m     //温度值全局变量

uchar n      //温度值全局变量

uchar set_st=0     //状态标志

signed char shangxian=70  //上限报警温度,默认值为70

signed char xiaxian=0   //下限报警温度,默认值为0

uchar code  LEDData[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff}

/*****延时子程序*****/

void Delay(uint num)

{

while( --num )

}

void shortdelay()(void)   //误差 0us

{

unsigned char a,b,c

for(c=165c>0c--)

for(b=100b>0b--)

for(a=150a>0a--)

_nop_  //if Keil,require use intrins.h

_nop_  //if Keil,require use intrins.h

}

/*****初始化定时器0*****/

void InitTimer(void)

{

TMOD=0x1

TH0=0x3c

TL0=0xb0     //50ms(晶振12M)

}

/*****定时器0中断服务程序*****/

void timer0(void) interrupt 1

{

TH0=0x3c

TL0=0xb0

x++

}

/*****外部中断0服务程序*****/

void int0(void) interrupt 0

{

EX0=0      //关外部中断0

if(DEC==0&&set_st==1)

{

shangxian--

if(shangxian<xiaxian)shangxian=xiaxian

}

else if(DEC==0&&set_st==2)

{

xiaxian--

if(xiaxian<0)xiaxian=0

}

}

/*****外部中断1服务程序*****/

void int1(void) interrupt 2

{

EX1=0      //关外部中断1

if(ADD==0&&set_st==1)

{

shangxian++

if(shangxian>99)shangxian=99

}

else if(ADD==0&&set_st==2)

{

xiaxian++

if(xiaxian>shangxian)xiaxian=shangxian

}

}

/*****读取温度*****/

void check_wendu(void)

{

uint a,b,c

c=ReadTemperature()-5  //获取温度值并减去DS18B20的温漂误差

a=c/100     //计算得到十位数字

b=c/10-a*10    //计算得到个位数字吵汪

m=c/10      //计算得到整数位

n=c-a*100-b*10    //计算得到小数位

if(m<0){m=0n=0}   //设置温度显示上限

if(m>99){m=99n=9}   //设置温度显示上限

}

/*****显示开机初始化等待画面*****/

Disp_init()

{

P2 = 0xbf      //显示-

P1 = 0xf7

Delay(200)

P1 = 0xfb

Delay(200)

P1 = 0xfd

Delay(200)

P1 = 0xfe

Delay(200)

P1 = 0xff         //关闭显示

}

/*****显示温度子程序*****/

Disp_Temperature()     //显示温度

{

P2 =0xc6      //显示C

P1 = 0xf7

Delay(300)

P2 =LEDData[n]    //显示个位

P1 = 0xfb

Delay(300)

P2 =LEDData[m%10]    //显示十位

DIAN = 0         //显示小数点

P1 = 0xfd

Delay(300)

P2 =LEDData[m/10]    //显示百位

P1 = 0xfe

Delay(300)

P1 = 0xff         //关闭显示

}

/*****显示报警温度子程序*****/

Disp_alarm(uchar baojing)

{

P2 =0xc6      //显示C

P1 = 0xf7

Delay(200)

P2 =LEDData[baojing%10] //显示十位

P1 = 0xfb

Delay(200)

P2 =LEDData[baojing/10] //显示百位

P1 = 0xfd

Delay(200)

if(set_st==1)P2 =0x89

else if(set_st==2)P2 =0xc7 //上限H、下限L标示

P1 = 0xfe

Delay(200)

P1 = 0xff         //关闭显示

}

/*****报警子程序*****/

void Alarm()

{

if(x>=10){beep_st=~beep_stx=0}

if((m>=shangxian&&beep_st==1)||(m<xiaxian&&beep_st==1))BEEP=0

else BEEP=1

if((m>=shangxian)||(m<xiaxian))

{shortdelay()()

JDQ=0}

else JDQ=1

}

/*****主函数*****/

void main(void)

{

uint z

InitTimer()    //初始化定时器

EA=1      //全局中断开关

TR0=1

ET0=1      //开启定时器0

IT0=1

IT1=1

check_wendu()

check_wendu()

for(z=0z<300z++)

{

Disp_init()

}

while(1)

{

if(SET==0)

{

Delay(2000)

do{}while(SET==0)

set_st++x=0shanshuo_st=1

if(set_st>2)set_st=0

}

if(set_st==0)

{

EX0=0    //关闭外部中断0

EX1=0    //关闭外部中断1

check_wendu()

Disp_Temperature()

Alarm()   //报警检测

}

else if(set_st==1)

{

BEEP=1    //关闭蜂鸣器

EX0=1    //开启外部中断0

EX1=1    //开启外部中断1

if(x>=10){shanshuo_st=~shanshuo_stx=0}

if(shanshuo_st) {Disp_alarm(shangxian)}

}

else if(set_st==2)

{

BEEP=1    //关闭蜂鸣器

EX0=1    //开启外部中断0

EX1=1    //开启外部中断1

if(x>=10){shanshuo_st=~shanshuo_stx=0}

if(shanshuo_st) {Disp_alarm(xiaxian)}

}

}

}

/*****END*****/

DS18B20.h:

#include <AT89X52.h>

#define  DQ  P3_6     //定义DS18B20总线I/O

/*****延时子程序*****/

void Delay_DS18B20(int num)

{

while(num--) 

}

/*****初始化DS18B20*****/

void Init_DS18B20(void)

{

unsigned char x=0

DQ = 1         //DQ复位

Delay_DS18B20(8)    //稍做延时

DQ = 0         //单片机将DQ拉低

Delay_DS18B20(80)   //精确延时,大于480us

DQ = 1         //拉高总线

Delay_DS18B20(14)

x = DQ           //稍做延时后,如果x=0则初始化成功,x=1则初始化失败

Delay_DS18B20(20)

}

/*****读一个字节*****/

unsigned char ReadOneChar(void)

{

unsigned char i=0

unsigned char dat = 0

for (i=8i>0i--)

{

DQ = 0     // 给脉冲信号

dat>>=1

DQ = 1     // 给脉冲信号

if(DQ)

dat|=0x80

Delay_DS18B20(4)

}

return(dat)

}

/*****写一个字节*****/

void WriteOneChar(unsigned char dat)

{

unsigned char i=0

for (i=8 i>0 i--)

{

DQ = 0

DQ = dat&0x01

Delay_DS18B20(5)

DQ = 1

dat>>=1

}

}

/*****读取温度*****/

unsigned int ReadTemperature(void)

{

unsigned char a=0

unsigned char b=0

unsigned int t=0

float tt=0

Init_DS18B20()

WriteOneChar(0xCC)  //跳过读序号列号的 *** 作

WriteOneChar(0x44)  //启动温度转换

Init_DS18B20()

WriteOneChar(0xCC)  //跳过读序号列号的 *** 作

WriteOneChar(0xBE)  //读取温度寄存器

a=ReadOneChar()     //读低8位

b=ReadOneChar()    //读高8位

t=b

t<<=8

t=t|a

tt=t*0.0625

t= tt*10+0.5     //放大10倍输出并四舍五入

return(t)

}

/*****END*****/

其中控制部分我用的是5V继电器,可以直接控制你的电机了。

两个电路图都差不多的,只不过我的多了几个调整按键,报警温度可以调的。我的这个程序你完全可以用到你的电路里的


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12361039.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存