蓝桥杯单片机第十一届国赛

有问题欢迎指出,共同学习交流

cs 复制代码
#include <STC15F2K60S2.H>
#include "ds1302.h"
#include "iic.h"
#include "onewire.h"
#include "intrins.h"
#define TSMG 500
void SelectHC573(unsigned char channel,unsigned char dat);
void DisplaySMG_Info();
void read_myadc();
sbit h3 = P3^2;
sbit h4 = P3^3;
sbit s1 = P4^4;
sbit s2 = P4^2;
code unsigned char Seg_Table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
code unsigned char Seg_Dot[] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
code unsigned char read_addre[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
code unsigned char write_addre[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char mytimer[7] = {0x50,0x59,0x16,0x06,0x05,0x01,0x24};
unsigned char t_h = 0;
unsigned char t_m = 0;
unsigned char t_s = 0;
unsigned int adc_smg = 0;
float temp_value = 0;
unsigned int temp_smg = 0;
unsigned char UI = 0;//0-数据界面 1-参数界面
unsigned char UI_data = 0;//0-时间 1-温度 2-亮暗状态
unsigned char UI_param = 0;//0-时间参数 1-温度参数 2-指示灯参数
unsigned char flag_light = 0;//灯亮暗标志
unsigned char h_param = 17;//小时参数
unsigned char temp_param = 25;//温度参数
unsigned char light_param = 4;//指示灯参数
unsigned char set_h = 0;//显示参数设置
unsigned char set_temp = 0;//温度参数设置
unsigned char set_light = 0;//指示灯参数设置
unsigned char stat_led = 0xff;
unsigned int count_3s = 0;
unsigned int count1_3s = 0;
unsigned char value_led;//临时状态0x01
unsigned char count_200ms = 0;
unsigned char hour = 0;//十进制小时
void Init_timer0()//10ms
{
	AUXR &= 0x7F;
	TMOD &= 0xf0;
	TMOD |= 0x01;
	TL0 = 0xf0;
	TH0 = 0xd8;
	TR0 = 1;
	ET0 = 1;
	EA = 1;
}
void sevice_timer0() interrupt 1
{
	TL0 = 0xf0;
	TH0 = 0xd8;
	DisplaySMG_Info();
	count_200ms++;
	if(flag_light == 1)
	{
		count_3s = 0;
		count1_3s++;
		if(count1_3s == 300)
		{
			count1_3s = 0;
			stat_led &= 0xfb;
			SelectHC573(4,stat_led);
		}
	}
	else if(flag_light == 0)
	{
		count1_3s = 0;
		count_3s++;
		if(count_3s == 300)
		{
			count_3s = 0;
			stat_led |= 0x04;
			SelectHC573(4,stat_led);
		}
		
	}
	if(count_200ms == 20)
	{
		count_200ms = 0;
		read_myadc();
	}
}
void Delay20ms()		//@12.000MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 1;
	j = 234;
	k = 113;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

void Init_mytemp()
{
	unsigned char LSB,MSB;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	do{
		init_ds18b20();
		Write_DS18B20(0xcc);
		Write_DS18B20(0xbe);
		LSB = Read_DS18B20();
		MSB = Read_DS18B20();
		MSB = (MSB << 4) | (LSB >> 4);
	}while(MSB == 85);
}
void read_mytemp()
{
	unsigned char LSB,MSB;
	unsigned int temp;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB = Read_DS18B20();
	MSB = Read_DS18B20();
	temp = MSB << 8;
	temp = temp | LSB;
	if((temp & 0xf800) == 0x0000)
	{
		temp_value = temp * 0.0625;
		temp_smg = temp_value * 10;
	}
}
void read_myadc()
{
	unsigned char adc_value = 0;
	float adc_volt = 0;
	I2CStart();
	I2CSendByte(0x90);
	I2CWaitAck();
	I2CSendByte(0x01);
	I2CWaitAck();
	I2CStop();
	
	I2CStart();
	I2CSendByte(0x91);
	I2CWaitAck();
	adc_value = I2CReceiveByte();
	I2CSendAck(1);
	I2CStop();
	adc_volt = adc_value * (5.0 / 255);
	adc_smg = adc_volt * 100;
	if(adc_volt < 1.0)
	{
		flag_light = 1;
	}
	else
	{
		flag_light = 0;
	}
}
void Init_mytimer()
{
	unsigned char i;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i = 0;i < 7;i++)
	{
		Write_Ds1302_Byte(write_addre[i],mytimer[i]);
	}
	Write_Ds1302_Byte(0x8e,0x80);
}
void read_mytimer()
{
	t_h = Read_Ds1302_Byte(0x85);
	t_m = Read_Ds1302_Byte(0x83);
	t_s = Read_Ds1302_Byte(0x81);
}
void SelectHC573(unsigned char channel,unsigned char dat)
{
	P2 = (P2 & 0x1f) | 0x00;
	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 DelaySMG(unsigned int t)
{
	while(t--);
}
void DisplaySMG_Bit(unsigned char pos,unsigned char value)
{
	SelectHC573(6,0x01 << pos);
	SelectHC573(7,value);
	DelaySMG(TSMG);
	SelectHC573(6,0x01 << pos);
	SelectHC573(7,0xff);
}
void DisplaySMG_All(unsigned char value)
{
	SelectHC573(6,0xff);
	SelectHC573(7,value);
}
void DisplaySMG_Info()
{
	if(UI == 0)
	{
		switch(UI_data)
		{
			case 0:
				DisplaySMG_Bit(0,Seg_Table[t_h >> 4]);
				DisplaySMG_Bit(1,Seg_Table[t_h & 0x0f]);
				DisplaySMG_Bit(2,0xbf);
				DisplaySMG_Bit(3,Seg_Table[t_m >> 4]);
				DisplaySMG_Bit(4,Seg_Table[t_m & 0x0f]);
				DisplaySMG_Bit(5,0xbf);
				DisplaySMG_Bit(6,Seg_Table[(t_s >> 4) & 0x07]);
				DisplaySMG_Bit(7,Seg_Table[t_s & 0x0f]);	
			break;
			case 1:
				DisplaySMG_Bit(0,0xc6);
				DisplaySMG_Bit(5,Seg_Table[temp_smg / 100]);
				DisplaySMG_Bit(6,Seg_Dot[temp_smg / 10 % 10]);
				DisplaySMG_Bit(7,Seg_Table[temp_smg % 10]);
			break;
			case 2:
				DisplaySMG_Bit(0,0x86);
				DisplaySMG_Bit(2,Seg_Dot[adc_smg / 100]);
				DisplaySMG_Bit(3,Seg_Table[adc_smg / 10 % 10]);
				DisplaySMG_Bit(4,Seg_Table[adc_smg % 10]);
				DisplaySMG_Bit(7,Seg_Table[flag_light]);
			break;
		}
	}
	else if(UI == 1)
	{
		switch(UI_param)
		{
			case 0:
				DisplaySMG_Bit(0,0x8c);
				DisplaySMG_Bit(1,Seg_Table[1]);
				DisplaySMG_Bit(6,Seg_Table[set_h / 10]);
				DisplaySMG_Bit(7,Seg_Table[set_h % 10]);
			break;
			case 1:
				DisplaySMG_Bit(0,0x8c);
				DisplaySMG_Bit(1,Seg_Table[2]);
				DisplaySMG_Bit(6,Seg_Table[set_temp / 10]);
				DisplaySMG_Bit(7,Seg_Table[set_temp % 10]);
			break;
			case 2:
				DisplaySMG_Bit(0,0x8c);
				DisplaySMG_Bit(1,Seg_Table[3]);
				DisplaySMG_Bit(7,Seg_Table[set_light % 10]);
			break;
		}
	}
}
void sand_key()
{
	h3 = 0;
	h4 = s1 = s2 = 1;
	if(s1 == 0)//s5
	{
		Delay20ms();
		if(s1 == 0)
		{
			if(UI == 0)
			{
				if(UI_data == 0)
				{
					UI_data = 1;
				}
				else if(UI_data == 1)
				{
					UI_data = 2;
				}
				else
				{
					UI_data = 0;
				}
			}
			else if(UI == 1)
			{
				if(UI_param == 0)
				{
					UI_param = 1;
				}
				else if(UI_param == 1)
				{
					UI_param = 2;
				}
				else
				{
					UI_param = 0;
				}
			}
			while(s1 == 0)
			{
				
			}
		}
	}
	else if(s2 == 0)//s9
	{
		Delay20ms();
		if(s2 == 0)
		{
			if(UI == 1)
			{
				if(UI_param == 0)
				{
					if(set_h == 23)
					{
						set_h = 0;
					}
					else
					{
						set_h += 1;
					}
				}
				else if(UI_param == 1)
				{
					if(set_temp == 99)
					{
						set_temp = 0;
					}
					else
					{
						set_temp += 1;
					}
				}
				else if(UI_param == 2)
				{
					if(set_light == 8)
					{
						set_light = 4;
					}
					else
					{
						set_light += 1;
					}
				}
			}
			while(s2 == 0)
			{
				
			}
		}
	}
	
	h4 = 0;
	h3 = s1 = s2 = 1;
	if(s1 == 0)//s4
	{
		Delay20ms();
		if(s1 == 0)
		{
			if(UI == 0)
			{
				set_h = h_param;
				set_temp = temp_param;
				set_light = light_param;
				UI_param = 0;
				UI = 1;
			}
			else
			{
				h_param = set_h;
				temp_param = set_temp;
				light_param = set_light;
				UI_data = 0;
				UI = 0;
			}
			while(s1 == 0)
			{
				
			}
		}
	}
	else if(s2 == 0)//s8
	{
		Delay20ms();
		if(s2 == 0)
		{
			if(UI == 1)
			{
				if(UI_param == 0)
				{
					if(set_h == 0)
					{
						set_h = 23;
					}
					else
					{
						set_h -= 1;
					}
				}
				else if(UI_param == 1)
				{
					if(set_temp == 0)
					{
						set_temp = 99;
					}
					else
					{
						set_temp -= 1;
					}
				}
				else if(UI_param == 2)
				{
					if(set_light == 4)
					{
						set_light = 8;
					}
					else
					{
						set_light -= 1;
					}
				}
			}
			while(s2 == 0)
			{
				
			}
		}
	}
}
void led_control()
{
	hour = (t_h / 16)*10 + t_h % 16;
	if(h_param < 8)
	{
		if((hour < 8) && (hour > h_param))
		{
			stat_led &= 0xfe;
		}
		else
		{
			stat_led |= 0x01;
		}
	}
	else
	{
		if((hour > 8) && (hour < h_param))
		{
			stat_led |= 0x01;
		}
		else
		{
			stat_led &= 0xfe;
		}
	}
	if(temp_value < (float)temp_param)
	{
		stat_led &= 0xfd;
	}
	else
	{
		stat_led |= 0x02;
	}
	if(flag_light == 1)
	{
		stat_led |= 0xf8;
		value_led = 0x01;
		value_led = value_led << (light_param - 1);
		stat_led = stat_led & (~value_led);
	}
	else
	{
		stat_led |= 0xf8;
	}
	SelectHC573(4,stat_led);
}
void main()
{
	Init_mytimer();
	Init_mytemp();
	Init_timer0();
	DisplaySMG_All(0xff);
	SelectHC573(4,0xff);
	SelectHC573(5,0x00);
	while(1)
	{
		read_mytemp();
		read_mytimer();
		sand_key();
		led_control();
	}
}
相关推荐
徐嵌1 小时前
STM32项目---水质水位检测
stm32·单片机·嵌入式硬件
徐嵌1 小时前
STM32项目---畜牧定位器
c语言·stm32·单片机·物联网·iot
lantiandianzi1 小时前
基于单片机的老人生活安全监测系统
单片机·嵌入式硬件·生活
xinghuitunan2 小时前
蓝桥杯顺子日期(填空题)
c语言·蓝桥杯
IT B业生11 小时前
51单片机教程(六)- LED流水灯
单片机·嵌入式硬件·51单片机
一枝小雨11 小时前
51单片机学习心得2(基于STC89C52):串口通信(UART)
单片机·嵌入式硬件·51单片机
IT B业生12 小时前
51单片机教程(一)- 开发环境搭建
单片机·嵌入式硬件·51单片机
海绵波波10714 小时前
Webserver(4.8)UDP、广播、组播
单片机·网络协议·udp
好想有猫猫15 小时前
【51单片机】串口通信原理 + 使用
c语言·单片机·嵌入式硬件·51单片机·1024程序员节
stm 学习ing16 小时前
C语言 循环高级
c语言·开发语言·单片机·嵌入式硬件·算法·嵌入式实时数据库