#include<reg51.h>
#include "public.h"
#define uchar unsigned char
#define uint unsigned int
#define write_addr  0xa0
#define read_addr  0xa1
uchar  number_list[10]={0xed,0x28,0xb5,0xb9,0x78,0xd9,0xdd,0xa8,0xfd,0xf9};
uchar  number_list1[10]={0xef,0x2A,0xb7,0xbB,0x7A,0xdB,0xdF,0xaA,0xfF,0xfB};
uchar  x_list[12]={0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b};
void init_clock(void);
void delay(ms);
void ds_time();
void ds_day();
void set_time();
uchar Sec;
uchar Min;
uchar Hour;
uchar Week;
uchar day;
uchar month;
uint year;
uchar Sec_H;
uchar Sec_L;
uchar Min_H;
uchar Min_L;
uchar Hour_H;
uchar Hour_L;
uchar timer_count;
uchar dsplay_count;
uchar key_week;
uchar key_h;
uchar key_m;
uchar key_s;
uchar set_flag;
uchar ds_flag;
uchar GJ;
void init_clock(void)
{
//uchar i;
Sec=00;
Min=39;
Hour=16;
Week=5;
day=25;
month=3;
year=2011;
P1=0x00;
P3=0x00;
/*  for(i=0;i<4;i++)
{
EXT_ADR=x_list[i+5];
EXT_DAT=number_list[0];
}
EXT_ADR=x_list[3];
EXT_DAT=number_list[1];
EXT_ADR=0X14;
EXT_DAT=0Xb7;
EXT_ADR=0X18;
EXT_DAT=0XEF;
*/
}
void main ()
{
uchar i,day_sum;
timer_count=0;
dsplay_count=0xAA;
key_h=0;
key_m=0;
key_s=0;
key_week=0;
set_flag=0;
ds_flag=0xBB;
GJ=0xd0;
LCD_EN=1;
init_clock();
TMOD=0X01;
TCON=0x10;
TH0=0X63;
TL0=0XC0;
EA=1;
ET0=1;
while(1)
{  WDT_RST=0xA1;
//时钟计算、进位 
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))
{
day_sum=31;
}
if(month==2) //暂不区分闰年
{
day_sum=28;
}
if((month==4)||(month==6)||(month==9)||(month==11))
{
day_sum=30;
}
if((timer_count==10)&&(set_flag==0))
timer_count=0;
Sec++;
if(Sec==60)
Sec=0;
Min++;
if(Min==60)
{
Min=0;
Hour++;
if(Hour==24)
{
Hour=0;
Week++;
day++;
if(Week==8)
{
Week=1;       
}
if(day==day_sum) 
{
day=1;
month++;
if(month==13)
{
month=1;
year++;
}
}
}
}
if(ds_flag==0xbb)
{
ds_time();
}
if(ds_flag==0xaa)
{
ds_day();
}
if (ds_flag==0xcc)
{
for(i=0;i<=12;i++)
{
EXT_ADR=x_list[i];
EXT_DAT=0x00;
}
}
set_time();
if((Hour==8)&&(Min==29))
{
GJ=0xDD;
}
if((Hour==11)&&(Min==49))
{
GJ=0xDD;
}
if((Hour==13)&&(Min==29))
{
GJ=0xDD;
}
if((Hour==17)&&(Min==29))
{
GJ=0xDD;
}
else{
GJ=0xD0;
}
}
}
void ds_time(void)
{
EXT_ADR=0x1b;  // 显示时间
EXT_DAT=0x10;
//时钟显示
Sec_H=Sec/10;
Sec_L=Sec%10;
EXT_ADR=0x17;
EXT_DA
T=number_list[Sec_H];
EXT_ADR=0x18;
if(Sec_L%2==0)
{
EXT_DAT=number_list1[Sec_L]; 
}
else
{
EXT_DAT=number_list[Sec_L]; 
}
Min_H=Min/10;
安卓开发实例入门Min_L=Min%10;
EXT_ADR=0x15;
EXT_DAT=number_list[Min_H];
EXT_ADR=0x16;
EXT_DAT=number_list[Min_L];
Hour_H=Hour/10;
Hour_L=Hour%10;
EXT_ADR=0x13;
EXT_DAT=number_list1[Hour_H];
EXT_ADR=0x14;
EXT_DAT=number_list1[Hour_L];
EXT_ADR=0x11;
EXT_DAT=number_list[Week];
}
void ds_day (void)
{
EXT_ADR=0x1b;  // 显示日期
EXT_DAT=0x40;
//  日期显示
EXT_ADR=0x17;
EXT_DAT=number_list[day/10];
EXT_ADR=0x18;
EXT_DAT=number_list1[day%10]; 
//month
EXT_ADR=0x15;
EXT_DAT=number_list[month/10];
EXT_ADR=0x16;
EXT_DAT=number_list[month%10];
//year
EXT_ADR=0x11;
EXT_DAT=number_list[year/1000];
EXT_ADR=0x12;
EXT_DAT=number_list[year%1000/100];
EXT_ADR=0x13;
EXT_DAT=number_list1[year%1000%100/10];
EXT_ADR=0x14;
EXT_DAT=number_list1[year%1000%100%10];
}
void set_time()
{
//set_s
if(key_s==1)
{
if(set_s==0)
key_s=0;
}
else
{ if(set_s==1)
{
delay(20000);
if(set_s==1)
{
set_flag=1;
key_s=1;
Sec++;
if(Sec==60)
{
Sec=0;   
}
}
set_flag=0;
//set_h
if(key_h==1)
{
if(set_h==0)
key_h=0;
}
else
{ if(set_h==1)
{
delay(20000);
if(set_h==1)
{
set_flag=1;
key_h=1;
Hour++;
if(Hour==24)
{
Hour=0;   
}
}
}
}
set_flag=0;
/
/set_m
if(key_m==1)
{
if(set_m==0)
key_m=0;
}
else
{ if(set_m==1)
{
delay(20000);
if(set_m==1)
{
set_flag=1;
key_m=1;
Min++;
if(Min==60)
{
Min=0;   
}
}
set_flag=0;
//set_week
if(key_week==1)
if(set_week==0)
key_week=0;
}
else
{
if (set_week==1)
{
delay(20000);
if(set_week==1)
{
key_week=1;
Week++;
if(Week==8)
{
Week=1;
}
}
}
}
}
void timer0_int(void) interrupt 1 //定时100ms
{
TMOD=0X01;
TCON=0x10;
TH0=0X63;
TL0=0XC0;
WDT_RST=0xA1;
timer_count++;
dsplay_count++;
if(dsplay_count<30)
{
ds_flag=0xaa;
}
if(((dsplay_count)>30)&&((dsplay_count)<32))
{
ds_flag=0xcc;
}
if(((dsplay_count)>32)&&((dsplay_count)<60))
{
ds_flag=0xbb;
}
if(dsplay_count>60)
{
dsplay_count=0;
}
if(GJ==0xDD)
{
if(timer_count==8)
{
WP=0;
}
}
if(GJ==0xD0)
{
WP=1;
}
}
void delay (ms)
{uint i;
for(i=0;i<ms;i++)
{
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。