蓝桥杯单片机制造温度报警器

stc15f2k61s2为核心,设计一个温度报警器.

温度高于27度时触发报警,当按键按下报警取消。

报警时:Led灯隔个点亮,蜂鸣器响,继电器吸合。

按键:按键按下所有警报解除。

数码管:数码管显示温度数值

代码如下

P0_device.c模块代码

#include "P0_device.h"
#include <STC15F2K60S2.H>
/**
* @brief p2=0xa0为蜂鸣器和继电器 ,P2=0x80为LED;数码管的为0XE0和0xc0;
  * @param  
  * @retval 
  * @author 
  */
void P0_Device(unsigned char p2,unsigned char p0)
{
P0=p0;	//保持恒定的输入值
P2=p2;//打开锁存器
P2=0;//关闭锁存器
}

P0_device.h

#ifndef __P0_DEVICE_H_
#define __P0_DEVICE_H_

#include <STC15F2K60S2.H>

void P0_Device(unsigned char p2,unsigned char p0);

#endif

smg_system.c


#include "smg_system.h"

//这是共阴极接法,蓝桥杯的为共阳极所以用的时候要取反
unsigned char Nixie[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80,0x39};//显示数字0123456789顺序10为.11为C

void smg_system(unsigned n,unsigned m)
{
static int a;
a=0x01 << n;
P0_Device(0xc0,0);
P0_Device(0xe0,~Nixie[m]);//显示的数字
P0_Device(0xc0,a);	 //显示的位置为1+n
}

smg_system.h

#ifndef __SMG_SYSTEM_H_
#define __SMG_SYSTEM_H_

#include <STC15F2K60S2.H>
#include "P0_device.h"

void smg_system(unsigned n,unsigned m);

#endif

onewire.c 

#include <STC15F2K60S2.H>

#include "onewire.h"

sbit DQ = P1^4;  //单总线接口
//单总线延时函数

void Delay_OneWire(unsigned int t)  
{
	t = t * 12;
	while(t--);
}

//从DS18B20读取一个字节
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
	unsigned char i;
	unsigned char dat;
  
	for(i=0;i<8;i++)
	{
		DQ = 0;
		dat >>= 1;
		DQ = 1;
		if(DQ)
		{
			dat |= 0x80;
		}	    
		Delay_OneWire(5);
	}
	return dat;
}

//DS18B20设备初始化
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}



//需要自己完成读取DS18B20温度的程序
float fRead_Temperature(void)
{
	float temp_return;
	int low,high;
	init_ds18b20();
	Write_DS18B20(0xcc); 		//跳过读取ROM
	Write_DS18B20(0x44); 		//启动温度转换
				        //不加上Delay延时,等待温度转换完成,直接去读取暂存器
	init_ds18b20();
	Write_DS18B20(0xcc); 		//跳过读取ROM
	Write_DS18B20(0xbe); 		//获取暂存器数据
	low = Read_DS18B20();		//温度低8位数据
	high = Read_DS18B20();		//温度高8位数据
	temp_return = (high<<8|low)*0.0625;//传化成10进制数字
	
	return temp_return;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H


float fRead_Temperature(void);

#endif

以下为主体代码

#include <STC15F2K60S2.H>
#include "P0_device.h"
#include "smg_system.h"
#include "onewire.h"
 unsigned long i;
int ms,a=0;
//延时函数
void Delay(void)		//@12.000MHz   500ns
{
	unsigned char i, j;

	i = 4;
	j = 125;
	do
	{
		while (--j);
	} while (--i);
}

void Timer2_Init(void)		//1微秒@12.000MHz
{
	AUXR &= 0xFB;		//定时器时钟12T模式
	T2L = 0xFF;		//设置定时初始值
	T2H = 0xFF;		//设置定时初始值
	AUXR |= 0x10;		//定时器2开始计时
	IE2 |=0x04;
	EA=1;
}
//初始化
void system(void)
{
P0_Device(0xa0,0);
P0_Device(0x80,0xff);
}
//读取温度
void ds18b20(void)
{
if(ms>=10)
{
	ms=0;
	i=(unsigned long )(fRead_Temperature()*10000);
}
}
//显示温度
void smg_show(void)
{
	smg_system(2,i/100000);
	smg_system(3,i/10000%10);
	smg_system(3,10);
	smg_system(4,i/1000%10);
	smg_system(5,i/100%10);
	smg_system(7,11);
}
//温度报警
void temperature_call(void)
{
if((i>270000&&i<500000)&&(a!=3)&&(a!=2))//报警的条件
{
P0_Device(0x80,0xaa);
P0_Device(0xa0,0x50);
}
else if((i>270000)&&(a==2))
{a=3;}
else if((i<270000)&&(a==3))
{a=0;}
}
//按键
void key(void)
{
if(P33==0)//按下S4
{
	Delay();//消抖
	if(P33==0)
	{
		
	P0_Device(0xa0,0);
	P0_Device(0x80,0xff);
		a=2;
	while(P33==0){smg_show();}//显示当时温度
	}
}
else if(a==2)
{
	P0_Device(0xa0,0);
	P0_Device(0x80,0xff);
}

}

void main(void)
{
	system();
	Timer2_Init();
while(1)
{

}
}

void T2_Init(void) interrupt 12  //中断入口
{
	ms++;
	smg_show();
	temperature_call();
	ds18b20();
	key();

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值