蓝桥杯 第五届省赛模拟题 智能灌溉系统 代码分享

备战2021蓝桥杯单片机的过程中做的模拟题
自我测试没什么问题,程序写法应该不是最简但很好理解
互相交流,欢迎大佬提建议~
题目可以戳这里 题目

-main.c

#include <reg52.h>
#include <ds1302.h>
#include <iic.h>
#define u8 unsigned char
#define u16 unsigned int
code u8 duanma[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,
							0x82,0xf8,0x80,0x90,0x88,0x80,
							0xc6,0xc0,0x86,0x8e,0xbf,0x7f};
u8 write[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
u8 read[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
u8 timer[7] = {0x55,0x30,0x08,0x20,0x01,0x01,0x21};
sbit beep = P0^6;
sbit relay = P0^4;
sbit S7 = P3^0;sbit S6 = P3^1;sbit S5 = P3^2;sbit S4 = P3^3;
sbit L1 = P0^0;
sbit L2 = P0^1;
u8 shidu,yuzhi=50;
u8 stat = 0;  //auto
u8 bi6 = 1;	  //可以响
u8 change = 0;  //调整阈值
u8 manned = 0;  //不浇水

u8 yuzhichange;
void delay(u16 t)
{
	while(t--);
}

void ds1302config()
{
	u8 i;
	Write_Ds1302_Byte(0x8e,0x00);  //关闭写保护
	for(i = 0; i < 7;i++)
	{
		Write_Ds1302_Byte(write[i],timer[i]);
	}
	Write_Ds1302_Byte(0x8e,0x80);
}

void read1302()
{
	char i;
	for(i=0;i<7;i++)
	{
	timer[i] = Read_Ds1302_Byte(read[i]);
	}
}


void displaybit(u8 value,u8 pos)
{
	P2 = 0xe0;P0 = 0xff;
	P2 = 0xc0;P0 = 0x01 << pos;
    P2 = 0xe0;P0 = value;
}

void display()
{
	if(change == 0){
	displaybit(duanma[timer[2]/16],0);
	delay(50);
	displaybit(duanma[timer[2]%16],1);
	delay(50);
	displaybit(duanma[16],2);
	delay(50);
	displaybit(duanma[timer[1]/16],3);
	delay(50);
	displaybit(duanma[timer[1]%16],4);
	delay(50);
	displaybit(0xff,5);
	delay(50);
	shidu = (u8)(IIC_Read(0x03)/2.57);
	displaybit(duanma[shidu/10],6);
	delay(50);
	displaybit(duanma[shidu%10],7);
	delay(50);
	}
	if(change == 1){
	displaybit(duanma[16],0);
	delay(50);
	displaybit(duanma[16],1);
	delay(50);
	displaybit(0xff,2);
	delay(50);
	displaybit(0xff,3);
	delay(50);
	displaybit(0xff,4);
	delay(50);
	displaybit(0xff,5);
	delay(50);
	displaybit(duanma[yuzhichange/10],6);
	delay(50);
	displaybit(duanma[yuzhichange%10],7);
	delay(50);
	}
	P2 = 0xc0;P0 = 0xff;
	P2 = 0xe0;P0 = 0xff;
}

void relaywork()
{
	if(stat == 0)
	{
		if(shidu >= yuzhi) {P2 = (P2&0x1f)|0xa0;beep = 0;relay = 0;}
		if(shidu < yuzhi) {P2 = (P2&0x1f)|0xa0;beep = 0;relay = 1;}
	}
	if(stat == 1)
	{
		if(manned == 0) 
		{
			if(bi6 == 0)
			{
				P2 = (P2&0x1f)|0xa0;relay = 0;beep = 0;
			}
			if(bi6 == 1)
			{
				if(shidu < yuzhi){P2 = (P2&0x1f)|0xa0;relay = 0;beep = 1;}
				if(shidu >= yuzhi){P2 = (P2&0x1f)|0xa0;relay = 0;beep = 0;}
			}
		} 
		if(manned == 1) 
		{
			if(bi6 == 0)
			{
				if(shidu < yuzhi){P2 = (P2&0x1f)|0xa0;relay = 1;beep = 0;}
				if(shidu >= yuzhi){P2 = (P2&0x1f)|0xa0;relay = 1;beep = 0;}
			}
			if(bi6 == 1)
			{
				if(shidu < yuzhi){P2 = (P2&0x1f)|0xa0;relay = 1;beep = 1;}
				if(shidu >= yuzhi){P2 = (P2&0x1f)|0xa0;relay = 1;beep = 0;}
			}
		}
	}
}

void keyscan()
{
	if(stat == 0)	 //auto
	{
		if(S7 == 0)
		{
			u8 t = 50;
			while(t--) display();
			if(S7 == 0)
			{
				while (S7 == 0) display();
				if(stat == 0) stat = 1;
			}
		}
		if(S6 == 0)
		{
			u8 t = 50;
			while(t--) display();
			if(S6 == 0 && change == 0){ change = 1;
			while(S6 == 0) display();yuzhichange = Read_24C02(0x01);}
			if(S6 == 0 && change == 1){ change = 0;
			while(S6 == 0) display();Write_24C02(0x01,yuzhichange);}
		}
		if(S5 == 0 && change == 1)
		{
			u8 t = 50;
			while(t--) display();
			if(S5 == 0) yuzhichange++;
		}
		if(S4 == 0 && change == 1)
		{
			u8 t = 50;
			while(t--) display();
			if(S4 == 0) yuzhichange--;
		}
	}
	if(stat == 1)	 //manned
	{
		if(S7 == 0)
		{
			u8 t = 50;
			while(t--) display();
			if(S7 == 0)
			{
				while (S7 == 0) display();
				if(stat == 1) stat = 0;
			}
		}
		if(S6 == 0)
		{
			u8 t = 50;
			while(t--) display();
			if(S6 == 0)
			{
				while (S6 == 0) display();
				if(bi6 == 0) bi6 = 1;
				if(bi6 == 1) bi6 = 0;
			}
		}
		if(S5 == 0)
		{
			u8 t = 50;
			while(t--) display();
			if(S5 == 0)
			{
				while (S5 == 0) display();
				manned = 1;
			}
		}
		if(S4 == 0)
		{
			u8 t = 50;
			while(t--) display();
			if(S4 == 0)
			{
				while (S4 == 0) display();
				manned = 0;
			}
		}
	}
}		


void ledrun()
{
	if(stat == 0) {P2 = 0x80;L1 = 0;L2 = 1;}
	if(stat == 1) {P2 = 0x80;L1 = 1;L2 = 0;}
	P2 = 0xff;
}
void main()
{
	P2 = 0xa0;P0 = 0x00;P2 = 0x80;P0 = 0xff;
	ds1302config();
	Write_24C02(0x01,0x32);
	yuzhichange = yuzhi;
	while(1)
	{
		ledrun();
		keyscan();
		display();
		yuzhi = Read_24C02(0x01);
		shidu = (u8)(IIC_Read(0x03)/2.57);
		read1302();
		relaywork();
		display();
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值