第四届蓝桥杯单片机设计与开发项目省赛试题

第四届蓝桥杯单片机设计与开发项目省赛试题:
“模拟智能灌溉系统”设计任务书

第四届蓝桥杯单片机设计与开发项目省赛试题_第1张图片
第四届蓝桥杯单片机设计与开发项目省赛试题_第2张图片

  • ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302_Byte(unsigned  char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void xie();
void read1302();
extern unsigned char shizhong[3];
#endif
  • ds1302.c
#include 
#include 
#include 
sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST= P1^3;   // DS1302复位												
unsigned char shi[3]={0,30,8};
unsigned char shizhong[3];

void Write_Ds1302_Byte(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK=0;
		SDA=temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

void Write_Ds1302(unsigned char address,unsigned char dat)     
{ unsigned char num;
	
 	RST=0;
	_nop_();
 	SCK=0;
	_nop_();
 	RST=1;	
   	_nop_();  
 	Write_Ds1302_Byte(address);
	
	num=(dat/10<<4)|(dat%10);
	
 	Write_Ds1302_Byte(num);		
 	RST=0; 
}

unsigned char Read_Ds1302 ( unsigned char address )
{
 	unsigned char i,temp=0x00;
	unsigned char dat1,dat2;
 	RST=0;
	_nop_();
 	SCK=0;
	_nop_();
 	RST=1;
	_nop_();
 	Write_Ds1302_Byte(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
  SDA=0;
	
	dat1=temp/16;
	dat2=temp%16;
	temp=dat1*10+dat2;
	RST=0;
	_nop_();
 	RST=0;
	SCK=0;
	_nop_();
	SCK=1;
	_nop_();
	SDA=0;
	_nop_();
	SDA=1;
	_nop_();
	return temp;			
}
void xie()
{ unsigned char add;
	int i;
	add=0x80;
	Write_Ds1302(0x8e,0x00);
	for(i=0;i<3;i++)
	{
		Write_Ds1302(add,shi[i]);
		add=add+2;
}
	Write_Ds1302(0x8e,0x80);
}
void read1302()
{
unsigned char add;
	int i;
	add=0x81;
	Write_Ds1302(0x8e,0x00);
	for(i=0;i<3;i++)
	{
		shizhong[i]=Read_Ds1302(add);
		add=add+2;
}
	Write_Ds1302(0x8e,0x80);
}
  • iic.h

#ifndef _IIC_H
#define _IIC_H

//函数声明
void IIC_Start(void); 
void IIC_Stop(void);  
void IIC_Ack(bit ackbit); 
void IIC_SendByte(unsigned char byt); 
bit IIC_WaitAck(void);  
unsigned char IIC_RecByte(void); 
unsigned char getad(unsigned char add);
void xieeeprom(unsigned char add,unsigned char date);
unsigned char readeeprom(unsigned char add);


#endif
  • iic.c
#include
#include "reg52.h"
#include "intrins.h"

#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}    


#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

//总线引脚定义
sbit SDA = P2^1;  /* 数据线 */
sbit SCL = P2^0;  /* 时钟线 */


//总线启动条件
void IIC_Start(void)
{
	SDA = 1;
	SCL = 1;
	somenop;
	SDA = 0;
	somenop;
	SCL = 0;	
}

//总线停止条件
void IIC_Stop(void)
{
	SDA = 0;
	SCL = 1;
	somenop;
	SDA = 1;
}

//应答位控制
void IIC_Ack(bit ackbit)
{
	if(ackbit) 
	{	
		SDA = 0;
	}
	else 
	{
		SDA = 1;
	}
	somenop;
	SCL = 1;
	somenop;
	SCL = 0;
	SDA = 1; 
	somenop;
}

//等待应答
bit IIC_WaitAck(void)
{
	SDA = 1;
	somenop;
	SCL = 1;
	somenop;
	if(SDA)    
	{   
		SCL = 0;
		IIC_Stop();
		return 0;
	}
	else  
	{ 
		SCL = 0;
		return 1;
	}
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{   
		if(byt&0x80) 
		{	
			SDA = 1;
		}
		else 
		{
			SDA = 0;
		}
		somenop;
		SCL = 1;
		byt <<= 1;
		somenop;
		SCL = 0;
	}
}

//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
	unsigned char da;
	unsigned char i;
	
	for(i=0;i<8;i++)
	{   
		SCL = 1;
		somenop;
		da <<= 1;
		if(SDA) 
		da |= 0x01;
		SCL = 0;
		somenop;
	}
	return da;
}
unsigned char getad(unsigned char add)
{ unsigned char ad;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
  ad=IIC_RecByte();
	IIC_Stop();
	return ad;

}
void xieeeprom(unsigned char add,unsigned char date)
{
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_SendByte(date);
	IIC_WaitAck();
	IIC_Stop();

}
unsigned char readeeprom(unsigned char add)
{ unsigned char eeprom;
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
   IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	eeprom=IIC_RecByte();
	IIC_Stop();
	return eeprom;

}

  • 主函数
#include
#include
#include
typedef unsigned char uchar;
typedef unsigned int uint;

int yi,er,san,si,wu,liu,qi,ba,a;
sbit s7=P3^0;
sbit s6=P3^1;
sbit s5=P3^2;
sbit s4=P3^3;
uchar ad;int dakai=0;
int fazhi=50,zhuangtai=0,tixing=0;//0 自动 1 手动
uchar code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};
void shangdian();
void delay1ms(int ms);
void keypros();
void display1();
void display2();

main()
{
	shangdian();
  xie();
	fazhi=readeeprom(2);
	delay1ms(5);
	if(fazhi==0)
	{
	xieeeprom(2,50);
		delay1ms(5);		
	fazhi=readeeprom(1);
		delay1ms(5);
	}
	
	P2=0X80;P0=~(0X01);
	while(1)
	{ ad=getad(0x03);
	  ad=ad*0.39;
		read1302();
		
	   keypros();
		if(a%2==0){
		yi=shizhong[2]/10;er=shizhong[2]%10;san=10;
		si=shizhong[1]/10;wu=shizhong[1]%10;liu=11;
		qi=ad/10;ba=ad%10;
		}
		 display1();
		 display2();

	if((zhuangtai%2==0)&&(a%2==0))//自动
		{
	if(ad<fazhi)
	{P2=0XA0;P0=0X10;
	}
	else
	{
		P2=0XA0;P0=0X00;
	}
}
	if(zhuangtai%2==1)//手动
	{
	if((dakai==1)&&(ad<fazhi)&&(tixing%2==0))//打开提醒
	 {P2=0XA0;P0=0X50;}
	 else if((dakai==2)&&(ad<fazhi)&&(tixing%2==0))//打开提醒
	 {P2=0XA0;P0=0X40;}	 
	 else if((dakai==1)&&(ad>=fazhi)&&(tixing%2==0))//打开提醒
	 {P2=0XA0;P0=0X10;}
	 else if((dakai==2)&&(ad>=fazhi)&&(tixing%2==0))//打开提醒
	 {P2=0XA0;P0=0X00;}
   else if((dakai==1)&&(tixing%2==1))//打开提醒
	 {P2=0XA0;P0=0X10;}	
	  else if((dakai==2)&&(tixing%2==1))//打开提醒
	 {P2=0XA0;P0=0X00;}
  
	 else	if((ad<fazhi)&&(tixing%2==0))//打开提醒
	{
		P2=0XA0;P0=0X40;
	}	
	else if((ad<fazhi)&&(tixing%2==1))//关闭提醒
	{
		P2=0XA0;P0=0X00;
	}	
	else if((ad>=fazhi)&&(tixing%2==0))//关闭提醒
	{
		P2=0XA0;P0=0X00;
	}	
}
}
}
void shangdian()
{
	P2=0XA0;P0=0X00;
	P2=0X80;P0=0XFF;
	P2=0XC0;P0=0XFF;
	P2=0XE0;P0=0XFF;
}
void delay1ms(int ms)
{
	int i;
	for(;ms>0;ms--)
	{
		for(i=845;i>0;i--) ;
	}
}
void keypros()
{
	if(s7==0)
	{
		delay1ms(5);
		if(s7==0)
		{
			zhuangtai++;
			if(zhuangtai%2==0)
			{
				P2=0X80;P0=~(0X01);
			}
			if(zhuangtai%2==1)
			{
				P2=0X80;P0=~(0X02);
			}		
		}
		while(!s7) ;
		
	}
	if(s6==0)
	{
		delay1ms(5);
		if(s6==0)
		{	
			if(zhuangtai%2==0)
			{  a++;//1 进入调整
				yi=er=10;san=si=wu=liu=11;qi=fazhi/10;ba=fazhi%10;
					P2=0XA0;P0=0X00;     
			}
			if((a%2==0)&&(zhuangtai%2==0))
			{
			xieeeprom(2,fazhi);
				delay1ms(5);
			}
			if(zhuangtai%2==1)
			{tixing++;				
			}
		}
		while(!s6) ;		
	}
	if(s5==0)
	{
		delay1ms(5);
		if(s5==0)
		{
			if((zhuangtai%2==0)&&(a%2==1))
			{
				fazhi++;
				qi=fazhi/10;ba=fazhi%10;
				if(fazhi==99)
					fazhi=98;
			}
			if(zhuangtai%2==1) //手动
			{ 
				dakai=1}
			
		}
		while(!s5) ;
		
	}
	if(s4==0)
	{
		delay1ms(5);
		if(s4==0)
		{
			if((zhuangtai%2==0)&&(a%2==1))
			{
				fazhi--;
				qi=fazhi/10;ba=fazhi%10;
				if(fazhi==0)
					fazhi=1;

			}
			if(zhuangtai%2==1) //手动
			{
				dakai=2;
			}
		}
		while(!s4) ;
	}}
void display1()
{
	P2=0XC0;P0=0X01;
	P2=0XE0;P0=smg[yi];
	delay1ms(1);
	P2=0XC0;P0=0X02;
	P2=0XE0;P0=smg[er];
	delay1ms(1);
	P2=0XC0;P0=0X04;
	P2=0XE0;P0=smg[san];
	delay1ms(1);
	P2=0XC0;P0=0X08;
	P2=0XE0;P0=smg[si];
	delay1ms(1);
}
void display2()
{
	P2=0XC0;P0=0X10;
	P2=0XE0;P0=smg[wu];
	delay1ms(1);
	P2=0XC0;P0=0X20;
	P2=0XE0;P0=smg[liu];
	delay1ms(1);
	P2=0XC0;P0=0X40;
	P2=0XE0;P0=smg[qi];
	delay1ms(1);
	P2=0XC0;P0=0X80;
	P2=0XE0;P0=smg[ba];
	delay1ms(1);
	P2=0XC0;P0=0X80;
	P2=0XE0;P0=0xff;
}

你可能感兴趣的:(第四届蓝桥杯单片机设计与开发项目省赛试题)