第13届蓝桥杯单片机省赛代码展示

13届代码相对于14届相对简单许多,模块也比较少。

但用键盘切换每个界面仍然是重要考点,需要熟练应用。

本人主体代码如下,欢迎大家交流和学习。

#include "reg52.h"
#include "ds1302.h"
#include "onewire.h"

unsigned char Write_DS1302[] = {0x80,0x82,0x84,0x86,0x88,0x8A,0x8C};
unsigned char Read_DS1302[] = {0x81,0x83,0x85,0x87,0x89,0x8B,0x8D};

//星期在倒数第二个
unsigned char Time[]= {0x01,0x59,0x17,0x07,0x04,0x07,0x24};

unsigned int temp;//实际温度*10
unsigned char st_temp = 23;//温度参数

unsigned char state;//切换按键状态
unsigned char led_state = 0xFF;//led状态
unsigned char relay_state;
unsigned char control_mode;
unsigned int t_count;
unsigned char led_count;

bit temp_relay_flag;//整点继电器标志位

sbit C1 = P3^5;
sbit C2 = P3^4;

sbit R1 = P3^2;
sbit R2 = P3^3;

unsigned char code SMG_duanma[] = {
	0xc0,//0
	0xf9,//1
	0xa4,//2
	0xb0,//3
	0x99,//4
	0x92,//5
	0x82,//6
	0xf8,//7
	0x80,//8
	0x90,//9
	0x88,//A
	0x83,//B
	0xc6,//C
	0xa1,//D
	0x86,//E
	0x8e,//F
	0xbf,//
	0x7f//
};

unsigned char code SMG_duanma_Dot[] = {
	0x40,//0
	0x79,//1
	0x24,//2
	0x30,//3
	0x19,//4
	0x12,//5
	0x02,//6
	0x78,//7
	0x00,//8
	0x10,//9
};

void DisPlay_SMG();

void Select_HC573(unsigned char channel , dat)
{
	P0 = dat;
	switch(channel)
	{
		case 4:
			P2 = (P2 & 0x1F) | 0x80;
			break;
		case 5:
			P2 = (P2 & 0x1F) | 0xA0;
			break;
		case 6:
			P2 = (P2 & 0x1F) | 0xC0;
			break;
		case 7:
			P2 = (P2 & 0x1F) | 0xE0;
			break;
		
		case 0:
			P2 = (P2 & 0x1F) | 0x00;
			break;
	}
	P2 = (P2 & 0x1F) | 0x00;
}



//==========时间==============//
void DS1302_Config()
{
	char i;
	Write_Ds1302(0x8E , 0x00);
	for(i=0;i<7;i++)
	{
		 Write_Ds1302(Write_DS1302[i],Time[i]);
	}
	Write_Ds1302(0x8E , 0x80);//打开写保护
}

void Read_DS1302_Timer()
{
	char i;
	for(i=0;i<7;i++)
	{
		Time[i] = Read_Ds1302_Byte(Read_DS1302[i]);
	}
	
	if(Time[0] == 0 && Time[1] == 0)
	{
		temp_relay_flag = 1;
	}
}
//===========================//


//==========温度=============//
void Delay(unsigned int t)
{
	while(t--);
}

void Read_DS18B20_temp()
{
	unsigned char LSB,MSB;
	
	init_ds18b20();
	Write_DS18B20(0xCC);//跳过ROM指令
	Write_DS18B20(0x44);//温度转化
	
	Delay(1000);
	
	init_ds18b20();
	Write_DS18B20(0xCC);//跳过ROM指令
	Write_DS18B20(0xBE);//温度转化

	LSB = Read_DS18B20();
	MSB = Read_DS18B20(); 
	
	temp = MSB;
	temp <<= 8;
	temp |= LSB;
	
	temp >>= 4;
	temp = temp * 10;
	temp = temp + (LSB & 0x0F) * 0.625;
}
//=========================//
void Delay_SMG(unsigned int t)
{
	while(t--);
}

void SMG_Bit(unsigned char pos , value)
{
	Select_HC573(6 , 0xFF);
	Select_HC573(7 , 0xFF);
	
	Select_HC573(6 , 0x01 << pos);
	Select_HC573(7 , value);
}

void DisPlay_Show_Temp()
{
	SMG_Bit(0,0xC1);
	Delay_SMG(500);
	SMG_Bit(1,SMG_duanma[1]);
	Delay_SMG(500);
	
	SMG_Bit(2,0xFF);
	Delay_SMG(500);
	SMG_Bit(3,0xFF);
	Delay_SMG(500);
	SMG_Bit(4,0xFF);
	Delay_SMG(500);
	
	SMG_Bit(5,SMG_duanma[temp / 100]);
	Delay_SMG(500);
	SMG_Bit(6,SMG_duanma_Dot[temp / 10 % 10]);
	Delay_SMG(500);
	SMG_Bit(7,SMG_duanma[temp % 10]);
	Delay_SMG(500);
	
	SMG_Bit(7,0xFF);
	Delay_SMG(500);
}

void DisPlay_Show_Time()
{
	SMG_Bit(0,0xC1);
	Delay_SMG(500);
	SMG_Bit(1,SMG_duanma[2]);
	Delay_SMG(500);
	
	SMG_Bit(2,0xFF);
	Delay_SMG(500);
	
	SMG_Bit(3,SMG_duanma[Time[2] / 16]);
	Delay_SMG(500);
	SMG_Bit(4,SMG_duanma[Time[2] % 16]);
	Delay_SMG(500);
	
	SMG_Bit(5,SMG_duanma[16]);
	Delay_SMG(500);
	
	SMG_Bit(6,SMG_duanma[Time[1] / 16]);
	Delay_SMG(500);
	SMG_Bit(7,SMG_duanma[Time[1] % 16]);
	Delay_SMG(500);
	
	SMG_Bit(7,0xFF);
	Delay_SMG(500);
}

void DisPlay_Show_Time_Second()
{
	SMG_Bit(0,0xC1);
	Delay_SMG(500);
	SMG_Bit(1,SMG_duanma[2]);
	Delay_SMG(500);
	
	SMG_Bit(2,0xFF);
	Delay_SMG(500);
	
	SMG_Bit(3,SMG_duanma[Time[1] / 16]);
	Delay_SMG(500);
	SMG_Bit(4,SMG_duanma[Time[1] % 16]);
	Delay_SMG(500);
	
	SMG_Bit(5,SMG_duanma[16]);
	Delay_SMG(500);
	
	SMG_Bit(6,SMG_duanma[Time[0] / 16]);
	Delay_SMG(500);
	SMG_Bit(7,SMG_duanma[Time[0] % 16]);
	Delay_SMG(500);
	
	SMG_Bit(7,0xFF);
	Delay_SMG(500);
}


void DisPlay_Set_Temp()
{
	SMG_Bit(0,0xC1);
	Delay_SMG(500);
	SMG_Bit(1,SMG_duanma[3]);
	Delay_SMG(500);
	
	SMG_Bit(2,0xFF);
	Delay_SMG(500);
	SMG_Bit(3,0xFF);
	Delay_SMG(500);
	SMG_Bit(4,0xFF);
	Delay_SMG(500);
	SMG_Bit(5,0xFF);
	Delay_SMG(500);
	
	SMG_Bit(6,SMG_duanma[st_temp / 10]);
	Delay_SMG(500);
	SMG_Bit(7,SMG_duanma[st_temp % 10]);
	Delay_SMG(500);
	
	SMG_Bit(7,0xFF);
	Delay_SMG(500);
}

//===========矩阵键盘===========//
void Scan_Key()
{
	C1 = 0;
	C2 = 1;
	R1 = R2 = 1;
	if(R1 == 0)//S13
	{
		Delay(50);
		if(R1 == 0)
		{
			if(control_mode == 0) //温度控制模式
			{
				control_mode = 1;
			}
			else if(control_mode == 1) //时间控制模式
			{
				control_mode = 0;
			}
			while(R1 == 0)
			{
				DisPlay_SMG();
			}
		}
	}
	else if(R2 == 0) //S12
	{
		Delay(50);
		if(R2 == 0)
		{
			if(state == 0)
			{
				state = 1;
			}
			else if(state == 1)
			{
				state = 2;
			}
			else if(state == 2)
			{
				state = 0;
			}
			while(R2 == 0)
			{
				DisPlay_SMG();
			}
		}
	}
	
	C1 = 1;
	C2 = 0;
	R1 = R2 = 1;
	if(R1 == 0) //S17
	{
		Delay(50);
		if(R1 == 0)
		{
			if(state == 2)
			{
				if(st_temp > 10)
				{
					st_temp -= 1;
				}
				else
				{
					st_temp = 10;
				}
				while(R1 == 0)
				{
					DisPlay_SMG();
				}
			}
			else if(state == 1)
			{
				while(R1 == 0)
				{
					Read_DS1302_Timer();
					DisPlay_Show_Time_Second();
				}
			}
		}
	}
	else if(R2 == 0) //S16
	{
		Delay(50);
		if(R2 == 0)
		{
			if(state == 2)
			{
				if(st_temp < 90)
				{
					st_temp += 1;
				}
				else
				{
					st_temp = 90;
				}
				while(R2 == 0)
				{
					DisPlay_SMG();
				}
			}
		}
	}
	
}
//=================================//

void DisPlay_SMG()
{
	if(state == 0)
	{
		DisPlay_Show_Temp();
	}
	else if(state == 1)
	{
		DisPlay_Show_Time();
	}
	else if(state == 2)
	{
		DisPlay_Set_Temp();
	}
}
//==========定时器=============//
void Init_Timer0()
{
	TMOD = 0x01;
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	
	ET0 = 1;
	EA = 1;
	TR0 = 1;
}

void Service_Timer0() interrupt 1
{
	TH0 = (65535 - 50000) / 256;
	TL0 = (65535 - 50000) % 256;
	
	if(temp_relay_flag == 1)
	{	
		t_count ++;
		if(t_count > 100)
		{
			temp_relay_flag = 0;
			t_count = 0;
		}
	}
	
	if(relay_state == 1)
	{
		led_count++;
		if(led_count == 2)
		{
			led_count = 0;
			led_state = led_state ^ 0x04;
			Select_HC573(4,led_state);
		}
	}
	else 
	{
		led_state = led_state | 0x04;
		Select_HC573(4,led_state);
	}
}

//=============================//

//=========继电器==============//
void Relay_Temp()
{
	if(control_mode == 0)
	{
		if(temp / 10 > st_temp)
		{
			Select_HC573(5,0x10);
			relay_state = 1;
		}
		else
		{
			Select_HC573(5,0x00);
			relay_state = 0;
		}
	}
	else
	{
		if(temp_relay_flag == 1)
		{
			Select_HC573(5,0x10);
		}
		else
		{
			Select_HC573(5,0x00);
		}
	}
}
//============================//

//============LED===========//
void LED_Working()
{
	if(control_mode == 1)
	{
		if(temp_relay_flag == 1)
		{
			relay_state = 1;
			led_state = led_state & 0xFE;
			Select_HC573(4,led_state);
		}
		else
		{
			relay_state = 0;
			led_state = led_state | ~(0xFE);
			Select_HC573(4,led_state);
		}
	}
	
	if(control_mode == 0)
	{
		led_state = led_state & 0xFD;
		Select_HC573(4,led_state);
	}
	else
	{
		led_state = led_state | ~(0xFD);
		Select_HC573(4,led_state);
	}
}
//====================================//


void main()
{
	DS1302_Config();
	Init_Timer0();
	while(1)
	{
		Read_DS18B20_temp();
		Read_DS1302_Timer();
		Scan_Key();
		LED_Working();
		Relay_Temp();
		DisPlay_SMG();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值