main函数在这
#include"stc15f2k60s2.h"
#include"ds1302.h"
#include"intrins.h"
#define uchar unsigned char
#define uint unsigned int
sbit Wr=P3^6;
#define rst573 P2&=0x1f//关闭选中
#define smgY6C P2|=0xc0 //位选
#define smgY7C P2|=0xe0 //段选
#define ledY4C P2|=0x80 //选中LED
#define beeY5C P2|=0xa0 //选中蜂鸣器+继电器
sbit k0=P3^0;
sbit k1=P3^1;
sbit k2=P3^2;
sbit k3=P3^3;
//行线
sbit k4=P3^4;
sbit k5=P3^5;
sbit k6=P4^2;
sbit k7=P4^4;
uchar code duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};
// 全灭+ 1~8wei
uchar code wei[]={0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
uchar num=0,time;
void CHS_BEEP_close() //关闭蜂鸣器继电器
{ Wr=0;
P2=0x5;
P06=1;
P04=0;
Wr=1;
}
void delay(uint z)//ms延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=1100;y>0;y--);
}
void display(uchar adt1,uchar adt2,uchar adt3)
{
P0=0x00; //消影
delay(1);
Wr=0; //位选
smgY6C;
P0=wei[1];
P2=0x1f;
Wr=0;
smgY7C; //段选
P0=duan[adt1/10];
P2=0x1f;
Wr=1;
delay(1);
Wr=0; //位选
smgY6C;
P0=wei[2];
P2=0x1f;
Wr=0;
smgY7C; //段选
P0=duan[adt1%10];
P2=0x1f;
Wr=1;
delay(1);
Wr=0; //位选
smgY6C;
P0=wei[3];
P2=0x1f;
Wr=0;
smgY7C; //段选
P0=0xbf;
P2=0x1f;
Wr=1;
delay(1);
Wr=0; //位选
smgY6C;
P0=wei[4];
P2=0x1f;
Wr=0;
smgY7C; //段选
P0=duan[adt2/10];
P2=0x1f;
Wr=1;
delay(1);
Wr=0; //位选
smgY6C;
P0=wei[5];
P2=0x1f;
Wr=0;
smgY7C; //段选
P0=duan[adt2%10];
P2=0x1f;
Wr=1;
delay(1);
Wr=0; //位选
smgY6C;
P0=wei[6];
P2=0x1f;
Wr=0;
smgY7C; //段选
P0=0xbf;
P2=0x1f;
Wr=1;
delay(1);
Wr=0; //位选
smgY6C;
P0=wei[7];
P2=0x1f;
Wr=0;
smgY7C; //段选
P0=duan[adt3/10];
P2=0x1f;
Wr=1;
delay(1);
Wr=0; //位选
smgY6C;
P0=wei[8];
P2=0x1f;
Wr=0;
smgY7C; //段选
P0=duan[adt3%10];
P2=0x1f;
Wr=1;
}
void keyscan()
{
if(k0==0){delay(50);if(k0==0)num=0;while(!k0);}
if(k1==0){delay(50);if(k1==0)num=1;while(!k1);}
}
void init_time()
{
TMOD=0x00; //16位自动重装定时器
TH0=0xec; //定时5MS
TL0=0x78;
ET0=1;
EA=1;
TR0=1;
}
void main()
{
init_time();
CHS_BEEP_close();
Ds1302_Init();
Ds1302_Write_Time();
Ds1302_Read_Time();
while(1)
{
keyscan();
if(num)display(time_buf1[1],time_buf1[2],time_buf1[3]);//显示年月日
else display(time_buf1[4],time_buf1[5],time_buf1[6]);//显示时分秒
}
}
void timer_0() interrupt 1
{
time++;
if(time==20) //定时读数据
{
Ds1302_Read_Time();
time=0;
}
}
|