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

在DS1302.c文件中加入如下代码 ,添加到头文件


unsigned char time_start[3] = {16,59,55};
unsigned char time_read[3];

void Write_Init()
{
	Write_Ds1302_Byte(0x8e,0x00);
	Write_Ds1302_Byte(0x84,(time_start[0]/10 << 4)+ (time_start[0]%10));
	Write_Ds1302_Byte(0x82,(time_start[1]/10 << 4)+ (time_start[1] %10));
	Write_Ds1302_Byte(0x80,(time_start[2]/10 << 4)+ (time_start[2] %10));
	Write_Ds1302_Byte(0x8e,0x80);
}

void Read_time()
{
	unsigned char tmp1,tmp2,tmp3;
	tmp1 = Read_Ds1302_Byte(0x85);
	time_read[0] = (tmp1 >> 4)*10 + (tmp1 & 0x0f);
	tmp2 = Read_Ds1302_Byte(0x83);
	time_read[1] = (tmp2 >> 4)*10 + (tmp2 & 0x0f);
	tmp3 = Read_Ds1302_Byte(0x81);
	time_read[2] = (tmp3 >> 4)*10 + (tmp3 & 0x0f);
}

在onewire.c文件中加入以下代码,注意时钟频率 

void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	unsigned int i;
	while(t--)
	{for(i = 0;i<12;i++);}	//×¢ÒâʱÖÓƵÂÊ
}

unsigned int rd_temperature(void)
{
	unsigned int temp,LSB,MSB;
	EA = 0;
	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) | LSB;
	temp = (temp >> 4)*10 + (LSB & 0x0f)*0.625;
	EA = 1;
	return temp;
}

在iic.c文件中加入如下代码,添加到头文件

unsigned int Read_adc_an3()
{
	unsigned int temp;
	EA = 0;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(0x41);
	IIC_WaitAck();
	
	IIC_Start();
	IIC_SendByte(0x91);
	temp = IIC_RecByte();
	IIC_SendAck(1);
	IIC_Stop();
	EA = 1;
	
	return temp;
}

按键处理

#include "stc15f2k60s2.h"
#include "key.h"
#include "Digtal.h"
unsigned char key_value = 0xff;
unsigned char jiemian_state = 0,canshu_state = 1;
bit jiemian_flag = 0;
void Read_keys()
{
	static int key_state = 0,hang;
	switch(key_state)
	{
		case 0:
		{
			P3 = 0x0f;P44 = 0;P42 = 0;
			if(P3 != 0x0f) key_state = 1;
		}break;
		case 1:
		{
			P3 = 0x0f;P44 = 0;P42 = 0;
			if(P3 != 0x0f) 
			{
//				if(P30 == 0) hang = 1;
//				if(P31 == 0) hang = 2;
				if(P32 == 0) hang = 3;
				if(P33 == 0) hang = 4;
				switch(hang)
				{
					case 3:
					{
						P3 = 0xf0;P42 = 1;P44 = 1;
						if(P44 == 0) {key_value = 5;key_state = 2;}
						if(P42 == 0) {key_value = 9;key_state = 2;}
					}break;
					case 4:
					{
						P3 = 0xf0;P42 = 1;P44 = 1;
						if(P44 == 0) {key_value = 4;key_state = 2;}
						if(P42 == 0) {key_value = 8;key_state = 2;}
					}break;
				}
			}
			else key_state = 0;
		}break;
		case 2:
		{
			P3 = 0x0f;P44 = 0;P42 = 0;
			if(P3 == 0x0f) 
			{
				key_state = 1;
				key_value = 0;
			}
		}break;
	}
}

void KEY_Proc()
{
	Read_keys();
	if(key_value != 0xff)
	{
		switch(key_value)
		{
			case 4:
			{
				key_value = 0;
				jiemian_flag  = ~jiemian_flag;
				canshu_state = 1;
				jiemian_state  = 0;
				light_set_light = light_set;
				time_set_shi = time_set;
				temp_set_temp = temp_set;
			}break;
			case 5:
			{
				if(jiemian_flag == 0)
				{
					key_value = 0;
					if(++jiemian_state == 3) jiemian_state = 0;
				}
				if(jiemian_flag == 1)
				{
					key_value = 0;
					if(++canshu_state == 4) 
						{
							canshu_state = 1;
						}
				}
			}break;
			case 9 :
			{
				key_value = 0;
				if(jiemian_flag == 1)
				{
					switch(canshu_state)
					{
						case 1:
						{
							if(++time_set == 24) time_set = 23;
						}break;
						case 2:
						{
							if(++temp_set == 100) temp_set = 99;
						}break;
						case 3:
						{
							if(++light_set == 9) light_set = 8;
						}break;
					}
				}
			}break;
			case 8 :
			{
				key_value = 0;
				if(jiemian_flag == 1)
				{
					switch(canshu_state)
					{
						case 1:
						{
							if(time_set <= 0) time_set = 0;
							else time_set--;
						}break;
						case 2:
						{
							if(temp_set <= 0) temp_set = 0;
							else temp_set--;
						}break;
						case 3:
						{
							if(light_set <= 4) light_set = 4;
							else light_set--;
						}break;
					}
				}
			}break;
			
		}
	}
}

数码管处理(*2 表示整数,*2+1表示小数)

#include "stc15f2k60s2.h"
#include "Digtal.h"
#include "key.h"
#include "iic.h"
#include "led.h"
#include "ds1302.h"
#include "onewire.h"
unsigned int read_light,read_lig,read_temp,light_set = 4,temp_set = 25,time_set = 17,light_level = 0;
code unsigned char Tab[] = {0xc0,0x40,0xf9,0x79,0xa4,0x24,0xb0,0x30,0x99,0x19,	
							0x92,0x12,0x82,0x02,0xf8,
							0x78,0x80,0x00,0x90,0x10,0xff,0xbf,0xc6,0x86,0x8c};
unsigned char Digtal[8] = {20,20,20,20,20,20,20,20};
unsigned int temp_set_temp = 25,time_set_shi = 17,light_set_light = 4;//²ÎÊý½çÃæÉ趨ֵ
unsigned int upcom = 0,ucled = 0;
void Digtal_buff()
{
	unsigned char buf;
	buf = P0;
	P0 = 0xff;
	P2 = (P2 & 0x1f) | 0xe0;
	P2 = (P2 & 0x1f);
	P0 = 1 << upcom;
	P2 = (P2 & 0x1f) | 0xc0;
	P2 = (P2 & 0x1f);
	P0 = Tab[Digtal[upcom]];
	P2 = (P2 & 0x1f) | 0xe0;
	P2 = (P2 & 0x1f);
	
	if(++upcom == 8)
		upcom = 0;
	P0 = buf;
}

void Digtal_Proc()
{
	if(jiemian_flag == 0)
	{
		if(jiemian_state == 0)
		{
			Digtal[0] = time_read[0]/10*2;
			Digtal[1] = time_read[0]%10*2;
			Digtal[2] = 21;
			Digtal[3] = time_read[1]/10*2;
			Digtal[4] = time_read[1]%10*2;
			Digtal[5] = 21;
			Digtal[6] = time_read[2]/10*2;
			Digtal[7] = time_read[2]%10*2;
		}
		else if(jiemian_state == 1)
		{
			Digtal[0] = 22;
			Digtal[1] = 20;
			Digtal[2] = 20;
			Digtal[3] = 20;
			Digtal[4] = 20;
			Digtal[5] = (read_temp/100)*2;
			Digtal[6] = read_temp%100/10*2 + 1;
			Digtal[7] = read_temp%10*2;
		}
		else if(jiemian_state == 2)
		{
			Digtal[0] = 23;
			Digtal[1] = 20;
			Digtal[2] = read_light/100*2+1;
			Digtal[3] = read_light%100/10*2;
			Digtal[4] = read_light%10*2;
			Digtal[5] = 20;
			Digtal[6] = 20;
			Digtal[7] = light_level*2;
		}
	}
	
	else if(jiemian_flag == 1)
	{
		if(canshu_state == 1)
		{
			Digtal[0] = 24;
			Digtal[1] = canshu_state*2;
			Digtal[2] = 20;
			Digtal[3] = 20;
			Digtal[4] = 20;
			Digtal[5] = 20;
			Digtal[6] = time_set_shi/10*2 ;
			Digtal[7] = time_set_shi%10*2;
		}
		else if(canshu_state == 2)
		{
			Digtal[0] = 24;
			Digtal[1] = canshu_state*2;
			Digtal[2] = 20;
			Digtal[3] = 20;
			Digtal[4] = 20;
			Digtal[5] = 20;
			Digtal[6] = temp_set_temp/10*2 ;
			Digtal[7] = temp_set_temp%10*2;
		}
		else if(canshu_state == 3)
		{
			Digtal[0] = 24;
			Digtal[1] = canshu_state*2;
			Digtal[2] = 20;
			Digtal[3] = 20;
			Digtal[4] = 20;
			Digtal[5] = 20;
			Digtal[6] = 20 ;
			Digtal[7] = light_set_light*2;
		}
	}
}

led处理

#include "led.h"
#include "Digtal.h"
#include "stc15f2k60s2.h"
#include "ds1302.h"
bit led_flag = 0,light_flag = 0,light_flag_1  =0;
unsigned char led_time = 0,led_time_one = 0;
void Disp_LED(unsigned char ucled)
{
	unsigned char buf;
	buf = P0;
	P0 = ~ucled;
	P2 = (P2 & 0x1f) | 0x80;
	P2 = (P2 & 0x1f);
	P0  = buf;
}

void LED_1_Proc()
{
	if(time_set_shi > 8)
	{
		if(((time_read[0] >= time_set_shi) && (time_read[0] <= 23)) || (time_read[0] < 8))
		{
			ucled |= 0x01;
			Disp_LED(ucled);
		}
		else 
		{
			ucled &= 0xfe;
			Disp_LED(ucled);
		}
	}
	if(time_set_shi < 8)
	{
		if((time_read[0] >= time_set_shi) && (time_read[0] < 8))
		{
			ucled |= 0x01;
			Disp_LED(ucled);
		}
 		else 
		{
			ucled &= 0xfe;
			Disp_LED(ucled);
		}
	}
}
void LED_2_Proc()
{
	if(light_level == 1)
	{
		if(light_set_light == 4)
		{
			ucled |= 0x08;
			Disp_LED(ucled);
		}
		else 
		{
			ucled &= 0xf7;
			Disp_LED(ucled);
		}
		if(light_set_light == 5)
		{
			ucled |= 0x10;
			Disp_LED(ucled);
		}
		else 
		{
			ucled &= 0xef;
			Disp_LED(ucled);
		}
		if(light_set_light == 6)
		{
			ucled |= 0x20;
			Disp_LED(ucled);
		}
		else 
		{
			ucled &= 0xdf;
			Disp_LED(ucled);
		}
		if(light_set_light == 7)
		{
			ucled |= 0x40;
			Disp_LED(ucled);
		}
		else 
		{
			ucled &= 0xbf;
			Disp_LED(ucled);
		}
		if(light_set_light == 8)
		{
			ucled |= 0x80;
			Disp_LED(ucled);
		}
		else 
		{
			ucled &= 0x7f;
			Disp_LED(ucled);
		}
	}
	else 
	{
		ucled &= 0x07;
		Disp_LED(ucled);
	}
}

void LED_Proc()
{
	if(read_temp < (temp_set_temp*10))
	{
		ucled |= 0x02;
		Disp_LED(ucled);
	}
	else 
	{
		ucled &= 0xfd;
		Disp_LED(ucled);
	}
	if(light_level == 1)
	{
		light_flag = 1;
	}
	else 
	{
		light_flag  = 0;
		led_time = 0;
	}
	if((light_level == 0) && (led_flag == 1))
	{
		light_flag_1 = 1;
	}
	else 
	{
		light_flag_1 = 0;
		led_time_one = 0;
	}
}

时钟处理

#include "stc15f2k60s2.h"
#include "Digtal.h"
#include "Timer.h"

unsigned int count_10ms = 0,count_300ms = 0,count_500ms = 0,count_1s = 0;
void Timer0Init(void)		//1??@12.000MHz
{
	AUXR &= 0x7F;		//?????12T??
	TMOD &= 0xF0;		//???????
	TMOD |= 0x01;		//???????
	TL0 = 0x18;		//??????
	TH0 = 0xFC;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????
	EA = 1;
	ET0 = 1;
}

void Service0() interrupt 1
{
	TL0 = 0x18;		//??????
	TH0 = 0xFC;
	Digtal_buff();
	count_10ms++;
	count_300ms++;
	count_500ms++;
	count_1s++;
}

主函数,注意建立各个模块的头文件

#include "stc15f2k60s2.h"
#include "Digtal.h"
#include "key.h"
#include "iic.h"
#include "led.h"
#include "Timer.h"
#include "ds1302.h"
#include "onewire.h"

void System()
{
	P0 = 0xff;
	P2 = (P2 & 0x1f) | 0x80;
	P2 = (P2 & 0x1f);
	
	P0 = 0x00;
	P2 = (P2 & 0x1f) | 0xa0;
	P2 = (P2 & 0x1f);
}

void main()
{
	Write_Init();
	System();
	Timer0Init();
	read_temp = rd_temperature();
	while(1)
	{
		if(count_10ms >= 10)
		{
			count_10ms  = 0;
			KEY_Proc();
		}
		if(count_300ms >= 300)
		{
			count_300ms  = 0;
			read_lig = Read_adc_an3();
			read_light = read_lig*1.96 + 0.5;
			if(read_light > 30) light_level = 0;
			else light_level = 1;
			LED_2_Proc();
		}
		
		if(count_500ms >= 500)
		{
			count_500ms  = 0;
			Read_time();
			Digtal_Proc();
			LED_1_Proc();
			LED_Proc();
			read_temp = rd_temperature();
		}
		if(count_1s > 1000)
		{
			count_1s = 0;
			if(light_flag == 1)
			{
				led_time++;
				if(led_time == 3)
				{
					led_time  = 0;
					led_flag  = 1;
					//light_flag = 0;
					ucled |= 0x04;
					Disp_LED(ucled);
				}
			}
			if(light_flag_1 == 1)
			{
				led_time_one++;
				if(led_time_one == 3)
				{
					led_flag = 0;
					led_time_one = 0;
					ucled &= 0xfb;
					Disp_LED(ucled);
				}
			}
			
			
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值