那要如何在用DS1302与1602LCD设计的可调式电子日历与时钟的程序里加上闹铃的功能啊?程序要怎么加进去啊?

2024-11-16 15:36:23
推荐回答(1个)
回答1:

#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit DS1302_SDA=P1^0;
sbit DS1302_SCK=P1^1;
sbit DS1302_RST=P1^2;
sbit KEY1=P3^0;
sbit KEY2=P3^1;
sbit KEY3=P3^2;
sbit KEY4=P3^3;
sbit BEEP=P3^7;
uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar Display_Buffer[]={0x00,0x00,0xbf,0x00,0x00,0xbf,0x00,0x00};
uchar Display_Buffer1[]={0x00,0x00,0xbf,0x00,0x00,0xbf,0x00,0x00};
uchar Bit_CODE[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar Current_Time[7];
uchar alarm_clock[7];
char Flag=0;
char T_OR_A=0;
uchar w=0,u=0,v;
void delay_ms(uint x)
{
uchar i;
while(x--)for(i=0;i<120;i++);
}
void WriteAByteToDS1302(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
DS1302_SDA=x&0x01;
DS1302_SCK=1;
DS1302_SCK=0;
x>>=1;
}
}
uchar GetAByteFromDS1302(void)
{
uchar i,b=0X00,t;
for(i=0;i<8;i++)
{
b>>=1;
t=DS1302_SDA;
b|=t<<7;
DS1302_SCK=1;
DS1302_SCK=0;
}
return (b/16*10+b%16);
}
void ResetDS1302(void)
{
DS1302_RST=0;
DS1302_SCK=0;
DS1302_RST=1;
}
uchar ReadData(uchar addr)
{
uchar dat;
ResetDS1302();
WriteAByteToDS1302(addr);
dat=GetAByteFromDS1302();
DS1302_SCK=0;
DS1302_RST=0;
return dat;
}
void WriteDS1302(uchar addr,uchar dat)
{
ResetDS1302();
WriteAByteToDS1302(addr);
WriteAByteToDS1302(dat);
DS1302_SCK=0;
DS1302_RST=0;
}
void SetDS1302(void)
{
uchar i;
WriteDS1302(0x8e,0x00);
for(i=0;i<7;i++)
{
WriteDS1302(0x80+i*2,(Current_Time[i]/10<<4)|(Current_Time[i]%10));
}
WriteDS1302(0x8e,0x80);
}
void GetTime(void)
{
uchar i,addr=0x81;
for(i=0;i<7;i++)
{
Current_Time[i]=ReadData(addr);
addr+=2;
}
}
void time0Init(void)
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void ScanKey(void)
{
if(KEY1==0)
{
delay_ms(10);
if(KEY1==0);
{
while(!KEY1);
w=w++;
if(w==5)w=1;
switch(w)
{
case 1:Flag=1;break;
case 2:Flag=1; break;
case 3:Flag=1; break;
case 4:SetDS1302();
Flag=0;
break;
}
}
}
if(w!=0)
{
if(KEY2==0)
{
delay_ms(10);
if(KEY2==0)
{
while(!KEY2);
switch(w)
{
case 1:Current_Time[0]++;break;
case 2:Current_Time[1]++;break;
case 3:Current_Time[2]++;break;
}
}
}
if(KEY3==0)
{
delay_ms(10);
if(KEY3==0)
{
while(!KEY3);
switch(w)
{
case 1:Current_Time[0]--;break;
case 2:Current_Time[1]--;break;
case 3:Current_Time[2]--;break;
}
}
}
}
if(KEY4==0)
{
delay_ms(10);
if(KEY4==0)
{
while(!KEY4);
u++;
if(u==5)u=1;
switch(u)
{
case 1:T_OR_A=1;
break;
case 2:break;
case 3:break;
case 4:T_OR_A=0;break;
}
}
}
if(u!=0)
{
if(KEY2==0)
{
delay_ms(10);
if(KEY2==0)
{
while(!KEY2);
switch(u)
{
case 1:alarm_clock[0]++;break;
case 2:alarm_clock[1]++;break;
case 3:alarm_clock[2]++;break;
}
}
}
if(KEY3==0)
{
delay_ms(10);
if(KEY3==0)
{
while(!KEY3);
switch(u)
{
case 1:alarm_clock[0]--;break;
case 2:alarm_clock[1]--;break;
case 3:alarm_clock[2]--;break;
}
}
}
}
}
void Play(uchar t)
{
uchar i;
for(i=0;i<100;i++)
{
BEEP = ~BEEP;
delay_ms(t);
}
BEEP = 0;
}
void main(void)
{
//uchar i;
SetDS1302();
time0Init();
//GetTime();
while(1)
{
if(Flag==0)
GetTime();
Display_Buffer[0]=DSY_CODE[Current_Time[2]/10];
Display_Buffer[1]=DSY_CODE[Current_Time[2]%10];
Display_Buffer[3]=DSY_CODE[Current_Time[1]/10];
Display_Buffer[4]=DSY_CODE[Current_Time[1]%10];
Display_Buffer[6]=DSY_CODE[Current_Time[0]/10];
Display_Buffer[7]=DSY_CODE[Current_Time[0]%10];
//Display_Buffer[0]=DSY_CODE[Current_Time[2]/10];
ScanKey();

Display_Buffer1[0]=DSY_CODE[alarm_clock[2]/10];
Display_Buffer1[1]=DSY_CODE[alarm_clock[2]%10];
Display_Buffer1[3]=DSY_CODE[alarm_clock[1]/10];
Display_Buffer1[4]=DSY_CODE[alarm_clock[1]%10];
Display_Buffer1[6]=DSY_CODE[alarm_clock[0]/10];
Display_Buffer1[7]=DSY_CODE[alarm_clock[0]%10];
if((Current_Time[2]==alarm_clock[2])&&(Current_Time[1]==alarm_clock[1])&&(Current_Time[0]==alarm_clock[0]))//闹钟
Play(1);
}
}
void tiemr0() interrupt 1
{
//if(flag1) j++;
uchar i,j;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(T_OR_A==0)
{
for(i=0;i<8;i++)
{
P2=Bit_CODE[i];
P0=Display_Buffer[i];
delay_ms(2);
}
}
else
{
for(j=0;j<8;j++)
{
P2=Bit_CODE[j];
P0=Display_Buffer1[j];
delay_ms(2);
}
}
}
上面是基于DS1302和LED数码管的可调闹钟C语言程序!