13届蓝桥杯单片机

13届蓝桥杯单片机,第1张

 

 

 

 

 

 

 

可能感觉这一届还挺简单,12点多子就交卷了,等下老师还要问比赛情况,然后现在有点慌。




main.c

#include "sys.h"
#include "onewire.h"
#include "ds1302.h"
bit FLAG_TEMP = 1;  //温度刷新开关
uchar FLAG = 0;    //L1,继电器开关
bit k3 = 0;    //L3开关控制
bit l3 = 0;    //L3亮灭控制
uchar KEY_FLAG = 0;  //按键响应时间
void SwitchMode();
void main()
{
	InitSystem();
	Timer0Init();
	Set_DS1302();
	while(1)
	{
		Get_DS1302();
		KeyScans();
		if(KEY_FLAG)
		{
			KEY_FLAG = 0;
			Operate_key_val();
		}
		
		DisplaySMG();
		SwitchMode();
	}
}

void SwitchMode()
{
	if(mode == 0)    //温度控制模式
	{
		OperateLed(2,0);
		if(temp > temp_p * 10)
		{
			Operatews(0x10,1);
			k3 = 1;
		}	
		else if(temp < temp_p*10)
		{
			Operatews(0x10,0);
			k3 = 0;
			l3 = 0;
		}	
	}
	else if(mode == 1)     //时间控制模式
	{
		OperateLed(2,1);
		if((Time[0] == 0) && (Time[1] == 0))
		{
			FLAG = 1;       //L1,继电器开标志
		}
		if(FLAG)
		{
			Operatews(0x10,1);
			OperateLed(1,0);
			k3 = 1;
		}
			
		else
		{
			Operatews(0x10,0);
			OperateLed(1,1);
			k3 = 0;
			l3 = 0;
		}
			
	}
	if(l3)
	{
		OperateLed(3,0);
	}
	else
	{
		OperateLed(3,1);
	}
}

sys.c

#include "sys.h"
#include "onewire.h"
void Select_74HC138(uchar channel)
{
	switch(channel)
	{
		case 0: P2 = P2 & 0X1F; break;
		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;
	}
}

void InitSystem()
{
	uchar i = 0;
	Select_74HC138(4);
	P0 = 0XFF;
	Select_74HC138(5);
	P0 = 0X00;
	Select_74HC138(0);
	for(i = 0; i < 80; i++)
		rd_temperature();
}

void Delay(uint ms)
{
	uint i;
	for(ms; ms > 0; ms--)
		for(i = 921; i > 0; i--);
}

void OperateLed(uchar who,status)
{
	static uchar temp;
	uchar zt;
	switch(status)
	{
		case 0:
			temp = temp | _crol_(0x01,who-1);
			P0 = ~temp;
			break;
		case 1:
			if(who == 0xff)
			{
				P0 = 0xff;
				temp = 0;
			}
			else
			{
				zt = (~temp) | _crol_(0x01,who-1);
				if(temp != (~zt))
					temp = ~zt;
				P0 = zt;
			}
			break;
		case 2:
			P0 = ~who;
			temp = who;
			break;
		default: break;
	}
	Select_74HC138(4);
	Select_74HC138(0);
}

void Operatews(uchar num,status)
{
	static uchar t;
	if(status)
	{
		t = num;
		P0 = num;
	}
	else
	{
		t = t & (~num);
		P0 = t;
	}
	Select_74HC138(5);
	Select_74HC138(0);
}

uchar t0 = 0;
uchar count = 0; //继电器
uchar t100ms = 0;   //L3计数
uchar key_t = 0;
void Timer0Init(void)		//50毫秒@12.000MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0xB0;		//设置定时初值
	TH0 = 0x3C;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0 = 1;
	EA = 1;
}

void ServiceTimer0() interrupt 1
{
	t0++;
	if(t0 == 10)    //500ms
	{
		FLAG_TEMP = 1;
		t0 = 0;
	}
	if(FLAG)
	{
		count++;
		if(count == 100)
		{
			count = 0;
			FLAG = 0;
		}
	}
	if(k3)
	{
		t100ms++;
		if(t100ms == 2)  //50ms  0.1s  100ms
		{
			l3 = ~l3;
			t100ms = 0;
		}
	}
	key_t++;
	if(key_t == 3) 
	{
		KEY_FLAG = 1;
		key_t = 0;
	}
}

nixie.c

#include "sys.h"
#include "onewire.h"
#include "ds1302.h"
uchar code nixie[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc1,0xbf,0xff};
//						0   1     2     3   4    5    6    7   8    9     U    -    灭
uint temp_p = 23;   //温度参数
uchar interface = 0;  //界面切换
void DisplayBit(uchar pos,value,bit k)
{
	P0 = 0X00;
	Select_74HC138(6);
	Select_74HC138(0);
	
	if(k)
		P0 = nixie[value]&0x7f;
	else
		P0 = nixie[value];
	Select_74HC138(7);
	Select_74HC138(0);
	
	P0 = _crol_(0x01,pos);
	Select_74HC138(6);
	Select_74HC138(0);
	
	Delay(1);
}

void DisplayTemp()
{
	if(FLAG_TEMP)
	{
		FLAG_TEMP = 0;
		rd_temperature();
	}
	DisplayBit(0,10,0);
	DisplayBit(1,1,0);
	DisplayBit(5,temp / 100,0);
	DisplayBit(6,temp / 10 % 10,1);
	DisplayBit(7,temp % 10,0);
}

void DisplayTime()
{
	DisplayBit(0,10,0);
	DisplayBit(1,2,0);
	
	DisplayBit(3,Time[2] / 10,0);
	DisplayBit(4,Time[2] % 10,0);
	
	DisplayBit(5,11,0);
	DisplayBit(6,Time[1] / 10,0);
	DisplayBit(7,Time[1] % 10,0);
}

void DisplayPara()
{
	DisplayBit(0,10,0);
	DisplayBit(1,3,0);
	
	DisplayBit(6,temp_p / 10,0);
	DisplayBit(7,temp_p % 10,0);
}

void DisplaySMG()
{
	switch(interface)
	{
		case 0: DisplayTemp(); break;
		case 1: DisplayTime(); break;
		case 2: DisplayPara(); break;
	}
}

key.c

#include "sys.h"
#include "ds1302.h"
uchar key_val = 20;
bit mode = 0; //工作模式切换
void KeyScans()
{
	if(key_val == 20)
	{
		l1 = 0;
		l2 = h1 = h2 = 1;
		if(h1 == 0)
		{
			Delay(10);
			key_val = 13;
			while(h1 == 0)
			{
				DisplaySMG();
				SwitchMode();
			}
		}
		if(h2 == 0)
		{
			Delay(10);
			key_val = 12;
			while(h2 == 0)
			{
				DisplaySMG();
				SwitchMode();
			}
		}
		l2 = 0;
		l1 = h1 = h2 = 1;
		if(h1 == 0)
		{
			Delay(10);
			key_val = 17;
			while(h1 == 0)
			{
				if(interface == 1)
				{
					Get_DS1302();
					DisplayBit(0,10,0);
					DisplayBit(1,2,0);
					
					DisplayBit(3,Time[2] / 10,0);
					DisplayBit(4,Time[2] % 10,0);
					
					DisplayBit(5,11,0);
					DisplayBit(6,Time[0] / 10,0);
					DisplayBit(7,Time[0] % 10,0);
				}
				else
				{
					DisplaySMG();
				}
				SwitchMode();
			}
		}
		if(h2 == 0)
		{
			Delay(10);
			key_val = 16;
			while(h2 == 0)
			{
				DisplaySMG();
				SwitchMode();
			}
			
		}
	}
	
}

void Operate_key_val()
{
	if(key_val != 20)
	{
		switch(key_val)
		{
			case 12:
				interface = (interface + 1) % 3;
				break;
			case 13: 
				mode = ~mode;
				break;
			case 16:
				if(interface == 2)
				{
					temp_p += 1;
					if(temp_p == 100)
						temp_p = 99;
				}
				break;
			case 17:
				if(interface == 2)
				{
					temp_p -= 1;
					if(temp_p == 9)
						temp_p = 10;
				}
				break;
			default: break;
		}
	}
	key_val = 20;
}

sys.h

#ifndef _SYS_H_
#define _SYS_H_
#include 
#include 
sbit l1 = P3^5;
sbit l2 = P3^4;
sbit h1 = P3^2;
sbit h2 = P3^3;
#define uchar unsigned char
#define uint unsigned int
extern bit FLAG_TEMP;   //温度刷新开关
extern unsigned char interface;  //界面切换
extern unsigned int temp_p;  //温度参数
extern bit mode;         //温度,时间控制模式
extern unsigned char FLAG;  //L1,继电器开关
extern bit k3;		//L3亮灭控制
extern bit l3;      //L3开关控制
extern unsigned char KEY_FLAG;   //按键响应时间
void SwitchMode();
//sys.c
void Select_74HC138(unsigned char channel);
void InitSystem();
void Delay(unsigned int ms);
void OperateLed(unsigned char who,status);
void Operatews(unsigned char num,status);
void Timer0Init(void);
//nixie.c
void DisplayBit(unsigned char pos,value,bit k);
void DisplayTemp();
void DisplayTime();
void DisplayPara();
void DisplaySMG();
//key.c
void KeyScans();
void Operate_key_val();
#endif

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/577983.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-11
下一篇 2022-04-11

发表评论

登录后才能评论

评论列表(0条)

保存