单片机比赛准备07-蓝桥杯-第五届初赛模拟题(模拟智能灌溉装置)

简介: 单片机比赛准备07-蓝桥杯-第五届初赛模拟题(模拟智能灌溉装置)

image.png

image.png

image.png

image.png

iic.h:


#ifndef _IIC_H#define _IIC_H#include "intrins.h"#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}#define SlaveAddrW 0xA0#define SlaveAddrR 0xA1//总线引脚定义sbitSDA=P2^1;  /*数据线*/sbitSCL=P2^0;  /*时钟线*///函数声明voidIIC_Start(void); 
voidIIC_Stop(void);  
voidIIC_Ack(unsignedcharackbit); 
voidIIC_SendByte(unsignedcharbyt); 
bitIIC_WaitAck(void);  
unsignedcharIIC_RecByte(void); 
voideeprom_write(unsignedcharadd,unsignedchardat);
unsignedchareeprom_read(unsignedcharadd);
unsignedcharad_read(unsignedcharadd);
#endif

iic.c:


/*程序说明: IIC总线驱动程序软件环境: KeiluVision4.10硬件环境: CT107单片机综合实训平台8051,12MHz: 2011-8-9*/#include "stc15f2k60s2.h"#include "intrins.h"#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}    #define SlaveAddrW 0xA0#define SlaveAddrR 0xA1//总线引脚定义sbitSDA=P2^1;  /*数据线*/sbitSCL=P2^0;  /*时钟线*///总线启动条件voidIIC_Start(void)
{
SDA=1;
SCL=1;
somenop;
SDA=0;
somenop;
SCL=0;    
}
//总线停止条件voidIIC_Stop(void)
{
SDA=0;
SCL=1;
somenop;
SDA=1;
}
//应答位控制voidIIC_Ack(bitackbit)
{
if(ackbit) 
    {   
SDA=0;
    }
else    {
SDA=1;
    }
somenop;
SCL=1;
somenop;
SCL=0;
SDA=1; 
somenop;
}
//等待应答bitIIC_WaitAck(void)
{
SDA=1;
somenop;
SCL=1;
somenop;
if(SDA)    
    {   
SCL=0;
IIC_Stop();
return0;
    }
else    { 
SCL=0;
return1;
    }
}
//通过I2C总线发送数据voidIIC_SendByte(unsignedcharbyt)
{
unsignedchari;
for(i=0;i<8;i++)
    {   
if(byt&0x80) 
        {   
SDA=1;
        }
else        {
SDA=0;
        }
somenop;
SCL=1;
byt<<=1;
somenop;
SCL=0;
    }
}
//从I2C总线上接收数据unsignedcharIIC_RecByte(void)
{
unsignedcharda;
unsignedchari;
for(i=0;i<8;i++)
    {   
SCL=1;
somenop;
da<<=1;
if(SDA) 
da|=0x01;
SCL=0;
somenop;
    }
returnda;
}
voideeprom_write(unsignedcharadd,unsignedchardat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
unsignedchareeprom_read(unsignedcharadd)
{
unsignedchartemp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
returntemp;
}
unsignedcharad_read(unsignedcharadd)
{
unsignedchartemp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
returntemp;
}

ds1302.h:


#ifndef __DS1302_H#define __DS1302_HvoidWrite_Ds1302_Byte(unsignedchartemp);
voidWrite_Ds1302( unsignedcharaddress,unsignedchardat );
unsignedcharRead_Ds1302 ( unsignedcharaddress );
#endif

ds1302.c:


/*程序说明: DS1302驱动程序软件环境: KeiluVision4.10硬件环境: CT107单片机综合实训平台8051,12MHz: 2011-8-9*/#include <stc15f2k60s2.h>#include <intrins.h>sbitSCK=P1^7;      
sbitSDA=P2^3;      
sbitRST=P1^3;   //DS1302复位voidWrite_Ds1302_Byte(unsignedchartemp) 
{
unsignedchari;
for (i=0;i<8;i++)       
    { 
SCK=0;
SDA=temp&0x01;
temp>>=1; 
SCK=1;
    }
}   
voidWrite_Ds1302( unsignedcharaddress,unsignedchardat )     
{
unsignedcharnum;
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; 
}
unsignedcharRead_Ds1302 ( unsignedcharaddress )
{
unsignedchari,temp=0x00;
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;
    } 
RST=0;
_nop_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp);          
}

main.c:


#include "stc15f2k60s2.h"#include <iic.h>#include <ds1302.h>#include "intrins.h"//----------------------------------------#define uchar unsigned char #define uint unsigned int #define S7 P30#define S6 P31#define S5 P32#define S4 P33//----------------------------------------voidallinit();
voiddisplay(ucharyi,er,san,si,wu,liu,qi,ba);
voidkey();
voiddsinit();
voidreadtime();
voidDelayms(uintms);
voidcontrol();
voidTimer0Init(void);
//----------------------------------------uchards[]={50,29,8};
ucharshijian[2];
ucharseg[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0X82,0XF8,
0X80,0X90,0XFF,0xBF};//数码管显示值ucharyi=10,er=10,san=10,si=10,wu=10,liu=10,qi=10,ba=10;
ucharfazhi=50;
ucharzhuangtai=0;
uintshidu=0;
ucharbell_flag=0;
ucharshezhi=0;
ucharkai;
//----------------------------------------voidmain()
{
allinit();
dsinit();
eeprom_write(0x01,fazhi);
Timer0Init();
while(1)
    {
control();
display(yi,er,san,si,wu,liu,qi,ba);
    }
}
voidallinit()
{
P2=0xa0;P0=0x00;//关闭蜂鸣器继电器P2=0x80;P0=0xff;//关闭ledP2=0xe0;P0=0xff;//段选P2=0xc0;P0=0xff;//位选}
voiddisplay(ucharyi,er,san,si,wu,liu,qi,ba)
{   
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[yi];//段选P2=0xc0;P0=0x01;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[er];//段选P2=0xc0;P0=0x02;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[san];//段选P2=0xc0;P0=0x04;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[si];//段选P2=0xc0;P0=0x08;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[wu];//段选P2=0xc0;P0=0x10;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[liu];//段选P2=0xc0;P0=0x20;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[qi];//段选P2=0xc0;P0=0x40;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[ba];//段选P2=0xc0;P0=0x80;//位选Delayms(1);
}
voidkey()
{
if(S7==0)
    {
Delayms(5);
if(S7==0)
        {
if(zhuangtai==0)
zhuangtai=1;
elseif(zhuangtai==1)
zhuangtai=0;
        }
while(!S7)
        {
display(yi,er,san,si,wu,liu,qi,ba);
        };
    }
if(S6==0)
    {
Delayms(5);
if(S6==0)
        {
if(zhuangtai==0)
            {
shezhi=~(shezhi);
if(shezhi!=0)
fazhi=eeprom_read(0x01);
elseeeprom_write(0x01,fazhi);
            }
elseif(zhuangtai==1)
            {
bell_flag=~(bell_flag);
            }
        }
while(!S6)
        {
display(yi,er,san,si,wu,liu,qi,ba);
        };
    }
if(S5==0)
    {
Delayms(5);
if(S5==0)
        {
if(zhuangtai==0)
            {
if(shezhi!=0)
                {
fazhi+=1;
if(fazhi==100)
fazhi=0;
                }
            }
elseif(zhuangtai!=0)
            {
kai=1;
            }
        }
while(!S5)
        {
display(yi,er,san,si,wu,liu,qi,ba);
        };
    }
if(S4==0)
    {
Delayms(5);
if(S4==0)
        {
if(zhuangtai==0)
            {
if(shezhi!=0)
                {
fazhi-=1;
if(fazhi==0)
fazhi=99;
                }
            }
elseif(zhuangtai!=0)
            {
kai=0;
            }
        }
while(!S4)
        {
display(yi,er,san,si,wu,liu,qi,ba);
        };
    }
}
voiddsinit()
{
ucharadd=0x80;
uchari;
Write_Ds1302(0x8e,0x00);
for(i=0;i<3;i++)
    {
Write_Ds1302(add,ds[i]);
add=add+2;
    }
Write_Ds1302(0x8e,0x80);
}
voidreadtime()
{
ucharadd=0x83;
uchari;
Write_Ds1302(0x8e,0x80);
for(i=0;i<2;i++)
    {
shijian[i]=Read_Ds1302(add);
add=add+2;
    }
Write_Ds1302(0x8e,0x80);
}
voidcontrol()
{
if(shezhi!=0)
    {
yi=11;
er=11;
san=10;
si=10;
wu=10;
liu=10;
qi=fazhi/10%10;
ba=fazhi%10;
    }
else    {
yi=shijian[1]/16;
er=shijian[1]%16;
san=11;
si=shijian[0]/16;
wu=shijian[0]%16;
liu=10;
qi=shidu/10%10;
ba=shidu%10;
    }
if(zhuangtai==0)
    {
P2=0x80;P0=0xfe;//关闭ledshidu=(uint)((ad_read(0x03))*0.39);
if(shidu<fazhi)
        {
P2=0xa0;P0=0x10;
        }
else        {
P2=0xa0;P0=0x00;
        }       
    }
elseif(zhuangtai!=0)
    {
P2=0x80;P0=0xfd;//关闭ledshidu=(uint)((ad_read(0x03))*0.39);
if((shidu<fazhi)&&(bell_flag==0))
        {
P2=0xa0;P06=1;
        }
else        {
P2=0xa0;P06=0;
        }   
if(kai==1)
        {
P2=0xa0;P04=1;
        }
else        {
P2=0xa0;P04=0;
        }       
    }
}
voidDelayms(uintms)       //@11.0592MHz{
unsignedchari, j,k;
for(k=ms;k>0;k--)
    {
_nop_();
_nop_();
_nop_();
i=11;
j=190;
do        {
while (--j);
        } while (--i);
    }
}
voidTimer0Init(void)       //1毫秒@11.0592MHz{
AUXR|=0x80;       //定时器时钟1T模式TMOD&=0xF0;       //设置定时器模式TL0=0xCD;     //设置定时初值TH0=0xD4;     //设置定时初值TF0=0;        //清除TF0标志TR0=1;        //定时器0开始计时EA=1;
ET0=1;
}
voidtimer0() interrupt1{
readtime();
key();
}
目录
相关文章
|
15天前
|
传感器
毕业设计 基于51单片机的智能水表水流量计流量报警器温度设计
毕业设计 基于51单片机的智能水表水流量计流量报警器温度设计
|
15天前
|
传感器 物联网 芯片
毕业设计 基于STM32单片机无线ZIGBEE智能大棚土壤湿度光照检测
毕业设计 基于STM32单片机无线ZIGBEE智能大棚土壤湿度光照检测
|
15天前
|
物联网
STC51单片机-实验开发装置仿真-物联网应用系统设计
STC51单片机-实验开发装置仿真-物联网应用系统设计
66 0
|
15天前
|
语音技术 物联网 程序员
51单片机智能小车(循迹、跟随、避障、测速、蓝牙、wifie、4g、语音识别)总结-2
51单片机智能小车(循迹、跟随、避障、测速、蓝牙、wifie、4g、语音识别)总结-2
51单片机智能小车(循迹、跟随、避障、测速、蓝牙、wifie、4g、语音识别)总结-2
|
15天前
|
语音技术 传感器
51单片机智能小车(循迹、跟随、避障、测速、蓝牙、wifie、4g、语音识别)总结-1
51单片机智能小车(循迹、跟随、避障、测速、蓝牙、wifie、4g、语音识别)总结-1
51单片机智能小车(循迹、跟随、避障、测速、蓝牙、wifie、4g、语音识别)总结-1
|
15天前
|
传感器 算法 物联网
基于单片机的智能停车场管理系统的设计与实现_kaic
基于单片机的智能停车场管理系统的设计与实现_kaic
|
15天前
|
机器学习/深度学习 算法 Java
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-659 比赛安排
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-659 比赛安排
31 0
|
15天前
|
传感器 人工智能 前端开发
单片机毕业设计|农家菜园自动灌溉控制系统设计
单片机毕业设计|农家菜园自动灌溉控制系统设计