蓝桥杯——第五届省赛_简易温度装置

一、使用到的模块

矩阵键盘,数码管,LED,DS18B20,继电器

题目要求

蓝桥杯——第五届省赛_简易温度装置_第1张图片
蓝桥杯——第五届省赛_简易温度装置_第2张图片

二、代码部分

实验平台:CT107D
实验芯片:stc15f2k60s2
实验现象:完成题目要求
代码如下

keydrive.c

#include
#include

#define uchar unsigned char
#define uint unsigned int



unsigned char keymap[4][4]={{'0','1','2',' '},
							{'3','4','5',' '},
							{'6','7','8',' '},
							{'9',set,clr,' '}};

unsigned char keysta[4][4]={{1,1,1,1},
					{1,1,1,1},
					{1,1,1,1},
					{1,1,1,1}};	

void keyscan()
{
	uchar i;
	static uchar keyout = 0;
	static uchar keybuff[4][4]={{0xff,0xff,0xff,0xff},
								{0xff,0xff,0xff,0xff},
								{0xff,0xff,0xff,0xff},
								{0xff,0xff,0xff,0xff}};
	switch(keyout)
	{
		case 0:key_out_1 = 0;key_out_4 = 1;break; 
		case 1:key_out_2 = 0;key_out_1 = 1;break;
		case 2:key_out_3 = 0;key_out_2 = 1;break;
		case 3:key_out_4 = 0;key_out_3 = 1;break;
	}
	
	keybuff[keyout][0]=keybuff[keyout][0]<<1|key_in_1;
	keybuff[keyout][1]=keybuff[keyout][1]<<1|key_in_2;
	keybuff[keyout][2]=keybuff[keyout][2]<<1|key_in_3;
	keybuff[keyout][3]=keybuff[keyout][3]<<1|key_in_4;

	for(i=0;i<4;i++)
	{
		if((keybuff[keyout][i]&0x0f) == 0x00)
		{
			keysta[keyout][i] = 0;
		}
		if((keybuff[keyout][i]&0x0f) == 0x0f)
		{
			 keysta[keyout][i] = 1;
		}
	}

	keyout++;
	keyout &= 0x03;
}

void keydrive()//放在主函数不断的判断按键状态是否改变
{
	uchar i,j;
	static uchar keyback[4][4] = {{1, 1, 1, 1},//备用状态数组     
								  {1, 1, 1, 1},
								  {1, 1, 1, 1},
								  {1, 1, 1, 1}};
	for(i=0;i<4;i++)
		for(j=0;j<4;j++)
		{
			   if(keysta[i][j] != keyback[i][j])
			   {
			   		if(keysta[i][j] != 0)
					{
						keyfun(keymap[i][j]);//获取按键值传递给keyfun
					}
			   } keyback[i][j] = keysta[i][j]; //更新状态,方便下一次判断
		}
}

keydrive.h

#ifndef __KEYDRIVE_H
#define __KEYDRIVE_H 

#define set 0x99
#define clr 0x88

sbit key_out_1 = P3^0;//行扫描
sbit key_out_2 = P3^1;
sbit key_out_3 = P3^2;
sbit key_out_4 = P3^3;

sbit key_in_1 = P4^4;//列扫描
sbit key_in_2 = P4^2;
sbit key_in_3 = P3^5;
sbit key_in_4 = P3^4;

void keyscan(void);
void keyfun(unsigned char key_val);
void keydrive(void);

#endif

onewire.c

#include "stc15f2k60s2.h"

sbit DQ = P1^4;  //单总线接口

//单总线延时函数
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	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;
}

unsigned int duwendu()
{
	 
	 unsigned char high,low;
	 unsigned int temp;
	 //ET0 = 0;
	 init_ds18b20();
	 Write_DS18B20(0xcc);
	 Write_DS18B20(0x44);
	 Delay_OneWire(200);

	 init_ds18b20();
	 Write_DS18B20(0xcc);
	 Write_DS18B20(0xbe);

	 low =	Read_DS18B20();
	 high = Read_DS18B20();
	 temp = high<<8;
	 temp |= low;
	 temp = temp*0.0625;
	 //ET0 = 1;
	 return temp;
}

onewire.h

#ifndef __ONEWIRE_H
#define __ONEWIRE_H

unsigned char rd_temperature(void);  //; ;
unsigned int read_temp();

#endif

main.c

#include
#include
#include

#define uchar unsigned char
#define uint unsigned int

sbit buzz = P0^6;
sbit relay = P0^4;
bit temp_flag = 0;
bit set_flag = 0;
bit waring_flag = 0;
bit light_flag = 0;
bit relay_flag = 0;

uchar code duan[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00,0x40};
uchar disbuff[8];
uchar temp;
uchar temp_range[4];
uchar temp_max = 30,temp_min = 20;
uint light;
uchar mode = 1;

					
void set_fucker()
{
	if(relay_flag == 1)
	{
		P2 = P2&0x1f|0xa0;
		relay = 1;
		buzz = 0;
		P2 = P2&0x1f; 	
	}

	if(relay_flag == 0)
	{
		P2 = P2&0x1f|0xa0;
		relay = 0;
		buzz = 0;
		P2 = P2&0x1f; 	
	}
}

void display()
{
	static uchar index;

	P2 = P2&0x1f|0xe0;
	P0 = ~0x00;
	P2 = P2&0x1f;

	P2 = P2&0x1f|0xc0;
	P0 = 1<<index;
	P2 = P2&0x1f;

	P2 = P2&0x1f|0xe0;
	P0 = ~duan[disbuff[index]];
	P2 = P2&0x1f;

	index++;
	index &= 0x07;
}

void show_num()
{
	if(set_flag == 0)
	{
		disbuff[0] = 17;
		disbuff[1] = mode;
		disbuff[2] = 17;
		disbuff[3] = 16;
		disbuff[4] = 16;
		disbuff[5] = 16;
		disbuff[6] = temp/10;
		disbuff[7] = temp%10;
	}

	else if(set_flag == 1)
	{
		{
		disbuff[0] = 17;
		disbuff[1] = temp_range[0];
		disbuff[2] = temp_range[1];
		disbuff[3] = 16;
		disbuff[4] = 16;
		disbuff[5] = 17;
		disbuff[6] = temp_range[2];
		disbuff[7] = temp_range[3];
		}	
	}		
}

void keyfun(uchar key_val)
{
	static uchar i;
	if(key_val == set)
	{
		 set_flag = ~set_flag;
		 temp_max = temp_range[0]*10+temp_range[1];
		 temp_min = temp_range[2]*10+temp_range[3];

		 if(temp_min>temp_max)
		 {
		 	 waring_flag = 1;
		 }

		 else waring_flag = 0;
	}

	if((set_flag == 1)&&('0'<=key_val)&&(key_val<='9'))
	{
		temp_range[i] = key_val-'0';
		i++;
		i &= 0x03;
	}

	if(key_val == clr)
	{
		for(i=0;i<4;i++)
		  temp_range[i]=0;
		  i = 0;
	}
}

void led_set(uchar led_dat)
{
		P2 = P2&0x1f|0x80;
		P0 = led_dat;
		P2 = P2&0x1f;		
}

void  set_drive()
{
	if(temp<temp_min)
	{
		mode = 0;
		light = 800;
		relay_flag = 0;			
	}

	if(temp>=temp_min&&temp<=temp_max)
	{
		mode = 1;
		light = 400;
		relay_flag = 0;
	}

	if(temp>temp_max)
	{
		mode = 2;
		light = 200;
		relay_flag = 1;	
	}

	if(waring_flag == 1) 
	{
		led_set(0xfd);	
	}

	if((set_flag == 1)&&(waring_flag == 0))
	{
		led_set(0xff);	
	}

	if((set_flag == 0)&&(waring_flag == 0))
	{
		if(light_flag == 0)
		{
		  led_set(0xff);
		}

		if(light_flag == 1)
		{
		  led_set(0xfe);
		}
	}
}

void Timer0Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x20;		//设置定时初值
	TH0 = 0xD1;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0 = 1;
	EA = 1;
}

void time1() interrupt 1
{
	static uchar count;
	static uint light_count;
	display();
	keyscan();
	set_drive();
	set_fucker();
	if(++count == 100)
	{
		temp_flag = 1;
		count = 0;
	}

	if(++light_count >= light)
	{
		light_flag = ~light_flag;
		light_count = 0;
	}
}

void main()
{
	Timer0Init();
	
	while(1)
	{
		show_num();
		keydrive();
		if(temp_flag == 1)
		{
			temp = duwendu();
			temp_flag = 0;
		}			
	}
}							

你可能感兴趣的:(蓝桥杯)