模拟灌溉系统

1.题目

在这里插入图片描述在这里插入图片描述

2.代码

main.c

******************************************************************************
* 文件名:模拟灌溉系统
* 描  述:
* 作  者:思索与猫
* 日  期:  19/3/12
* 备  注:
*         
******************************************************************************
#include<stc15f2k60s2.h>
#include<ds1302.h>
#include<iic.h>
typedef unsigned char uchar;
typedef unsigned int uint;
	
sbit buzz = P0^6;
sbit relay = P0^4;

uchar code duan[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x040};//10black,11-
uchar dispbuff[8];
uchar shi,fen,humid,valve = 50;     //时,分 ,湿度,阈值
uchar trg,cont;      //KEY
bit mode = 0, view = 0, buzz_flag = 0 , relay_flag = 0, warn_flag = 1;
//function
void CloseFucker();
void Timer0Init();
void Display();
void Show_Led_Number();
void Read();
void KeyScan();
void KeyAction();
void Fucker();

void main()
{
		Timer0Init();
		CloseFucker();
		init_pcf859();
		SetTime(8,30,00);
		valve = read_eeprom(0x55);     //读取eeprom里的阈值
		while(1)
		{
				Read();  
				KeyScan();
				KeyAction();
				Show_Led_Number();
				Fucker();
		}
}

void T0_time() interrupt 1
{
		Display();                     //显示
		
		if(mode == 0)               //LED
		{
				P2 = P2&0x1f|0x80;
				P0 = 0xfe;                 //L1亮
				P2 = P2&0x1f;
		}
		else if(mode == 1)
		{
				P2 = P2&0x1f|0x80;
				P0 = 0xfd;            		//L1亮
				P2 = P2&0x1f;
		}
}

void Fucker()                 //继电器,蜂鸣器
{
		if(buzz_flag)
		{
				if(relay_flag)
				{
						P2 = P2&0x1f|0xa0;
						buzz = 1;relay = 1;
						P2 = P2&0x1f;
				}
				else
				{
						P2 = P2&0x1f|0xa0;
						buzz = 1;relay = 0;
						P2 = P2&0x1f;
				}
		}
		else 
		{
				if(relay_flag)
				{
						P2 = P2&0x1f|0xa0;
						buzz = 0;relay = 1;
						P2 = P2&0x1f;
				}
				else
				{
						P2 = P2&0x1f|0xa0;
						buzz = 0;relay = 0;
						P2 = P2&0x1f;
				}
		}
}

void KeyAction()
{
		if(trg == 0x01)       //按下S7,切换模式
		{
				mode = ~mode;
				CloseFucker();
				view = 0;
		}
		else if(mode == 0&&trg == 0x02)          //按下S6,切换界面
		{
				view = ~view;
		}
		else if(mode == 0&&view == 1&&trg == 0x04)        //阈值+1
		{
				if(++valve == 99) valve = 99; 
				write_eeprom(valve,0x55);
		}
		else if(mode == 0&&view == 1&&trg == 0x08)        //阈值-1
		{
				if(--valve == 0) valve = 0; 
				write_eeprom(valve,0x55);
		}
		else if(mode == 1&&trg == 0x02)          //警告开关
		{
				warn_flag = ~warn_flag;
		}
		else if(mode == 1&&trg == 0x04)         //继电器亮
		{
				relay_flag = 1;
		}
		else if(mode == 1&&trg == 0x08)         //继电器灭
		{
				relay_flag = 0;
		}
		
}
 
void KeyScan()        //三行独立按键
{
		uchar ReadData = P3^0xff;
		trg = ReadData&(ReadData^cont);
		cont = ReadData;
}

void Read()
{
		shi = Ds1302_Single_Byte_Read(0x85);       //读时间
		Ds1302_Single_Byte_Write(0x00,0x00);
		fen = Ds1302_Single_Byte_Read(0x83);
		Ds1302_Single_Byte_Write(0x00,0x00);	
		
		humid = read_pcf859();               //读湿度,转换
		humid = humid*99/255.0f;
	
		if(warn_flag == 1 && mode == 1&&humid < valve)
		{
				buzz_flag = 1;                      //蜂鸣器
		}
		else
		{
				buzz_flag = 0;
		}
		
		if(mode == 0&&humid < valve)
		{
				relay_flag = 1;           //继电器
		}
		else if(mode == 0&&humid >= valve)
		{
				relay_flag = 0;
		}
}
	
void Show_Led_Number()
{

		if(view == 0)
		{			
				dispbuff[0] = shi/16;
				dispbuff[1] = shi%16;
				dispbuff[2] = 11;
				dispbuff[3] = fen/16;
				dispbuff[4] = fen%16;
				dispbuff[5] = 10;
				dispbuff[6] = humid/10;
				dispbuff[7] = humid%10;
		}
		else if(mode == 0&&view == 1)
		{
				dispbuff[0] = 10;
				dispbuff[1] = 10;
				dispbuff[2] = 10;
				dispbuff[3] = 10;
				dispbuff[4] = 10;
				dispbuff[5] = 11;
				dispbuff[6] = valve/10;
				dispbuff[7] = valve%10;
		}		
}
	
void Display()
{
		static uchar index = 0;
		P2 = P2&0x1f|0xe0;
		P0 = 0xff;
		P2 = P2&0x1f;
	
		P2 = P2&0x1f|0xc0;
		P0 = 1<<index;
		P2 = P2&0x1f;
	
		P2 = P2&0x1f|0xe0;
		P0 = ~duan[dispbuff[index]];
		P2 = P2&0x1f;
	
		index++;
		index &= 0x07;
}

void Timer0Init()		//2ms@12.000MHz
{
		AUXR |= 0x80;		
		TMOD &= 0xF0;		
		TL0 = 0x40;		
		TH0 = 0xA2;		
		TF0 = 0;		
		TR0 = 1;	
		ET0 = 1;
		EA = 1;
}

void CloseFucker()
{
		P2 = P2&0x1f|0xa0;
		P0 = 0xaf;
		P2 = P2&0x1f;
}

iic.h

#ifndef _IIC_H
#define _IIC_H

#include "stc15f2k60s2.h"
#include "intrins.h"

#define somenop Delay5us()
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1


sbit SDA = P2^1; 
sbit SCL = P2^0;  


void IIC_Start(void); 
void IIC_Stop(void);  
void IIC_Ack(unsigned char ackbit); 
void IIC_SendByte(unsigned char byt); 
bit IIC_WaitAck(void);  
unsigned char IIC_RecByte(void); 
void Delay5us();
void write_eeprom(unsigned char dat,unsigned char add);
unsigned char read_eeprom(unsigned char add);
void init_pcf859();
unsigned char read_pcf859();
#endif

iic.c

#include "iic.h"
void Delay5us()		//@12.000MHz
{
	unsigned char i;

	_nop_();
	_nop_();
	i = 12;
	while (--i);
}

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(unsigned char 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;
	}
}

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;
	}
}

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;
}
void write_eeprom(unsigned char dat,unsigned char add)      //写eeprom
{
		IIC_Start();
		IIC_SendByte(0xa0);
		IIC_WaitAck();
		IIC_SendByte(add);
		IIC_WaitAck();
		IIC_SendByte(dat);
		IIC_WaitAck();
		IIC_Stop();
}
unsigned char read_eeprom(unsigned char add)              //读eeprom
{
		unsigned char dat;
		IIC_Start();
		IIC_SendByte(0xa0);
		IIC_WaitAck();
		IIC_SendByte(add);
		IIC_WaitAck();
	
		IIC_Start();
		IIC_SendByte(0xa1);
		IIC_WaitAck();
		dat = IIC_RecByte();
		IIC_Ack(0);
		IIC_Stop();
		return dat;
}
void init_pcf859()                            //pcf859初始化
{
		IIC_Start();
		IIC_SendByte(0x90);
		IIC_WaitAck();
		IIC_SendByte(0x03);
		IIC_WaitAck();
		IIC_Stop();
}
unsigned char read_pcf859()           //读取AD值
{
		unsigned char dat;
		IIC_Start();
		IIC_SendByte(0x91);
		IIC_WaitAck();
		dat = IIC_RecByte();
		IIC_Ack(0);
		IIC_Stop();
		return dat;
}

ds1302.h

#ifndef  __DS1302_H__
#define  __DS1302_H__

#include<stc15f2k60s2.h>
#include<intrins.h>
/********************************************************************/ 
sbit SCK=P1^7;		
sbit SD=P2^3;		
sbit RST=P1^3;
/********************************************************************/ 
#define RST_CLR	RST=0	
#define RST_SET	RST=1	

#define SDA_CLR	SD=0	
#define SDA_SET	SD=1	
#define SDA_R	SD	

#define SCK_CLR	SCK=0	
#define SCK_SET	SCK=1	
/********************************************************************/ 
#define ds1302_sec_addr			0x80		
#define ds1302_min_addr			0x82		
#define ds1302_hr_addr			0x84		
#define ds1302_date_addr		0x86		
#define ds1302_month_addr		0x88		
#define ds1302_day_addr			0x8A		
#define ds1302_year_addr		0x8C		

#define ds1302_control_addr		0x8E		
#define ds1302_charger_addr		0x90 				 
#define ds1302_clkburst_addr	0xBE		

extern void Write_Ds1302_Byte(unsigned char dat);
extern unsigned char Read_Ds1302_Byte(void);
  


extern void Ds1302_Single_Byte_Write(unsigned char addr, unsigned char dat);

extern unsigned char Ds1302_Single_Byte_Read(unsigned char addr);
void SetTime(unsigned char shi,unsigned char fen,unsigned char miao);
#endif	 


ds1302.c

#include "ds1302.h"
/********************************************************************/ 
void Write_Ds1302_Byte(unsigned char dat) 
{
	unsigned char i;
	SCK = 0;
	for (i=0;i<8;i++) 
	{ 
		if (dat & 0x01) 	
		{
			SDA_SET;		
		}
		else 
		{
			SDA_CLR;		
		}		 
		SCK_SET;
		SCK_CLR;		
		dat = dat >> 1; 
	} 
}
/********************************************************************/ 
unsigned char Read_Ds1302_Byte(void) 
{
	unsigned char i, dat=0;	
	for (i=0;i<8;i++)
	{	
		dat = dat >> 1;
		if (SDA_R) 	 
		{
			dat |= 0x80;
		}
		else 
		{
			dat &= 0x7F;
		}
		SCK_SET;
		SCK_CLR;
	}
	return dat;
}

void Ds1302_Single_Byte_Write(unsigned char addr, unsigned char dat)
{ 

	RST_CLR;			
	SCK_CLR;			

	RST_SET;			
	addr = addr & 0xFE;	 
	Write_Ds1302_Byte(addr);
	Write_Ds1302_Byte(dat);	 
	RST_CLR;				
}

/********************************************************************/ 
unsigned char Ds1302_Single_Byte_Read(unsigned char addr) 
{ 
	unsigned char temp;
	RST_CLR;			
	SCK_CLR;			

	RST_SET;	
	addr = addr | 0x01;	 
	Write_Ds1302_Byte(addr);
	temp=Read_Ds1302_Byte(); 
	return temp;
}
void SetTime(unsigned char shi,unsigned char fen,unsigned char miao)   //设置时分秒
{
	Ds1302_Single_Byte_Write(0x8e,0x00);
	Ds1302_Single_Byte_Write(0x80,(miao/10)*16+miao%10);
	Ds1302_Single_Byte_Write(0x82,(fen/10)*16+fen%10);
	Ds1302_Single_Byte_Write(0x84,(shi/10)*16+shi%10);
	Ds1302_Single_Byte_Write(0x8e,0x80);
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值