蓝桥杯单片机省赛第六届(测试过没有任何问题)

 不知道复习啥,敲敲往年代码吧,随缘敲到第几届,从第六届开始,小问题还是存在的,想要完美还是要细心呀!


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

sbit S7=P3^0;
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;
sbit L1=P0^0;

unsigned char code SMG_duanma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
unsigned char interval=1;
unsigned char stat=0;
unsigned char Timer[3]={0x50,0x59,0x23};
unsigned char temp[10]={0};
unsigned char index=0;
unsigned char count=0;
bit smg_f=0;
unsigned char temp_num=0;
unsigned char temp_f=0;
unsigned char led_f=0;
unsigned char writeaddr[3]={0x80,0x82,0x84};
unsigned char readaddr[3]={0x81,0x83,0x85};
unsigned char temp_c=0;
unsigned char k6=0;


void HC138(unsigned char m)
{
	switch(m)
	{
		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 SMG_bit(unsigned char pos,unsigned char dat)
{
	P0=(0x01<<pos);
	HC138(6);
	P0=dat;
	HC138(7);
}

void SMG_all(unsigned char dat)
{
	P0=0xff;
	HC138(6);
	P0=dat;
	HC138(7);
}

void SMG_delay(unsigned char q)
{
	while(q--);
}

void SMG_display()	   
{
	if(stat==0)
	{
		SMG_bit(5,0xbf);
		SMG_delay(200);
		SMG_bit(5,0xff);
		SMG_delay(200);
		SMG_bit(6,SMG_duanma[interval/10]);
		SMG_delay(200);
		SMG_bit(6,0xff);
		SMG_delay(200);
		SMG_bit(7,SMG_duanma[interval%10]);
		SMG_delay(200);
		SMG_bit(8,0xff);
		SMG_delay(200);
	
		SMG_all(0xff);
		SMG_delay(200);
	}
	else if(stat==1)
	{
		SMG_bit(0,SMG_duanma[Timer[2]/16]);
		SMG_delay(200);
		SMG_bit(0,0xff);
		SMG_delay(200);
		SMG_bit(1,SMG_duanma[Timer[2]%16]);
		SMG_delay(200);
		SMG_bit(1,0xff);
		SMG_delay(200);
		if(smg_f==1)
		{
			SMG_bit(2,0xbf);
			SMG_delay(200);
			SMG_bit(2,0xff);
			SMG_delay(200);
		}
		SMG_bit(3,SMG_duanma[Timer[1]/16]);
		SMG_delay(200);
		SMG_bit(3,0xff);
		SMG_delay(200);
		SMG_bit(4,SMG_duanma[Timer[1]%16]);
		SMG_delay(200);
		SMG_bit(4,0xff);
		SMG_delay(200);
		if(smg_f==1)
		{
			SMG_bit(5,0xbf);
			SMG_delay(200);
			SMG_bit(5,0xff);
			SMG_delay(200);
		}
		SMG_bit(6,SMG_duanma[Timer[0]/16]);
		SMG_delay(200);
		SMG_bit(6,0xff);
		SMG_delay(200);
		SMG_bit(7,SMG_duanma[Timer[0]%16]);
		SMG_delay(200);
		SMG_bit(8,0xff);
		SMG_delay(200);
	
		SMG_all(0xff);
		SMG_delay(200);
	}
	if(stat==2)
	{
		SMG_bit(0,0xbf);
		SMG_delay(200);
		SMG_bit(0,0xff);
		SMG_delay(200);
		SMG_bit(1,SMG_duanma[index/10]);
		SMG_delay(200);
		SMG_bit(1,0xff);
		SMG_delay(200);
		SMG_bit(2,SMG_duanma[index%10]);
		SMG_delay(200);
		SMG_bit(2,0xff);
		SMG_delay(200);
		SMG_bit(5,0xbf);
		SMG_delay(200);
		SMG_bit(5,0xff);
		SMG_delay(200);
		SMG_bit(6,SMG_duanma[temp[index]/10]);
		SMG_delay(200);
		SMG_bit(6,0xff);
		SMG_delay(200);
		SMG_bit(7,SMG_duanma[temp[index]%10]);
		SMG_delay(200);
		SMG_bit(7,0xff);
		SMG_delay(200);
		
		SMG_all(0xff);
		SMG_delay(200);
	}	
}

void InitTimer0()
{
	TMOD=0x01;
	TH0=(65535-10000)/256; //10ms
	TL0=(65535-10000)%256;

	EA=1;
	ET0=1;
	TR0=1;
}

void serviceTimer0() interrupt 1
{
	TH0=(65535-10000)/256; //10ms		   0.01s
	TL0=(65535-10000)%256;

	count++;
	if(count>=100)
	{
		smg_f=~smg_f;
		count=0;
	}

	if(temp_f==0)
	{
		temp_c++;
	}
	if(temp_c>=interval*100)
	{
		temp_f=1;
		temp_c=0;
	}
}

void delay(unsigned char d)
{
	while(d--);
}

float read_temp()		  //attention
{
	unsigned char LSB;
	unsigned char MSB;
	unsigned int tem;
	float rev;

	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	delay(1000);
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();

//	tem=(MSB<<4)|LSB;
//	if((tem&0xf8000)==0x0000)
//	{
//		rev=tem>>4;
//	}
    tem = MSB;
	tem = tem << 8;
	tem = tem | LSB;
	rev = tem * 0.0625;
	return rev;
}

void temp_collect()
{
	
	if(stat==1)
	{
		if(temp_num<=9)
		{
			if(temp_f==1)
			{
				temp[index]=read_temp();
				temp_f=0;
				index++;
				temp_num++;
			}
		}
			else if(temp_num>=10)
			{
				led_f=1;
				index = 0;
			}
	}
}

void write1302()
{
	unsigned char i;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i=0;i<=2;i++)
	{
		Write_Ds1302_Byte(writeaddr[i],Timer[i]);
	}
	Write_Ds1302_Byte(0x8e,0x80);
}

void read1302()
{
	unsigned char i;
	for(i=0;i<=2;i++)
	{
		Timer[i]=Read_Ds1302_Byte(readaddr[i]);
	}
}

void key_delay(unsigned char f)
{
	while(f--);
}

void keyboard()
{
	unsigned char i=0;
	if(S4==0)
	{
		key_delay(200);
		if(S4==0)
		{
			while(S4==0)
			{
				SMG_display();
			}
			if(interval==1)
			{				  
				interval=5;
			}
			else if(interval==5)
			{				  
				interval=30;
			}
			else if(interval==30)
			{				  
				interval=60;
			}
			else if(interval==60)
			{				  
				interval=1;
			}
		}
	}
	if(S5==0)
	{
		key_delay(200);
		if(S5==0)
		{
			while(S5==0)
			{
				SMG_display();
			}
			if(stat==0)
			{
				stat=1;
			}
		}
	}
	if(S6==0)
	{
		key_delay(200);
		if(S6==0)
		{
			while(S6==0)
			{
				SMG_display();
			}
			if(stat==1)
			{
				stat=2;
			}
			if(led_f==1)
			{
				led_f=0;
			}
			if(stat==2)
			{
				k6++;
				if(k6>1)
				{
					index++;
				}
				if(k6>=10)
				{
					index=0;
					k6=1;
				}
			}
		}
	}
	if(S7==0)
	{
		key_delay(200);
		if(S7==0)
		{
			while(S7==0)
			{
				SMG_display();
			}
			if(stat==2)
			{
				stat=0;
			}
			temp_num=0;
			index=0;
			smg_f=1;
			k6=0;
			for(i=0;i<9;i++)
			{
				temp[1]=0;
			}
		}
	}
}
	
void InitSystem()
{
	P0=0xff;
	HC138(4);
	P0=0x00;
	HC138(5);
	SMG_all(0xff);
}

void LED()
{
	if(led_f==1)
	{
		if(smg_f==1)
		{
			L1=0;
		}
		else if(smg_f==0)
		{
			L1=1;
		}
	}
	HC138(4);
}

void main()
{
	InitSystem();
	read_temp();
	InitTimer0();
	write1302();
	while(1)
	{
		read1302();
		LED();
		SMG_display();
		keyboard();
		temp_collect();
	}
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值