有关单片机设置闹钟的问题,求大神赐教!
这是一个时钟函数,可以调节时间,我打算设置一个闹钟,但是不知道每次闹钟响的时候时钟就会停止工作,而且不知道怎样控制按键使得闹钟能够停下来。请路过的大神们支招,谢谢啦!注:函数的编译没有任何问题,前面的时钟和按键控制调节时间都是没有问题的,就是不知道怎样把闹钟加进去,
并且设置s4键使得当闹钟响时按下这个键可以停止闹铃,在后面我会把出错的地方做说明。
另外不知哪位大神可以赐教一下如何使用ds1302在此基础上做一个万年历,如果可以的话请分享一段代码,让我参拜一下大神的作品,感激不尽!
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula = P2^6;
sbit wela = P2^7;
sbit lcdrs = P1^0;
sbit lcdrw = P1^1;
sbit lcden = P2^5;
sbit s1 = P3^0;
sbit s2 = P3^1;
sbit s3 = P3^2;
sbit rd = P3^7;
sbit beep = P2^3;
uchar code table[] = " 2014-04-02 WED";
uchar code table1[] = " 00:00:5";
uchar count,shi,fen,miao,s1num;
void delay(uint z) //延时函数
{
uint x,y;
for(x = z;x > 0;x --)
for(y = 110;y > 0;y--);
}
void write_com(uchar com) //写指令函数
{
lcdrs = 0;
lcdrw = 0;
lcden = 0;
P0 = com;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
void write_date(uchar date) //写数据函数
{
lcdrs = 1;
lcdrw = 0;
lcden = 0;
P0 = date;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
void init() //初始化函数
{
uchar num;
dula = 0;
wela = 0;
lcden = 0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num = 0;num < 15;num ++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80 + 0x41);
for(num = 0;num < 11;num ++)
{
write_date(table1[num]);
delay(5);
}
TMOD = 0x01; //设置定时器工作方式
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
//miao = 53;
//fen = 59;
//shi = 23;
}
void write_sfm(uchar add,uchar date) //写时分秒函数
{
uchar shi,ge;
shi = date / 10;
ge = date % 10;
write_com(0x80 + 0x40 + add);
write_date(0x30 + shi);
write_date(0x30 + ge);
}
void keyscan() //键盘扫描函数
{
rd = 0;
if(s1 == 0)
{
delay(5);
if(s1 == 0)
{
s1num ++; //设置标志位,用来判断功能键s1是否按下
while(!s1);
if(s1num == 1)
{
TR0 = 0;
write_com(0x80 + 0x40 + 11);
write_com(0x0f);
}
if(s1num == 2)
{
write_com(0x80 + 0x40 + 8);
}
if(s1num == 3)
{
write_com(0x80 + 0x40 + 5);
}
if(s1num == 4)
{
s1num = 0;
write_com(0x0c);
TR0 = 1;
}
}
}
if(s1num != 0) //判断当功能键按下时,对按下的次数进行判断,并移动指针来对时或分或秒进行加1
{
if(s2 == 0)
{
delay(5); //消抖
if(s2 == 0)
{
while(!s2); //松手检测,下同
if(s1num == 1)
{
miao ++;
if(miao == 60)
miao = 0;
write_sfm(10,miao);
write_com(0x80 + 0x40 + 10);
}
if(s1num == 2)
{
fen ++;
if(fen == 60)
fen = 0;
write_sfm(7,fen);
write_com(0x80 + 0x40 + 7);
}
if(s1num == 3)
{
shi ++;
if(shi == 24)
shi = 0;
write_sfm(4,shi);
write_com(0x80 + 0x40 + 4);
}
}
}
}
if(s1num != 0) //判断当功能键按下时,对按下的次数进行判断,并移动指针来对时或分或秒进行减1
{
if(s3 == 0)
{
delay(5);
if(s3 == 0)
{
while(!s3);
if(s1num == 1)
{
miao --;
if(miao == -1)
miao = 59;
write_sfm(10,miao);
write_com(0x80 + 0x40 + 10);
}
if(s1num == 2)
{
fen --;
if(fen == -1)
fen = 59;
write_sfm(7,fen);
write_com(0x80 + 0x40 + 7);
}
if(s1num == 3)
{
shi --;
if(shi == -1)
shi = 23;
write_sfm(4,shi);
write_com(0x80 + 0x40 + 4);
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
//while(1);
}
void timer0() interrupt 1
{
TH0 = (65536 - 50000) / 256;
TL0 = (65536 - 50000) % 256;
count ++;
if(count == 18)
{
count = 0;
miao ++;
if(miao == 60)
{
miao = 0;
beep = 1;
fen ++;
if(fen == 60)
{
fen = 0;
shi ++;
if(shi == 24)
{
shi = 0;
}
write_sfm(4,shi);
}
write_sfm(7,fen);
}
write_sfm(10,miao);
}
while(miao == 5) //设置闹钟的地方就在这里,如果哪位有办法的话不妨删去这一段,用其他的方式把闹钟加进去,非常感谢!
{
beep = 1;
delay(200);
beep = 0;
delay(200);
if(miao == 8)
break;
}
}