分模块写的 代码可能会多一点 麻烦大神帮忙看一下。被折磨好几天了 快崩溃
//-主函数 预定义和main()
#include<reg52.h>
#include"achieve.h"
void main()
{
while(1)
{
key();
delay(2);
}
}
//-------延时函数---------
void delay(uchar z)
{
uchar i,j;
i=z;
for(;i>0;i--)
{
for(j=2;j>0;j--)
{};
}
}
#include<reg52.h>
#include"achieve.h"
sbit key1=P3^4; //K1 设置第一次的时间
sbit key2=P3^5; //K2 设置第二次的时间
sbit key3=P3^6; //K3 启动闹铃
sbit key4=P3^7; //K4 关闭闹铃
//--------按键程序------------
//--------是否要消抖----------
void key()
{
P3=0xff;
if(key1==0)
{
/* delay(10);
if(key1==0)*/
fun1();
}
if(key2==0)
{
/* delay(10);
if(key2==0)*/
fun2();
}
if(key3==0)
{
/* delay(10);
if(key3==0) */
begin();
}
if(key4==0)
{
/* delay(10);
if(key4==0) */
close();
}
}
//--------检测按键---------------
//注释掉了 延时
uchar temp; //获取P3端口状态
uchar flag;
uchar keyscan()
{
flag=0;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
//delay(10); //延时消抖
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //确认非外界因素 导致键盘按下
{
temp=P3;
switch(temp)
{
case 0xee:{flag=1;break;}
case 0xde:{flag=2;break;}
case 0xbe:{flag=3;break;}
case 0x7e:{flag=4;break;}
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
//delay(10); //延时消抖
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //确认非外界因素 导致键盘按下
{
temp=P3;
switch(temp)
{
case 0xed:{flag=5;break;}
case 0xdd:{flag=6;break;}
case 0xbd:{flag=7;break;}
case 0x7d:{flag=8;break;}
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
//delay(10); //延时消抖
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0) //确认非外界因素 导致键盘按下
{
temp=P3;
switch(temp)
{
case 0xeb:{flag=9;break;}
case 0xdb:{flag=10;break;}
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
return flag;
}
#include<reg52.h>
#include"achieve.h"
/*sbit dula=P2^6; //段选 位选控制
sbit wela=P2^7;*/
sbit first=P0^0;
sbit two=P0^1;
sbit third=P0^3;
sbit fouth=P0^4; //控制第 1,2,4,5个数码管 24小时制 XX:XX
uchar diyici[4];
uchar dierci[4];
uchar sum; //if(!keyscan()) 改为 sum=keyscan(); if(sum)
int num0,num1; //两次时间化为分钟数
uchar shu;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
};
void fun1()
{
sum=keyscan();
if(!sum)
{
wela=1;
first=0;
wela=0; //选通位选
dula=1;
P0=0x3f;
dula=0;
while(!sum)
{
sum=keyscan();
}
}
diyici[0]=table[sum-1];
dula=1;
P0=table[sum-1];
dula=0;
sum=keyscan();
if(!sum)
{
wela=1;
two=0;
wela=0;
dula=1;
P0=0x3f;
dula=0;
while(!sum)
{
sum=keyscan();
}
}
diyici[1]=table[sum-1];
dula=1;
P0=table[sum-1];
dula=0;
sum=keyscan();
if(!sum)
{
wela=1;
third=0;
wela=0; //选通位选
dula=1;
P0=0x3f;
dula=0;
while(!sum)
{
sum=keyscan();
}
}
diyici[2]=table[sum-1];
dula=1;
P0=table[sum-1];
dula=0;
sum=keyscan();
if(!sum)
{
wela=1;
fouth=0;
wela=0; //选通位选
dula=1;
P0=0x3f;
dula=0;
while(!sum)
{
sum=keyscan();
}
}
diyici[3]=table[sum-1];
dula=1;
P0=table[sum-1];
dula=0;
}
void fun2()
{
sum=keyscan();
if(!sum)
{
wela=1;
first=0;
wela=0; //选通位选
dula=1;
P0=0x3f;
dula=0;
while(!sum)
{
sum=keyscan();
}
}
dierci[0]=table[sum-1];
dula=1;
P0=table[sum-1];
dula=0;
sum=keyscan();
if(!sum)
{
wela=1;
two=0;
wela=0; //选通位选
dula=1;
P0=0x3f;
dula=0;
while(!sum)
{
sum=keyscan();
}
}
dierci[1]=table[sum-1];
dula=1;
P0=table[sum-1];
dula=0;
sum=keyscan();
if(!sum)
{
wela=1;
third=0;
wela=0; //选通位选
dula=1;
P0=0x3f;
dula=0;
while(!sum)
{
sum=keyscan();
}
}
diyici[2]=table[sum-1];
dula=1;
P0=table[sum-1];
dula=0;
sum=keyscan();
if(!sum)
{
wela=1;
fouth=0;
wela=0; //选通位选
dula=1;
P0=0x3f;
dula=0;
while(!sum)
{
sum=keyscan();
}
}
dierci[3]=table[sum-1];
dula=1;
P0=table[sum-1];
dula=0;
}
void begin()
{
num0=(diyici[0]*10+diyici[1])*60+diyici[2]*10+diyici[3];
num1=(dierci[0]*10+dierci[1])*60+dierci[2]*10+dierci[3];
if(num1>=num0)
shu=num1-num0;
else
shu=num0-num1;
liushuideng();
}
void close()
{
P1=0xff;
TR0=0;
dula=1;
P0=0;
dula=0;
}
#include<reg52.h>
#include<intrins.h>
#include"achieve.h"
sbit deep=P2^3;
uchar time0,led;
int time1;
void liushuideng()
{ //使用中断以控制闹铃时间
TMOD=0x01; //工作方式
TH0=(65536-50000)/256; // 赋值
TL0=(65536-50000)%256;
EA=1; //开定时器总中断
ET0=1; //开定时器T0中断
TR0=1; //启动定时器T0
led=0xfe;
P1=led;
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
time0++;
if(time0==20)
{
time0=0;
led=_crol_(led,1);
P1=led;
time1++;
if(time1==shu*60)
{
TR0=0;
deep=0; //蜂鸣器响
dula=1;
P0=0;
dula=0;
}
}
}
#define uint unsigned int
#define uchar unsigned char
//------function---------
extern uchar flag; //返回值,返回按下数字0-9的哪个值
//------interrupt---------
extern uchar time0,led;
extern int time1;
//------achieve-----------
extern int num0,num1;
extern uchar shu;
sbit dula=P2^6;
sbit wela=P2^7;
//-------各模块函数接口----------
void key();
void fun1();
void fun2();
void begin();
void close();
void delay(uchar z);
uchar keyscan();
void liushuideng();
谢谢论坛平台 谢谢各位有心进来帮忙解决的前辈!!!!非伸手
|