本帖最后由 funfactory 于 2011-6-11 17:14 编辑
问题闹钟设置程序
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char code dispcode[]={
0x3f, /*0*/
0x06, /*1*/
0x5b, /*2*/
0x4f, /*3*/
0x66, /*4*/
0x6d, /*5*/
0x7d, /*6*/
0x07, /*7*/
0x7f, /*8*/
0x6f, /*9*/
0x40, /* */};
unsigned char dispbuf[8]={0,0,10,0,0,10,0,0};
unsigned char clok_dispbuf[8]={0,0,10,0,0,10,0,0};
unsigned char dispbitcnt=0;
unsigned char second = 57;
unsigned char minite = 58;
unsigned char hour = 11;
unsigned int tcnt;
unsigned char mstcnt;
unsigned char i,j,k,flag,a,b,c,n;
uchar clok_second=0;
uchar clok_minite=0;
uchar clok_hour=0;
uchar clock_set;
sbit ksec=P0^0;
sbit kmin=P0^1;
sbit khour=P0^2;
sbit kalrm = P0^3;
sbit fmq = P2^7;
sbit led = P0^7;
void init()
{
TMOD=0X11;
TH0=(65536-50000)/256; //50ms
TL0=(65536-50000)%256;
TH1=(65536-1000)/256; //1ms
TL1=(65536-1000)%256;
ET0=1; ET1=1;
TR0=1; TR1=1;
EA=1;
}
void delay(uint z)
{
uint i, j;
for (i=z; i>0;i--)
for (j=110; j>0; j--);
}
void buzzer(uint h)
{
for (k=h; k>0; k--)
{
fmq = ~fmq;
delay(1);
}
}
void key_set()
{
if(ksec==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(ksec==0)
{
buzzer(850);
second++;
if(second==60)
{
second=0;
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
while(ksec==0);
}
}
if(kmin==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(kmin==0)
{
buzzer(850);
minite++;
if(minite==60)
{
minite=0;
}
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
while(kmin==0);
}
}
if(khour==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(khour==0)
{
buzzer(850);
hour++;
if(hour==24)
{
hour=0;
}
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
while(khour==0);
}
}
if (clock_set == 1)
{
if(ksec==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(ksec==0)
{
buzzer(850);
clok_second++;
if(clok_second==60)
{
clok_second=0;
}
clok_dispbuf[0]=clok_second%10;
clok_dispbuf[1]=clok_second/10;
while(ksec==0);
}
}
if(kmin==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(kmin==0)
{
buzzer(850);
clok_minite++;
if(minite==60)
{
clok_minite=0;
}
clok_dispbuf[3]=clok_minite%10;
clok_dispbuf[4]=clok_minite/10;
while(kmin==0);
}
}
if(khour==0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
if(khour==0)
{
buzzer(850);
clok_hour++;
if(clok_hour==24)
{
clok_hour=0;
}
clok_dispbuf[6]=clok_hour%10;
clok_dispbuf[7]=clok_hour/10;
while(khour==0);
}
}
}
}
void alarm_set()
{
if (kalrm == 0)
{
for(i=5;i>0;i--)
for(j=248;j>0;j--);
//delay(10);
if (kalrm == 0)
{
buzzer(850); //蜂鸣器按键音
clock_set++; //闹钟设置标志位
if (clock_set == 1)
{
led = 0; //设置时led灯亮
key_set(); //按键控制时间的加减
}
if (clock_set == 2)
{
clock_set = 0;
led = 1;
}
}
}
}
void main(void)
{
init();
while(1)
{
key_set();
alarm_set();
if ((second==clok_second)&&(minite==clok_minite)&(&hour==clok_hour)&&(clock_set==0))
{
for (n=100; n>0; n--)
{
buzzer(5000);
}
}
if ((second==0)&&(minite==0)) //整点报时
{
buzzer(3000);
}
}
}
void t0(void) interrupt 1 //
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tcnt++;
if(tcnt==20)
{
tcnt=0;
second++;
if(second==60)
{
second=0;
minite++;
if(minite==60)
{
minite=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
dispbuf[0]=second%10;
dispbuf[1]=second/10;
dispbuf[3]=minite%10;
dispbuf[4]=minite/10;
dispbuf[6]=hour%10;
dispbuf[7]=hour/10;
}
}
void timer1() interrupt 3 //扫面显示
{
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
//dispbitcnt++;
if (clock_set == 1)
{
P3=~(0x01<<dispbitcnt);
P1=dispcode[clok_dispbuf[dispbitcnt]];
}
else
P3=~(0x01<<dispbitcnt);
P1=dispcode[dispbuf[dispbitcnt]];
if(dispbitcnt++==8)
{
dispbitcnt=0;
}
}
|