目录:
? ? ? ? ?一、题目内容
? ? ? ? ?二、程序代码(含驱动代码)
注意:驱动函数需要改动一下,觉得好一键三连。
?主函数:
#include "stc15f2k60s2.h"
#include "intrins.h"
#include "ds1302.h"
#include "iic.h"
#define control(x,y) P0=x;P2=y;P2=0;
typedef unsigned char uchar;
typedef unsigned int uint;
uchar code dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
uchar dsp_Time[]={10,10,11,10,10,10,10,10};
uchar dsp_Set[]={11,11,10,10,10,10,10,10};
uchar L1,L2;
uchar buzzer,relay; //继电器 开 灌溉
uchar cnt,trg;
uchar count_key; //按键刷新率
uchar h,m,s; //8时30分
uchar shidu;
uchar count_adc;
uchar shidu_th=50;
uchar f_mode=0; //工作模式切换
bit flag_set; //阈值调整界面标志
bit flag_beep=1; //蜂鸣器功能标志
void Delay300ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 13;
j = 156;
k = 83;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
ET0=1;
EA=1;
TR0 = 1; //定时器0开始计时
}
void Timer0_Service() interrupt 1
{
static uchar pos=0;
uchar P0_Keeper=0,P2_Keeper=0;
P0_Keeper=P0;P2_Keeper=P2;P2=0;
control(0x00,0xc0);
if(flag_set)
{
control(dsp_code[dsp_Set[pos]],0xe0);
}
else
{
control(dsp_code[dsp_Time[pos]],0xe0);
}
control(1 << pos ,0xc0);
if(++pos == 8)
pos=0;
control(~(L1+L2*2),0x80);
control(buzzer*64+relay*16,0xa0);
++count_key;
++count_adc;
P0=P0_Keeper;P2=P2_Keeper;
}
/*==================================按键扫描函数==============================*/
void Scan_key()
{
uchar readdat=P3^0xff;
trg=readdat&(readdat^cnt);
cnt=readdat;
if(trg & 0x01) //S7
{
if(!flag_set)
{
if(++f_mode == 2)
{
f_mode=0;
}
}
flag_beep=1;
}
else if(trg & 0x02) //S6
{
if(f_mode == 1)
{
flag_beep=~flag_beep;
}
else
{
flag_set=~flag_set;
if(!flag_set)
{
EA=0;
write_24c02(0x00,shidu_th);
EA=1;
}
}
}
else if(trg & 0x04) //S5
{
if(f_mode == 1)
{
relay=1;
}
else
{
if(flag_set)
{
++shidu_th;
}
}
}
else if(trg & 0x08) //S4
{
if(f_mode == 1)
{
relay=0;
}
else
{
if(flag_set)
{
--shidu_th;
}
}
}
}
void Display()
{
dsp_Time[0]=h / 16;
dsp_Time[1]=h % 16;
dsp_Time[3]=m / 16;
dsp_Time[4]=m % 16;
dsp_Time[6]=shidu / 10;
dsp_Time[7]=shidu % 10;
dsp_Set[6]=shidu_th / 10;
dsp_Set[7]=shidu_th % 10;
if(f_mode == 1)
{
L2=1;L1=0;
if(shidu < shidu_th)
{
if(flag_beep)
{
buzzer=1;
}
else
{
buzzer=0;
}
}
else
{
buzzer=0;
}
}
else
{
L1=1;L2=0;
buzzer=0;
if(shidu < shidu_th)
{
relay=1;
}
else
{
relay=0;
}
}
}
void Init_System()
{
uchar i=0;
control(0x00,0xa0);
control(0xff,0x80);
shidu_th=read_24c02(0x00);
if(shidu_th > 99)
{
shidu_th=50;
}
for(i=0;i<5;i++)
{
shidu=(uchar)(read_adc(0x03)/2.57);
}
Set_Time(8,30,55);
Timer0Init();
}
void main()
{
Init_System();
Delay300ms();
while(1)
{
if(TH0 < 0xf0)
{
h=Read_Ds1302_Byte(0x85);
m=Read_Ds1302_Byte(0x83);
s=Read_Ds1302_Byte(0x81);
}
if(count_adc > 199)
{
count_adc=0;
EA=0;
shidu=(uchar)(read_adc(0x03)/2.57);
EA=1;
}
Display();
if(count_key > 9)
{
count_key=0;
Scan_key();
}
}
}
ds1302.C驱动函数
#include <reg52.h>
#include <ds1302.h>
#include <intrins.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);
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_();
return (temp);
}
/*=========================================================================*/
void Set_Time(char h, char m,char s)
{
Write_Ds1302_Byte(0x8e,0x00);
Write_Ds1302_Byte(0x84,(h/10)*16+h%10);
Write_Ds1302_Byte(0x82,(m/10)*16+m%10);
Write_Ds1302_Byte(0x80,(s/10)*16+m%10);
Write_Ds1302_Byte(0x8e,0x80);
}
ds1302.h驱动函数
#ifndef __DS1302_H
#define __DS1302_H
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void Set_Time(char h, char m,char s);
#endif
iic.c驱动函数
#include "reg52.h"
#include "iic.h"
#include "intrins.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;
IIC_WaitAck();
}
//从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);
}
IIC_WaitAck();
return da;
}
/*===================================24c02==============================*/
void write_24c02(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_SendByte(add);
IIC_SendByte(dat);
IIC_Stop();
}
unsigned char read_24c02(unsigned char add)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0xa0);
IIC_SendByte(add);
IIC_Start();
IIC_SendByte(0xa1);
dat=IIC_RecByte();
IIC_Stop();
return dat;
}
/*=============================PCF8591读取rb2的值================================*/
unsigned char read_adc(unsigned char add)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_SendByte(add);
IIC_Start();
IIC_SendByte(0x91);
dat=IIC_RecByte();
IIC_Stop();
return dat;
}
iic.h驱动函数
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
//void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
unsigned char read_adc(unsigned char add);
void write_24c02(unsigned char add,unsigned char dat);
unsigned char read_24c02(unsigned char add);
#endif
|