多用途时钟程序DS1302.docx
- 文档编号:12027531
- 上传时间:2023-04-16
- 格式:DOCX
- 页数:35
- 大小:19.03KB
多用途时钟程序DS1302.docx
《多用途时钟程序DS1302.docx》由会员分享,可在线阅读,更多相关《多用途时钟程序DS1302.docx(35页珍藏版)》请在冰豆网上搜索。
多用途时钟程序DS1302
/**************************************************
===============多用途时钟程序==================
**所用元器件:
AT89S52、DS1302、蜂鸣器、继电器、
**红外一体化接收头、按键、LED灯。
**修改时间:
2012.2.12byKeliwen
**************************************************/
#include
#include
typedefunsignedcharuchar;
typedefunsignedintuint;
#defineNOP(){_nop_();_nop_();_nop_();_nop_();}
/*DS1302端口定义*/
sbitCE=P1^2;
sbitSCLK=P1^0;
sbitIO=P1^1;
sbitKey_Setup=P1^5;//设置下一位
sbitKey_Add=P1^6;//加
sbitKey_Dec=P1^7;//减
sbitKey_All=P2^2;//设置
//数码管位选
sbitRow1=P2^7;
sbitRow2=P2^6;
sbitRow3=P2^5;
sbitRow4=P2^4;
sbitBeep=P2^0;//蜂鸣器
sbitSW=P2^1;//继电器
sbitLED=P1^3;//工作指示灯
ucharDot;
bitSign;
ucharTr;
ucharflag=0;
charHour,Minute;
ucharBlack_1,Black_2;
uinttime;
ucharNum;
bitZ_flag=0;
uintN;
charT_Hour,T_Minute;
bitAlarm_flag=0;
ucharCH=0;
charT_ON_Hour,T_ON_Minute,T_OFF_Hour,T_OFF_Minute;
bitAl_flag=0;
bitSW_flag=0;
bitDis_Sign=0;
ucharcodeDis[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴数码管花数
ucharIR_Code[4];
ucharIR_Data[33];
ucharIR_Time;
bitIRPRO_OK=0,IR_OK=0;
/*延时函数*/
voidDelay(uintt)
{
while(t--)
{}
}
/*DS1302写一个字节*/
voidWrite_Byte(ucharByte)
{
uchari;
for(i=0;i<8;i++)
{
SCLK=0;
IO=(bit)(Byte&0x01);
SCLK=1;
Byte>>=1;
}
}
/*DS1302读一个字节*/
ucharRead_Byte(void)
{
uchari,Result;
for(i=0;i<8;i++)
{
Result>>=1;
SCLK=0;
NOP();
if(IO)
Result|=0x80;
else
Result&=0x7f;
SCLK=1;
NOP();
}
returnResult;
}
/*写DS1302*/
voidWrite_1302(ucharAddr,ucharDat)
{
CE=0;
SCLK=0;
CE=1;
NOP();
Write_Byte(Addr);
Write_Byte(Dat);
NOP();
SCLK=0;
CE=0;
NOP();
}
/*读DS1302*/
ucharRead_1302(ucharAddr)
{
ucharValue;
CE=0;
SCLK=0;
CE=1;
NOP();
Write_Byte(Addr);
Value=Read_Byte();
NOP();
SCLK=0;
CE=0;
NOP();
returnValue;
}
/*DS1302初始化*/
voidInit_1302(void)
{
Delay(60000);
Write_1302(0x8e,0x00);//关写保护
Delay(100);
Write_1302(0x8c,0x12);//年
Delay(100);
Write_1302(0x8a,0x04);//周
Delay(100);
Write_1302(0x88,0x01);//月
Delay(100);
Write_1302(0x86,0x26);//日
Delay(100);
Write_1302(0x84,0x00);//时
Delay(100);
Write_1302(0x82,0x35);//分
Delay(100);
Write_1302(0x80,0x05);//秒
Delay(100);
Write_1302(0x8e,0x80);//开写保护
Delay(500);
}
/*按键声*/
voidBeep_Play(void)
{
Beep=1;
Delay(3000);
Beep=0;
}
/*闹铃声*/
voidClock_Alarm(void)
{
time++;
if(time==100)
Beep=1;
if(time==150)
{
time=0;
Beep=0;
}
}
/*整点报时声*/
voidZ_Alarm(void)
{
if(Z_flag)
{
time++;
if(time==249)
Beep=1;
if(time==250)
{
time=0;
Beep=0;
Z_flag=0;
}
}
}
/*闪灯*/
voidWork_LED(void)
{
N++;
if(N==750)
LED=1;
if(N>800)
{
N=0;
LED=0;
}
}
/*数码管显示1*/
voidTime_Display(void)
{
ucharTime[3];
Time[0]=Read_1302(0x81);
Time[1]=Read_1302(0x85);
P0=Dis[Time[1]>>4]&Black_1;
Row1=0;
Delay(250);
Row1=1;
P0=(Dis[Time[1]&0x0f]&Black_1)|Dot;
Row2=0;
Delay(250);
Row2=1;
Time[2]=Read_1302(0x83);
P0=Dis[Time[2]>>4]&Black_2;
Row3=0;
Delay(250);
Row3=1;
P0=Dis[Time[2]&0x0f]&Black_2;
Row4=0;
Delay(200);
Row4=1;
if(Tr!
=(Time[0]&0x0f))
Sign=!
Sign;
Tr=Time[0]&0x0f;
if(Time[2]==0x00&&Time[0]==0x00)//整点报时
{
Z_flag=1;
}
if(Al_flag)//控制蜂鸣器标志标
{
if(Time[1]==T_Hour&&Time[2]==T_Minute&&Time[0]<0x30)
Alarm_flag=1;
else
{
Alarm_flag=0;
Beep=0;
}
}
if(SW_flag)//控制继电器
{
if(Time[1]>T_ON_Hour&&Time[2]>=T_ON_Minute)
{
SW=1;
}
elseif(Time[1]==T_OFF_Hour&&Time[2]==T_OFF_Minute)
{
SW=0;
}
else_nop_();
}
P0=0x00;
P2|=0xf0;
}
/*数码管显示2*/
voidDisplay_Alarm(ucharT_h,ucharT_m,bita)
{
ucharDota;
if(a)
Dota=0x80;
else
Dota=0x00;
P0=Dis[T_h>>4]&Black_1;
Row1=0;
Delay(250);
Row1=1;
P0=(Dis[T_h&0x0f]&Black_1)|Dota;
Row2=0;
Delay(250);
Row2=1;
P0=Dis[T_m>>4]&Black_2;
Row3=0;
Delay(250);
Row3=1;
P0=Dis[T_m&0x0f]&Black_2;
Row4=0;
Delay(250);
Row4=1;
P0=0x00;
P2|=0xf0;
}
/*数码管显示3*/
voidDis_ON_OFF(void)
{
if(Al_flag)
P0=Dis[1];
else
P0=Dis[0];
Row1=0;
Delay(250);
Row1=1;
P0=0x40;
Row2=0;
Delay(250);
Row2=1;
P0=0x40;
Row3=0;
Delay(250);
Row3=1;
if(SW_flag)
P0=Dis[1];
else
P0=Dis[0];
Row4=0;
Delay(250);
Row4=1;
P0=0x00;
P2|=0xf0;
}
/*设置*/
voidTime_Setup(void)
{
charShi,Ge;
if(!
Key_Setup)//设置下一位
{
Delay(3000);
if(!
Key_Setup)
{
while(!
Key_Setup);
Beep_Play();
flag++;
if(flag>1)
flag=0;
}
}
if(flag==0)
{
if(!
Key_Add)//"+"
{
Delay(3000);
if(!
Key_Add)
{
while(!
Key_Add);
Beep_Play();
if(CH==1)//设置“时”
{
Hour=Read_1302(0x85);
Ge=Hour&0x0f;
Shi=Hour>>4;
Ge++;
if(Ge>9)
{
Ge=0;
Shi++;
if(Shi>2)
Shi=0;
}
Shi<<=4;
Hour=Shi|Ge;
if(Hour>0x23)
Hour=0x00;
Write_1302(0x8e,0x00);
Delay(50);
Write_1302(0x84,Hour);
Delay(10);
Write_1302(0x8e,0x80);
}
elseif(CH==2)//设置闹钟“时”
{
Ge=T_Hour&0x0f;
Shi=T_Hour>>4;
Ge++;
if(Ge>9)
{
Ge=0;
Shi++;
if(Shi>2)
Shi=0;
}
Shi<<=4;
T_Hour=Shi|Ge;
if(T_Hour>0x23)
T_Hour=0x00;
}
elseif(CH==3)//设置继电器开“时”
{
Ge=T_ON_Hour&0x0f;
Shi=T_ON_Hour>>4;
Ge++;
if(Ge>9)
{
Ge=0;
Shi++;
if(Shi>2)
Shi=0;
}
Shi<<=4;
T_ON_Hour=Shi|Ge;
if(T_ON_Hour>0x23)
T_ON_Hour=0x00;
}
elseif(CH==4)//设置继电器关“时”
{
Ge=T_OFF_Hour&0x0f;
Shi=T_OFF_Hour>>4;
Ge++;
if(Ge>9)
{
Ge=0;
Shi++;
if(Shi>2)
Shi=0;
}
Shi<<=4;
T_OFF_Hour=Shi|Ge;
if(T_OFF_Hour>0x23)
T_OFF_Hour=0x00;
}
elseif(CH==5)//设置闹钟标志位
{
Al_flag=!
Al_flag;
}
}
}
if(!
Key_Dec)//"-"注释同上
{
Delay(3000);
if(!
Key_Dec)
{
while(!
Key_Dec);
Beep_Play();
if(CH==1)
{
Hour=Read_1302(0x85);
Ge=Hour&0x0f;
Shi=Hour>>4;
Ge--;
if(Ge<0)
{
Ge=9;
Shi--;
if(Shi<0)
{
Shi=2;
Ge=3;
}
}
Shi<<=4;
Hour=Shi|Ge;
if(Hour<0)
Hour=0x23;
Write_1302(0x8e,0x00);
Delay(50);
Write_1302(0x84,Hour);
Delay(10);
Write_1302(0x8e,0x80);
}
elseif(CH==2)
{
Ge=T_Hour&0x0f;
Shi=T_Hour>>4;
Ge--;
if(Ge<0)
{
Ge=9;
Shi--;
if(Shi<0)
{
Shi=2;
Ge=3;
}
}
Shi<<=4;
T_Hour=Shi|Ge;
if(T_Hour<0)
T_Hour=0x23;
}
elseif(CH==3)
{
Ge=T_ON_Hour&0x0f;
Shi=T_ON_Hour>>4;
Ge--;
if(Ge<0)
{
Ge=9;
Shi--;
if(Shi<0)
{
Shi=2;
Ge=3;
}
}
Shi<<=4;
T_ON_Hour=Shi|Ge;
if(T_ON_Hour<0)
T_ON_Hour=0x23;
}
elseif(CH==4)
{
Ge=T_OFF_Hour&0x0f;
Shi=T_OFF_Hour>>4;
Ge--;
if(Ge<0)
{
Ge=9;
Shi--;
if(Shi<0)
{
Shi=2;
Ge=3;
}
}
Shi<<=4;
T_OFF_Hour=Shi|Ge;
if(T_OFF_Hour<0)
T_OFF_Hour=0x23;
}
elseif(CH==5)
{
SW_flag=!
SW_flag;
}
}
}
}
if(flag==1)
{
if(!
Key_Add)//"+"
{
Delay(3000);
if(!
Key_Add)
{
while(!
Key_Add);
Beep_Play();
if(CH==1)//设置“分”
{
Minute=Read_1302(0x83);
Ge=Minute&0x0f;
Shi=Minute>>4;
Ge++;
if(Ge>9)
{
Ge=0;
Shi++;
if(Shi>5)
Shi=0;
}
Shi<<=4;
Minute=Shi|Ge;
if(Minute>0x59)
Minute=0x00;
Write_1302(0x8e,0x00);
Delay(50);
Write_1302(0x82,Minute);
Delay(10);
Write_1302(0x8e,0x80);
}
elseif(CH==2)//设置闹钟“分”
{
Ge=T_Minute&0x0f;
Shi=T_Minute>>4;
Ge++;
if(Ge>9)
{
Ge=0;
Shi++;
if(Shi>5)
Shi=0;
}
Shi<<=4;
T_Minute=Shi|Ge;
if(T_Minute>0x59)
T_Minute=0;
}
elseif(CH==3)//设置继电器开“分”
{
Ge=T_ON_Minute&0x0f;
Shi=T_ON_Minute>>4;
Ge++;
if(Ge>9)
{
Ge=0;
Shi++;
if(Shi>5)
Shi=0;
}
Shi<<=4;
T_ON_Minute=Shi|Ge;
if(T_ON_Minute>0x59)
T_ON_Minute=0;
}
elseif(CH==4)//设置继电器关“分”
{
Ge=T_OFF_Minute&0x0f;
Shi=T_OFF_Minute>>4;
Ge++;
if(Ge>9)
{
Ge=0;
Shi++;
if(Shi>5)
Shi=0;
}
Shi<<=4;
T_OFF_Minute=Shi|Ge;
if(T_OFF_Minute>0x59)
T_OFF_Minute=0;
}
elseif(CH==5)//设置继电器标志位
{
Al_flag=!
Al_flag;
}
}
}
if(!
Key_Dec)//"-"注释同上
{
Delay(3000);
if(!
Key_Dec)
{
while(!
Key_Dec);
Beep_Play();
if(CH==1)
{
Minute=Read_1302(0x83);
Ge=Minute&0x0f;
Shi=Minute>>4;
Ge--;
if(Ge<0)
{
Ge=9;
Shi--;
if(Shi<0)
Shi=9;
}
Shi<<=4;
Minute=Shi|Ge;
if(Minute<0)
Minute=0x59;
Write_1302(0x8e,0x00);
Delay(50);
Write_1302(0x82,Minute);
Delay(10);
Write_1302(0x8e,0x80);
}
elseif(CH==2)
{
Ge=T_Minute&0x0f;
Shi=T_Minute>>4;
Ge--;
if(Ge<0)
{
Ge=9;
Shi--;
if(Shi<0)
Shi=9;
}
Shi<<=4;
T_Minute=Shi|Ge;
if(T_Minute<0)
T_Minute=0x59;
}
elseif(CH==3)
{
Ge=T_ON_Minute&0x0f;
Shi=T_ON_Minute>>4;
Ge--;
if(Ge<0)
{
Ge=9;
Shi--;
if(Shi<0)
Shi=9;
}
Shi<<=4;
T_ON_Minute=Shi|Ge;
if(T_ON_Minute<0)
T_ON_Minute=0x59;
}
elseif(CH==4)
{
Ge=T_OFF_Minute&0x0f;
Shi=T_OFF_Minute>>4;
Ge--;
if(Ge<0)
{
Ge=9;
Shi--;
if(Shi<0)
Shi=9;
}
Shi<<=4;
T_OFF_Minute=Shi|Ge;
if(T_OFF_Minute<0)
T_OFF_Minute=0x59;
}
elseif(CH==5)
{
SW_flag=!
SW_flag;
}
}
}
}
}
/*中断、定时器初始化*/
voidIsr_Init(void)
{
TMOD=0x02;//定时器0工作于8位自动重装模式
TH0=TL0=0x00;
IE=0x83;//开总中断,开定时器0中断,开外部中断0
TCON=0x11;//TR0=1,IT0=1
}
/*将接收到的数据解码*/
voidIR_Pro(void)
{
uchari,j,k;
ucharCord,Value=0;
k=1;//k=1,丢掉引导码
for(i=0;i<4;i++)//共4个数据
{
for(j=0;j<8;j++)//一个数据8位
{
Value>>=1;
Cord=IR_Data[k];
if(Cord>12)//Cord>12,说明数据位为1,否则为0
Value|=0x80;
k++;
}
IR_Code[i]=Value;
Value=0;
}
IRPRO_OK=1;
}
/*红外操作*/
voidIR_Operate(void)
{
if(IR_Code[0]==0x00&&IR_Code[1]==0xff)
{
if(IR_Code[2]==0x4c&&IR_Code[3]==~IR_Code[2])//蜂鸣器控制
{
Beep=~Beep;
}
if(IR_Code[2]==0x0d&&IR_Code[3]==~IR_Code[2])//继电器控制
{
SW=~SW;
}
if(IR_Code[2]==0x0c&&IR_Code[3]==~IR_Code[2])//关闭数码管显示
{
Beep_Play();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多用途 时钟 程序 DS1302
![提示](https://static.bdocx.com/images/bang_tan.gif)