標(biāo)題:
單片機(jī)模擬智能灌溉系統(tǒng)程序
[打印本頁]
作者:
17803856022
時(shí)間:
2020-10-12 13:18
標(biāo)題:
單片機(jī)模擬智能灌溉系統(tǒng)程序
main.c
#include <STC15F2K60S2.h>
#include "ds1302.h"
#include "iic.h"
#define uchar unsigned char
#define uint unsigned int
#define Y4 P2=(P2&0x1f)|0x80;
#define Y5 P2=(P2&0x1f)|0xa0;
#define Y6 P2=(P2&0x1f)|0xc0;
#define Y7 P2=(P2&0x1f)|0xe0;
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
uchar yi,er,san,si,wu,liu,qi,ba;
uchar num;
uint shidu;
bit mode; //初始,自動(dòng)模式
bit buzz_mode=1;
bit open_mode;
bit S6,S5,S4;
bit flag;
uchar size=50;
uchar temp;
extern uchar hour,min,s;
void allinit(void);
void display1(uchar yi,uchar er);
void display2(uchar san,uchar si);
void display3(uchar wu,uchar liu);
void display4(uchar qi,uchar ba);
void keyscan();
void keyscan16();
void delayms(int ms) //延時(shí)函數(shù)
{
int i,j;
for(i=ms;i>0;i--)
for(j=845;j>0;j--);
}
void main()
{
allinit();
DS1302_init(0x08,0x30,0x50);
while(1)
{
shidu= pcf8591(3);
shidu=shidu*99/255;
keyscan(); //獨(dú)立按鍵
//keyscan16(); //矩陣按鍵
DS1302_read();
display1(yi,er);
display2(san,si);
display3(wu,liu);
display4(qi,ba);
yi=hour/16;er=hour%16;san=11;si=min/16;
wu=min%16;liu=10;qi=shidu/10;ba=shidu%10;
if(mode==0) //自動(dòng)模式
{
Y4;P0=0xfe;
if(shidu < size)
{
Y5;P0=0x10;
}
if(shidu > size)
{
Y5;P0=0x00;
}
if(S6==1)
{
yi=11;er=11;san=10;si=10;
wu=temp/10;liu=temp%10;qi=size/10;ba=size%10;
temp=e2prom_read(0);
}
}
if(mode==1) //手動(dòng)模式
{
Y4;P0=0xfd;
if(buzz_mode==1 && open_mode==1)
{
if(shidu < size) {Y5;P0=0x50;}
if(shidu >= size) {Y5;P0=0x10;}
}
if(buzz_mode==1 && open_mode==0)
{
if(shidu < size) {Y5;P0=0x40;}
if(shidu >= size) {Y5;P0=0x00;}
}
if(buzz_mode==0 && open_mode==1)
{
Y5;P0=0x10;
}
if(buzz_mode==0 && open_mode==0)
{
Y5;P0=0x00;
}
}
}
}
void allinit(void)
{
Y5;P0=0x00; //關(guān)閉蜂鳴器和繼電器
Y6;P0=0xff; //打開數(shù)碼管位選
Y7;P0=0xff; //關(guān)閉數(shù)碼管段選
Y4;P0=0xff; //關(guān)閉led燈
}
void display1(uchar yi,uchar er) //數(shù)碼管顯示
{
Y6;P0=0x01;Y7;P0=tab[yi];delayms(1);
Y6;P0=0x02;Y7;P0=tab[er];delayms(1);
}
void display2(uchar san,uchar si)
{
Y6;P0=0x04;Y7;P0=tab[san];delayms(1);
Y6;P0=0x08;Y7;P0=tab[si];delayms(1);
}
void display3(uchar wu,uchar liu)
{
Y6;P0=0x10;Y7;P0=tab[wu];delayms(1);
Y6;P0=0x20;Y7;P0=tab[liu];delayms(1);
}
void display4(uchar qi,uchar ba)
{
Y6;P0=0x40;Y7;P0=tab[qi];delayms(1);
Y6;P0=0x80;Y7;P0=tab[ba];delayms(1);
}
void keyscan(void) //獨(dú)立按鍵處理
{
if(P30==0)
{
delayms(5);
if(P30==0)
{
mode=~mode;
}while(!P30);
}
else if(P31==0)
{
delayms(5);
if(P31==0)
{
if(mode==0)
{
S6=~S6;
if(S6==1)
{e2prom_write(0,size);}
}
if(mode==1)
{
buzz_mode = ~buzz_mode;
}while(!P31);
}
}
else if(P32==0)
{
delayms(5);
if(P32==0)
{
if(mode==0) {size=size+1;}
if(mode==1) {open_mode=1;}
}while(!P32);
}
else if(P33==0)
{
delayms(5);
if(P33==0)
{
if(mode==0) {size=size-1;}
if(mode==1) {open_mode=0;}
}while(!P33);
}
}
void keyscan16() //矩陣按鍵
{
uchar temp;
P44=0;P42=1;P3=0X7F;
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0x7e: num=0;break;
case 0x7d: num=4;break;
case 0x7b: num=8;break;
case 0x77: num=12;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0X0F;
}
}
}
P44=1;P42=0;P3=0XBF;
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xBe: num=1;break;
case 0xBd: num=5;break;
case 0xBb: num=9;break;
case 0xB7: num=13;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0X0F;
}
}
}
P3=0XDF;P44=1;P42=1;
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xDe: num=2;break;
case 0xDd: num=6;break;
case 0xDb: num=10;break;
case 0xD7: num=14;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0X0F;
}
}
}
P3=0XEF;P44=1;P42=1;
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0X0F;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xEe: num=3;break;
case 0xEd: num=7;break;
case 0xEb: num=11;break;
case 0xE7: num=15;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0X0F;
}
}
}
}
復(fù)制代碼
iic.c
/*
程序說明: IIC總線驅(qū)動(dòng)程序
軟件環(huán)境: Keil uVision 4.10
硬件環(huán)境: CT107單片機(jī)綜合實(shí)訓(xùn)平臺(tái) 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#include "iic.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_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; /* 數(shù)據(jù)線 */
sbit SCL = P2^0; /* 時(shí)鐘線 */
//總線啟動(dòng)條件
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;
}
//應(yīng)答位控制
void IIC_Ack(bit ackbit)
{
if(ackbit)
{
SDA = 0;
}
else
{
SDA = 1;
}
somenop;
SCL = 1;
somenop;
SCL = 0;
SDA = 1;
somenop;
}
//等待應(yīng)答
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通過I2C總線發(fā)送數(shù)據(jù)
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總線上接收數(shù)據(jù)
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;
}
uchar pcf8591(uchar addr)
{
uchar dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_Stop();
return dat;
}
void e2prom_write(uchar addr,uchar dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
uchar e2prom_read(uchar addr)
{
uchar dat;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_Stop();
return dat;
}
復(fù)制代碼
iic.h
#ifndef _IIC_H
#define _IIC_H
#define uchar unsigned char
#define uint unsigned int
//函數(shù)聲明
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);
uchar pcf8591(uchar addr);
void e2prom_write(uchar addr,uchar dat);
uchar e2prom_read(uchar addr);
#endif
復(fù)制代碼
ds1302.c
/*
程序說明: DS1302驅(qū)動(dòng)程序
軟件環(huán)境: Keil uVision 4.10
硬件環(huán)境: CT107單片機(jī)綜合實(shí)訓(xùn)平臺(tái) 8051,12MHz
日 期: 2011-8-9
*/
#include <reg52.h>
#include "ds1302.h"
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302復(fù)位
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 )
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=0;
}
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,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);
}
void DS1302_init(uchar hour,uchar min,uchar s)
{
Write_Ds1302(0x84,hour);
Write_Ds1302(0x82,min);
Write_Ds1302(0x80,s);
}
uchar hour,min,s;
void DS1302_read()
{
hour=Read_Ds1302(0x85);
min= Read_Ds1302(0x83);
s= Read_Ds1302(0x81);
}
復(fù)制代碼
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
#define uchar unsigned char
#define uint unsigned int
void Write_Ds1302_Byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void DS1302_read();
void DS1302_init(uchar hour,uchar min,uchar s);
#endif
復(fù)制代碼
作者:
51hei團(tuán)團(tuán)
時(shí)間:
2020-10-12 22:23
電路圖能分享一下嗎?
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1