蓝桥杯-简易温度采集与控制装置

 简易温度采集与控制装置

 

 



/**********
	 
   简易温度采集与控制装置
	 
**********/

#include <stc15f2k60s2.h>
#include <onewire.h>
#define uint  unsigned int
#define uchar unsigned char

sbit A1 = P3^0;
sbit A2 = P3^1;
sbit A3 = P3^2;
sbit A4 = P3^3;

sbit B1 = P4^4;
sbit B2 = P4^2;
sbit B3 = P3^5;
sbit B4 = P3^4;

uint  wendu,count;
uchar yi,er,san,si,wu,liu,qi,ba;
uchar model,set,flag,qujian,num,max,min,max1,max2,min1,min2;
uchar shuma[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
void display();
void set_wendu();
//
void allinit()
{
	P2=0x80;P0=0xff;
	P2=0xa0;P0=0x00;
	P2=0xc0;P0=0xff;
	P2=0xff;P0=0xff;
}
//
void Delay_Ms(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
	  for(y=845;y>0;y--);
}
//
void keyscan()
{
	A1=0;A2=A3=A4=1;
	B1=B2=B3=B4=1;
	if(B1==0)
	{
		flag = 0;
		num++;
		while(B1==0);
	}
	if(B2==0)
	{
		flag = 1;
		num++;
		while(B2==0);
	}
	if(B3==0)
	{
		flag = 2;
		num++;
		while(B3==0);
	}
//	if(B4==0)
//	{
//		flag = 0;
//  	while(B1==0);
//	}
	///
	A2=0;A1=A3=A4=1;
	B1=B2=B3=B4=1;
	if(B1==0)
	{
		flag = 3;
		num++;
		while(B1==0);
	}
	if(B2==0)
	{
		flag = 4;
		num++;
		while(B2==0);
	}
	if(B3==0)
	{
		flag = 5;
		num++;
		while(B3==0);
	}
//	if(B4==0)
//	{
//		flag = 0;
//  	while(B1==0);
//	}
	///
	A3=0;A1=A2=A4=1;
	B1=B2=B3=B4=1;
	if(B1==0)
	{
		flag = 6;
		num++;
		while(B1==0);
	}
	if(B2==0)
	{
		flag = 7;
		num++;
		while(B2==0);
	}
	if(B3==0)
	{
		flag = 8;
		num++;
		while(B3==0);
	}
//	if(B4==0)
//	{
//		flag = 0;
//  	while(B1==0);
//	}
	///
	A4=0;A1=A2=A3=1;
	B1=B2=B3=B4=1;
	if(B1==0)
	{
		flag = 9;
		num++;
		while(B1==0);
	}
	if(B2==0)
	{
		set = 100;
		while(B2==0);
	}
	if(B3==0)
	{
		set = 101;
		while(B3==0);
	}
//	if(B4==0)
//	{
//		flag = 0;
//  	while(B1==0);
//	}
}
//
void Timer0Init(void)
{
	AUXR |= 0x80;
	TMOD &= 0xF0;
	TL0 = 0x00;
	TH0 = 0x28;
	TF0 = 0;
	TR0 = 1;
}
//
void main()
{
	model = 0;
	max = 23;
	min = 20;
	allinit();
	Timer0Init();
	while(1)
	{
		keyscan();
		set_wendu();
		display();
	}
}
//
void exter0() interrupt 1
{
	count++;
}
//
void set_wendu()
{
	if(model==0)
	{
		EA  = 1;
		ET0 = 1;
		
		if(set==100) {model=1;set=200;}
		
		wendu = get_wendu();
		
		if(wendu>max)       //温度区间2
		{
			if(count>=40)     //40 * 5 = 200;
			{
			  P2=0x80;P0=0xff;
				if(count>80) {count=0;}
			}
			else {P2=0x80;P0=0xfe;}
			
			P2=0xa0;P0=0x10;
			yi=10;er=2;san=10;
			si=11;wu=11;liu=11;
			qi=wendu/10;ba=wendu%10;
		}
		else if(wendu<min)   //温度区间0
		{
			if(count>=160)     //160 * 5 = 800;
			{
			  P2=0x80;P0=0xff;
				if(count>320) {count=0;}
			}
			else {P2=0x80;P0=0xfe;}
			
			P2=0xa0;P0=0x00;
			yi=10;er=0;san=10;
			si=11;wu=11;liu=11;
			qi=wendu/10;ba=wendu%10;
		}
		else                       //温度区间1
		{
			P2=0xa0;P0=0x00;
			if(count>=80)     //80 * 5 = 400;
			{
			  P2=0x80;P0=0xff;
				if(count>160) {count=0;}
			}
			else {P2=0x80;P0=0xfe;}
			
			yi=10;er=1;san=10;
			si=11;wu=11;liu=11;
			qi=wendu/10;ba=wendu%10;
		}
	}
	//
	else if(model==1)
	{
		EA  = 0;
		ET0 = 0;
		if(set==100) {model=0;set=200;}
		else if(set==101) {num=0;set=200;}
		
		     if(num==5) {num = 1;}
		else if(num==1) {max1 = flag;}
		else if(num==2) {max2 = flag;}
		else if(num==3) {min1 = flag;}
		else if(num==4) {min2 = flag;}
		
		if(num==0)
		{
			yi=10;er=11;san=11;
			si=11;wu=11;liu=10;
			qi=11;ba=11;
		}
		else if(num==1)
		{
			yi=10;er=max1;san=11;
			si=11;wu=11;liu=10;
			qi=11;ba=11;
		}
		else if(num==2)
		{
			yi=10;er=max1;san=max2;
			si=11;wu=11;liu=10;
			qi=11;ba=11;
		}
		else if(num==3)
		{
			yi=10;er=max1;san=max2;
			si=11;wu=11;liu=10;
			qi=min1;ba=11;
		}
		else if(num==4)
		{
			yi=10;er=max1;san=max1;
			si=11;wu=11;liu=10;
			qi=min1;ba=min2;
		}
		
		max = max1*10+max2;
		min = min1*10+min2;
		if(min>max) {P2=0x80;P0=0xfd;}
		else {P2=0x80;P0=0xff;}
	}
}
//
void display()
{
	P2=0xc0;
	P0=0x01;
	P2=0xff;
	P0=shuma[yi];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x02;
	P2=0xff;
	P0=shuma[er];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x04;
	P2=0xff;
	P0=shuma[san];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x08;
	P2=0xff;
	P0=shuma[si];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x10;
	P2=0xff;
	P0=shuma[wu];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x20;
	P2=0xff;
	P0=shuma[liu];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x40;
	P2=0xff;
	P0=shuma[qi];
	Delay_Ms(1);
	
	P2=0xc0;
	P0=0x80;
	P2=0xff;
	P0=shuma[ba];
	Delay_Ms(1);
}

 

 onewire.h

#ifndef _ONEWIRE_H
#define _ONEWIRE_H

#include "stc15f2k60s2.h"
#include "intrins.h"

#define OW_SKIP_ROM 0xcc
#define DS18B20_CONVERT 0x44
#define DS18B20_READ 0xbe

//IC引脚定义
sbit DQ = P1^4;

//函数声明
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
bit Init_DS18B20(void);
unsigned char Read_DS18B20(void);

unsigned int get_wendu();
#endif

 

 onewire.c

/*
  程序说明: 单总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台
  日    期: 2011-8-9
*/

#include "onewire.h"

//单总线延时函数
void Delay_OneWire(unsigned int t)
{
	unsigned char i;
  while(t--)
		for(i=0;i<8;i++);
}

//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写一个字节
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;
}


//
unsigned int get_wendu()
{
	unsigned int  date;
	unsigned char low,high;
	
	Init_DS18B20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	Init_DS18B20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	low  = Read_DS18B20();
	high = Read_DS18B20();
	
	date = high;
	date <<= 8;
	date = (date | low);
	date = (date * 0.0625);
	
	return date;
}

个人见解,感谢阅读。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值