蓝桥杯单片机省赛 第四届模拟智能灌溉系统

main.c

//如果在自动模式下按s6出现乱码,是因为eeprom数据变了,把数据重新写进去就可以了;



#include "main.h"
uchar h,m,s;
uchar shidu;
bit s7;
bit s6;
bit relay_flag;

uchar shidu_val;//湿度阈值
bit time_flag;
bit shidu_flag;

void Timer0Init(void)		//2毫秒@11.0592MHz
{
	AUXR |= 0x80;		//定时器时钟1T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x9A;		//设置定时初值
	TH0 = 0xA9;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	EA = 1;
	ET0 = 1;
}

void key()//按键
{
	if(P30 == 0)
	{
		Delay5ms();
		if(P30 == 0)
		{
			s6 = 0;
			relay_flag = 0;
			s7 = ~s7;
		}
		while(!P30);
	}
	else if(P31 == 0)
	{
		Delay5ms();
		if(P31 == 0)
		{s6 = ~s6;}
		while(!P31);
	}
	else if(P32 == 0)
	{
		Delay5ms();
		if(P32 == 0)
		{
			if(s6 == 1)
			{
				shidu_val++;
				if(shidu_flag == 100)
					shidu_flag = 0;
			}
			relay_flag = 1;
		}
		while(!P32);
	}
	else if(P33 == 0)
	{
		Delay5ms();
		if(P33 == 0)
		{
			if(s6 == 1)
			{
				shidu_val--;
				if(shidu_flag == 0)
					shidu_flag = 99;
			}
			relay_flag = 0;
		}
		while(!P33);
	}
}

void ds1302_read()
{
	h = Read_Ds1302_Byte(0x85);
	m = Read_Ds1302_Byte(0x83);
	s = Read_Ds1302_Byte(0x81);
}

void main()
{
	Y4; P0 = 0xff;
	Y5; P0 = 0x00;
	ds1302_wirte();
	//at24c02_wirte(50);上电初始值;出现乱码使用的
	shidu_val = at24c02_read(1);
	Delay5ms();
	Timer0Init();
	while(1)
	{
		key();
		if(time_flag == 1){ds1302_read();time_flag = 0;}
		if(shidu_flag == 1){shidu = AD_read(0x03);shidu_flag = 0;}
		if(s7 == 0)
		{
			if(s6 == 0)
			{
				smg_display(h/10,h%10,11,m/10,m%10,11,shidu%100/10,shidu%10);
				if(shidu_flag == 0)
				{at24c02_wirte(shidu_val);}//eeprom和ad共用iic总线,所以,轮流使用;
			}
			else if(s6 == 1)
			{
				smg_display(11,11,10,10,10,10,shidu_val/10,shidu_val%10);
			}
		}
	}
}

void Timer0() interrupt 1
{	
	uchar ms;
	smg();
	ms++;
	if(ms%10 == 0){time_flag = 1;ms = 0;}
	if(ms%5 == 0){shidu_flag = 1;}
	if(s7 == 0)//自动工作状态
	{
		if(shidu<shidu_val){Y5; P0 = 0x10;}
		else {Y5; P0 = 0x00;}
	}
	else if(s7 == 1)//手动工作状态
	{
		if(s6 == 0)
		{
			if(relay_flag == 0)
			{
				if(shidu<shidu_val){Y5; P0 = 0x40;}
				else {Y5; P0 = 0x00;}
			}
			else if(relay_flag == 1)
			{
				if(shidu<shidu_val){Y5; P0 = 0x50;}
				else {Y5; P0 = 0x10;}
			}
		}
		if(s6 == 1)
		{
			if(relay_flag == 1)
			{
				Y5; P0 = 0x10;
			}
			else if(relay_flag == 0)
			{
				Y5; P0 = 0x00;
			}
		}
	}
}

main.h

#ifndef _MAIN_H
#define _MAIN_H

#include "common.h"
#include "smg.h"
#include "ds1302.h"
#include "iic.h"
#endif

common.c//公用的一些延时

#include "common.h"

void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 54;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}

common.h

#ifndef _COMMON_H
#define _COMMON_H

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

#define uchar unsigned char
#define uint unsigned int

#define Y4 P2 = (P2&0x1f)|0x9f
#define Y5 P2 = (P2&0x1f)|0xaf
#define Y6 P2 = (P2&0x1f)|0xcf
#define Y7 P2 = (P2&0x1f)|0xef
#define Y0 P2 = (P2&0x1f)|0x1f

void Delay5ms();

#endif```

# ds1302.c

```c
/*
  程序说明: DS1302驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include <ds1302.h>

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302复位												

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

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

unsigned char Read_Ds1302_Byte ( unsigned char address )
{
 	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	temp = (temp/16)*10 + temp%16;
	return (temp);			
}

void ds1302_wirte()
{
	Write_Ds1302_Byte(0x84,8);
	Write_Ds1302_Byte(0x82,30);
	Write_Ds1302_Byte(0x80,0);
}


ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

#include "common.h"

void ds1302_wirte();
unsigned char Read_Ds1302_Byte ( unsigned char address );

#endif

iic.c

/*
  程序说明: IIC总线驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include "iic.h"

#define DELAY_TIME 5

#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1

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

void IIC_Delay(unsigned char i)
{
    do{_nop_();}
    while(i--);        
}
//总线启动条件
void IIC_Start(void)
{
    SDA = 1;
    SCL = 1;
    IIC_Delay(DELAY_TIME);
    SDA = 0;
    IIC_Delay(DELAY_TIME);
    SCL = 0;	
}

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

//发送应答
//void IIC_SendAck(bit ackbit)
//{
//    SCL = 0;
//    SDA = ackbit;  					// 0:应答,1:非应答
//    IIC_Delay(DELAY_TIME);
//    SCL = 1;
//    IIC_Delay(DELAY_TIME);
//    SCL = 0; 
//    SDA = 1;
//    IIC_Delay(DELAY_TIME);
//}

//等待应答
bit IIC_WaitAck(void)
{
    bit ackbit;
	
    SCL  = 1;
    IIC_Delay(DELAY_TIME);
    ackbit = SDA;
    SCL = 0;
    IIC_Delay(DELAY_TIME);
    return ackbit;
}

//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
    unsigned char i;

    for(i=0; i<8; i++)
    {
        SCL  = 0;
        IIC_Delay(DELAY_TIME);
        if(byt & 0x80) SDA  = 1;
        else SDA  = 0;
        IIC_Delay(DELAY_TIME);
        SCL = 1;
        byt <<= 1;
        IIC_Delay(DELAY_TIME);
    }
    SCL  = 0;  
}

//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
    unsigned char i, da;
    for(i=0; i<8; i++)
    {   
    	SCL = 1;
	IIC_Delay(DELAY_TIME);
	da <<= 1;
	if(SDA) da |= 1;
	SCL = 0;
	IIC_Delay(DELAY_TIME);
    }
    return da;    
}
uint AD_read(uchar add)
{
	uchar dat;
	IIC_Start();
	IIC_SendByte(0x90);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0x91);
	IIC_WaitAck();
	dat = IIC_RecByte();
	IIC_WaitAck();
	IIC_Stop();
	
	dat = dat/2.56;
	return dat;
}

void at24c02_wirte(uchar dat)
{
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(1);
	IIC_WaitAck();
	IIC_SendByte(dat);
	IIC_WaitAck();
	IIC_Stop();
}

uchar at24c02_read(uchar add)
{
	uchar dat;
	EA = 0;
	IIC_Start();
	IIC_SendByte(0xa0);
	IIC_WaitAck();
	IIC_SendByte(add);
	IIC_WaitAck();
	IIC_Stop();
	
	IIC_Start();
	IIC_SendByte(0xa1);
	IIC_WaitAck();
	dat = IIC_RecByte();
	IIC_WaitAck();
	IIC_Stop();
	EA = 1;
	return dat;
}

iic.h

#ifndef _IIC_H
#define _IIC_H

#include "common.h"

uint AD_read(uchar add);
void at24c02_wirte(uchar dat);
uchar at24c02_read(uchar add);

#endif```

# smg.c

```c
#include "smg.h"

uchar code smg_dula[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
uchar code smg_wela[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar smg_display_buff[] = {1,2,3,4,5,6,7,8};

void smg_display(uchar d0,d1,d2,d3,d4,d5,d6,d7)
{
	smg_display_buff[0] = d0;
	smg_display_buff[1] = d1;
	smg_display_buff[2] = d2;
	smg_display_buff[3] = d3;
	smg_display_buff[4] = d4;
	smg_display_buff[5] = d5;
	smg_display_buff[6] = d6;
	smg_display_buff[7] = d7;
}

void smg()
{
	uchar num;
	Y6; P0 = smg_wela[num];
	Y7; P0 = smg_dula[smg_display_buff[num]];
	Y0; P0 = 0xff;
	num++;
	if(num == 8)
	{
		num = 0;
	}
}

smg.h

#ifndef _SMG_H
#define _SMG_H

#include "common.h"

void smg();
void smg_display(uchar d0,d1,d2,d3,d4,d5,d6,d7);
	
#endif

小结

欢迎各位参加蓝桥杯单片机的小伙伴,能够指出我写的不好的地方,同时也感谢小破站逐日追影和小蜜蜂老师,我是从他们的视频开始学习的,我不推荐购买淘宝的课程。也希望各位同学可以一起交流,我的qq:2509834026

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值