|
#include<reg51.h>
#define uchar unsigned char
#define uint8 unsigned int
sbit io_DS1302_RST = P1^5 ;
sbit io_DS1302_IO = P1^6 ;
sbit io_DS1302_SCLK = P1^7 ;
sbit duan=P1^0;
sbit wei=P1^2;
char shi,fen,miao,day,month,year,week,k,tiaoshijian,tiaoriqi,bb;
uchar table[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xbf};
uchar table1[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0x80,0x90,0xbf,0xbf};
#define KEY_1 0x0e
#define KEY_2 0x0d
#define KEY_3 0x0b
#define KEY_4 0x07
#define KEY_5 0x10
#define KEY_NULL 0x1f
#define KEY_PRESS 0x80
#define KEY_LONG 0x40
#define KEY_STATE_INIT 0
#define KEY_STATE_PRESS 1
#define KEY_STATE_LONG 2
#define KEY_STATE_UP 3
#define KEY_LONG_PERIOD 20
#define KEY_CONTINUE_PERIOD 10
bit set;
bit dao1S=0;
bit dao2MS=0;
bit dao10MS;
bit ss,mm,ff,yue,tian,xq;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
sbit key5=P3^4;
int main_flag=0,exit_flag,up_flag,down_flag;
int tab[]={0,0,0,0,0,0};
uchar weitable[]={0x01,0x02,0x04,0x08,0x10,0x20};
uchar tab1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void nint()
{ TMOD=0X01;
TH0=0XF8;
TL0=0XCC;
TR0=1;
ET0=1;
}
uchar KeyScan()
{
if(key1==0) return KEY_1;
if(key2==0) return KEY_2;
if(key3==0) return KEY_3;
if(key4==0) return KEY_4;
if(key5==0) return KEY_5;
return KEY_NULL;
}
uchar GetKey()
{
uchar keyRetu=0,keyTemp=KEY_NULL;
static uchar s_keyState=KEY_STATE_INIT,keyTime=0,keyLast=KEY_NULL;
keyTemp=KeyScan();
switch (s_keyState)
{
case KEY_STATE_INIT:
if(keyTemp!=KEY_NULL)
{
s_keyState=KEY_STA TE_PRESS;
}
break;
case KEY_STATE_PRESS:
if(keyTemp!=KEY_NULL)
{
s_keyState=KEY_STA TE_LONG;
keyTime=0;
keyLast=keyTemp;
}
else
{
s_keyState=KEY_STA TE_INIT;
}
break;
case KEY_STATE_LONG:
if(keyTemp==KEY_NULL)
{
s_keyState=KEY_STA TE_INIT;
keyRetu=(keyLast|KEY_PRESS);
}
else
{
if(++keyTime>=KEY_LONG_PERIOD)
{
s_keyState=KEY_STATE_UP;
keyTime=0;
}
}
break;
case KEY_STATE_UP:
if(keyTemp==KEY_NULL)
{
s_keyState=KEY_STA TE_INIT;
}
else
{
if(++keyTime>=KEY_CONTINUE_PERIOD)
{
keyTime=0;
keyRetu=(keyLast|KEY_LONG);
}
}
break;
}
return keyRetu;
}
uchar bcdtoasc(uchar cha)
{
return ((cha/16)*10+(cha%16));
}
uchar asctobcd(uchar cha)
{
return ((cha/10)*16+(cha%10));
}
void timer() interrupt 1
{ static count=0;
static count1=0;
TH0=0XF8;
TL0=0XCC;
dao2MS=1;
count++;
if(++count1==10)
{ count1=0;
dao10MS=1;
}
if(!(count%25)) set = !set;
if(count==500)
{
count=0;
dao1S=1;
}
}
//------------------------------------- 常数宏 ---------------------------------//
#define DS1302_SECOND_WRITE 0x80 //写时钟芯片的寄存器位置
#define DS1302_MINUTE_WRITE 0x82
#define DS1302_HOUR_WRITE 0x84
#define DS1302_WEEK_WRITE 0x8A
#define DS1302_DAY_WRITE 0x86
#define DS1302_MONTH_WRITE 0x88
#define DS1302_YEAR_WRITE 0x8C
#define DS1302_SECOND_READ 0x81 //读时钟芯片的寄存器位置
#define DS1302_MINUTE_READ 0x83
#define DS1302_HOUR_READ 0x85
#define DS1302_WEEK_READ 0x8B
#define DS1302_DAY_READ 0x87
#define DS1302_MONTH_READ 0x89
#define DS1302_YEAR_READ 0x8D
//----------------------------------- 操作宏 ----------------------------------//
#define DS1302_SCLK_HIGH io_DS1302_SCLK = 1 ;
#define DS1302_SCLK_LOW io_DS1302_SCLK = 0 ;
#define DS1302_IO_HIGH io_DS1302_IO = 1 ;
#define DS1302_IO_LOW io_DS1302_IO = 0 ;
#define DS1302_IO_READ io_DS1302_IO
#define DS1302_RST_HIGH io_DS1302_RST = 1 ;
#define DS1302_RST_LOW io_DS1302_RST = 0 ;
/******************************************************
* 保存时间数据的结构体 *
******************************************************/
struct
{
uint8 Second ;
uint8 Minute ;
uint8 Hour ;
uint8 Day ;
uint8 Week ;
uint8 Month ;
uint8 Year ;
}CurrentTime ;
/******************************************************************************
* Function: static void v_DS1302Write_f( uint8 Content ) *
* Description: 向 DS1302 写一个字节的内容 *
* Parameter:uint8 Content : 要写的字节 *
* *
******************************************************************************/
void delay(uchar z)
{ uchar a,b;
for(a=z;a>0;a--)
for(b=110;b>0;b--);
}
static void v_DS1302Write_f( uint8 Content )
{
uint8 i ;
for( i = 8 ; i > 0 ; i-- )
{
if( Content & 0x01 )
{
DS1302_IO_HIGH
}
else
{
DS1302_IO_LOW
}
Content >>= 1 ;
DS1302_SCLK_HIGH
DS1302_SCLK_LOW
}
}
/******************************************************************************
* Function: static uint8 v_DS1302Read_f( void ) *
* Description: 从 DS1302 当前设定的地址读取一个字节的内容 *
* Parameter: *
* Return: 返回读出来的值 (uint8) *
******************************************************************************/
static uint8 v_DS1302Read_f( void )
{
uint8 i, ReadValue ;
DS1302_IO_HIGH
for( i = 8 ; i > 0 ; i-- )
{
ReadValue >>= 1 ;
if( DS1302_IO_READ )
{
ReadValue |= 0x80 ;
}
else
{
ReadValue &= 0x7f ;
}
DS1302_SCLK_HIGH
DS1302_SCLK_LOW
}
return ReadValue ;
}
/******************************************************************************
* Function: void v_DS1302WriteByte_f( uint8 Address, uint8 Content ) *
* Description: 从 DS1302 指定的地址写入一个字节的内容 *
* Parameter: Address: 要写入数据的地址 *
* Content: 写入数据的具体值 *
* Return:
*
******************************************************************************/
void v_DS1302WriteByte_f( uint8 Address, uint8 Content )
{
DS1302_RST_LOW
DS1302_SCLK_LOW
DS1302_RST_HIGH
v_DS1302Write_f( Address ) ;
v_DS1302Write_f( Content ) ;
DS1302_RST_LOW
DS1302_SCLK_HIGH
}
/******************************************************************************
* Function: uint8 v_DS1302ReadByte_f( uint8 Address ) *
* Description: 从 DS1302 指定的地址读出一个字节的内容 *
* Parameter:Address: 要读出数据的地址 *
* *
* Return: 指定地址读出的值 (uint8) *
******************************************************************************/
uint8 v_DS1302ReadByte_f( uint8 Address )
{
uint8 ReadValue ;
DS1302_RST_LOW
DS1302_SCLK_LOW
DS1302_RST_HIGH
v_DS1302Write_f( Address ) ;
ReadValue = v_DS1302Read_f() ;
DS1302_RST_LOW
DS1302_SCLK_HIGH
return ReadValue ;
}
/******************************************************************************
* Function: void v_ClockInit_f( void ) *
* Description: 初始化写入 DS1302 时钟寄存器的值 (主程序中只需调用一次即可 ) *
* Parameter:
*
* *
* Return: *
******************************************************************************/
void v_ClockInit_f( void )
{
if( v_DS1302ReadByte_f( 0xc1) != 0xf0 )
{
v_DS1302WriteByte_f( 0x8e, 0x00 ) ; //允许写操作
v_DS1302WriteByte_f( DS1302_YEAR_WRITE, 0x11 ) ; //年
v_DS1302WriteByte_f( DS1302_WEEK_WRITE, 0x01 ) ; //星期
v_DS1302WriteByte_f( DS1302_MONTH_WRITE, 0x01 ) ; //月
v_DS1302WriteByte_f( DS1302_DAY_WRITE, 0x03 ) ; //日
v_DS1302WriteByte_f( DS1302_HOUR_WRITE, 0x15 ) ; //小时
v_DS1302WriteByte_f( DS1302_MINUTE_WRITE, 0x21 ) ; //分钟
v_DS1302WriteByte_f( DS1302_SECOND_WRITE, 0x30 ) ; //秒
v_DS1302WriteByte_f( 0x90, 0xa5 ) ; //充电
v_DS1302WriteByte_f( 0xc0, 0xf0 ) ; //判断是否初始化一次标识写入
v_DS1302WriteByte_f( 0x8e, 0x80 ) ; //禁止写操作
}
}
/******************************************************************************
* Function: void v_ClockUpdata_f( void ) *
* Description: 读取时间数据 ,并保存在结构体 CurrentTime 中 *
* Parameter:
*
* *
* Return:
*
******************************************************************************/
void v_ClockUpdata_f( void )
{
CurrentTime.Second = v_DS1302ReadByte_f( DS1302_SECOND_READ ) ;
CurrentTime.Minute = v_DS1302ReadByte_f( DS1302_MINUTE_READ ) ;
CurrentTime.Hour = v_DS1302ReadByte_f( DS1302_HOUR_READ ) ;
CurrentTime.Day = v_DS1302ReadByte_f( DS1302_DAY_READ ) ;
CurrentTime.Month = v_DS1302ReadByte_f( DS1302_MONTH_READ ) ;
CurrentTime.Week = v_DS1302ReadByte_f( DS1302_WEEK_READ ) ;
CurrentTime.Year = v_DS1302ReadByte_f( DS1302_YEAR_READ ) ;
}
void updata()
{
miao = (((CurrentTime.Second&0x70)>>4)&0x0f)*10 + (CurrentTime.Second&0x0F);
fen = (((CurrentTime.Minute&0x70)>>4)&0x0f)*10 + (CurrentTime.Minute&0x0F);
shi = (((CurrentTime.Hour&0x30)>>4)&0x0f)*10 + (CurrentTime.Hour&0x0F);
day=(((CurrentTime.Day&0x30)>>4)&0x0f)*10 + (CurrentTime.Day&0x0F);
month=(((CurrentTime.Month&0x10)>>4)&0x0f)*10 + (CurrentTime.Month&0x0F);
week=CurrentTime.Week;
year=(((CurrentTime.Year&0xf0)>>4)&0x0f)*10 + (CurrentTime.Year&0x0F);
}
void displayriqi()
{ //因为当 LE 为 1 时,锁存器是透明的,应该先使 P0 清零,即一个段也不
被点亮
duan=1; // 打开段锁存器
P0=table[month%10]; // 将第 1 位的值放入锁存器 ,显示第 1 位
if(set&&yue)
{P0=0XFF;}
duan=0; //关闭段锁存器,将内容锁存
wei=1; //开位锁存器
P2=0x40; //位选通
wei=0; //关位锁存器
delay(10);
duan=1;
P0=table[month/10]; //显示第 2 位
if(set&&yue)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x80;
wei=0;
delay(10);
duan=1;
P0=table[11]; //显示 "_"
duan=0;
wei=1;
P2=0x20;
wei=0;
delay(10);
duan=1;
P0=table[day%10]; //显示第 3 位
if(set&&tian)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x08;
wei=0;
delay(10);
duan=1;
P0=table[day/10]; //显示第 4 位
if(set&&tian)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x10;
wei=0;
delay(10);
duan=1;
P0=table1[week];
if(set&&xq)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x01;
wei=0;
delay(10);
}
//if(set&&((k==main_flag-1)))
//{P0=0XFF;}
void dispalyshijian()
{
duan=1; // 打开段锁存器
P0=table[miao%10]; // 将第 1 位的值放入锁存器 ,显示第 1 位
if(set&&mm)
{P0=0XFF;}
duan=0; //关闭段锁存器,将内容锁存
wei=1; //开位锁存器
P2=0x01; //位选通
wei=0; //关位锁存器
delay(5);
duan=1;
P0=table[miao/10]; //显示第 2 位
if(set&&mm)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x02;
wei=0;
delay(5);
duan=1;
P0=table[11]; //显示 "_"
duan=0;
wei=1;
P2=0x04;
wei=0;
delay(5);
duan=1;
P0=table[fen%10]; //显示第 3 位
if(set&&ff)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x08;
wei=0;
delay(5);
duan=1;
P0=table[fen/10]; //显示第 4 位
if(set&&ff)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x10;
wei=0;
delay(5);
duan=1;
P0=table[11]; //显示 "_"
duan=0;
wei=1;
P2=0x20;
wei=0;
delay(5);
duan=1;
P0=table[shi%10]; //显示第 5 位
if(set&&ss)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x40;
wei=0;
delay(5);
duan=1;
P0=table[shi/10]; //显示第 6 位
if(set&&ss)
{P0=0XFF;}
duan=0;
wei=1;
P2=0x80;
wei=0;
// delay(5);
}
void main()
{ v_ClockInit_f();
nint();
EA=1;
while(1)
{ v_ClockUpdata_f();
updata();
if(main_flag==1)
displayriqi();
else
dispalyshijian();
if(dao10MS)
{
dao10MS=0;
switch (GetKey())
{
case (KEY_1|KEY_PRESS):
{
if(++main_flag>=2)
main_flag=0;
}
break;
case (KEY_2|KEY_PRESS):
{
switch (main_flag)
{
case 0:
{if(++tiaoshijian>=5)
tiaoshijian=0;
switch(tiaoshijian)
{
case 1:
{mm=0;
ss=1;
}
break;
case 2:
{ss=0;
ff=1;
}
break;
case 3:
{ff=0;
mm=1;
}
break;
case 4:
{ff=0;
mm=0;
ss=0;
}
break;
}
}
break;
case 1:
{if(++tiaoriqi>=5)
tiaoriqi=0;
switch(tiaoriqi)
{
case 1:
{xq=0;
yue=1;
}
break;
case 2:
{yue=0;
tian=1;
}
break;
case 3:
{tian=0;
xq=1;
}
break;
case 4:
{tian=0;
xq=0;
yue=0;
}
break;
}
}
}
}
break;
case (KEY_3|KEY_PRESS):
{
if((main_flag==0)&&(ss==1)) //调小时
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
shi=bcdtoasc(CurrentTime.Hour);
shi+=1;
if(shi>=24)
shi=0;
shi=asctobcd(shi);
v_DS1302WriteByte_f(0x84,shi);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Hour =
v_DS1302ReadByte_f( DS1302_HOUR_READ ) ;
shi=(((CurrentTime.Hour&0x30)>>4)&0x0f)*10 +
(CurrentTime.Hour&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==0)&&(ff==1)) //调分钟
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
fen=bcdtoasc(CurrentTime.Minute);
fen+=1;
if(fen>=60)
fen=0;
fen=asctobcd(fen);
v_DS1302WriteByte_f(0x82,fen);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Minute =
v_DS1302ReadByte_f(DS1302_MINUTE_READ ) ;
fen=(((CurrentTime.Minute&0x70)>>4)&0x0f)*10 +
(CurrentTime.Minute&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==0)&&(mm==1)) //调秒
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
miao=bcdtoasc(CurrentTime.Second);
miao+=1;
if(miao>=60)
miao=0;
miao=asctobcd(miao);
v_DS1302WriteByte_f(0x80,miao);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Second=
v_DS1302ReadByte_f(DS1302_SECOND_READ ) ;
miao=(((CurrentTime.Second&0x70)>>4)&0x0f)*10 +
(CurrentTime.Second&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(yue==1)) //调月份
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
month=bcdtoasc(CurrentTime.Month);
month+=1;
if(month>=13)
month=0;
month=asctobcd(month);
v_DS1302WriteByte_f(0x88,month);
CurrentTime.Month =
v_DS1302ReadByte_f( DS1302_MONTH_READ ) ;
month=(((CurrentTime.Month&0x10)>>4)&0x0f)*10 +
(CurrentTime.Month&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(tian==1)) //调日
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
day=bcdtoasc(CurrentTime.Day);
day+=1;
if(day>=32)
day=1;
day=asctobcd(day);
v_DS1302WriteByte_f(0x86,day);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Day =
v_DS1302ReadByte_f( DS1302_DAY_READ ) ;
day=(((CurrentTime.Day&0x30)>>4)&0x0f)*10 +
(CurrentTime.Day&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(xq==1)) //调星期
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
week=bcdtoasc(CurrentTime.Week);
week+=1;
if(week>=8)
week=1;
week=asctobcd(week);
v_DS1302WriteByte_f(0x8a,week);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Week =
v_DS1302ReadByte_f( DS1302_WEEK_READ ) ;
week=CurrentTime.Week;
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
};
}
break;
case (KEY_3|KEY_LONG):
{
if((main_flag==0)&&(ss==1)) //调小时
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
shi=bcdtoasc(CurrentTime.Hour);
shi+=1;
if(shi>=24)
shi=0;
shi=asctobcd(shi);
v_DS1302WriteByte_f(0x84,shi);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Hour =
v_DS1302ReadByte_f( DS1302_HOUR_READ ) ;
shi=(((CurrentTime.Hour&0x30)>>4)&0x0f)*10 +
(CurrentTime.Hour&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==0)&&(ff==1)) //调分钟
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
fen=bcdtoasc(CurrentTime.Minute);
fen+=1;
if(fen>=60)
fen=0;
fen=asctobcd(fen);
v_DS1302WriteByte_f(0x82,fen);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Minute =
v_DS1302ReadByte_f(DS1302_MINUTE_READ ) ;
fen=(((CurrentTime.Minute&0x70)>>4)&0x0f)*10 +
(CurrentTime.Minute&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==0)&&(mm==1)) //调秒
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
miao=bcdtoasc(CurrentTime.Second);
miao+=1;
if(miao>=60)
miao=0;
miao=asctobcd(miao);
v_DS1302WriteByte_f(0x80,miao);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Second=
v_DS1302ReadByte_f(DS1302_SECOND_READ ) ;
miao=(((CurrentTime.Second&0x70)>>4)&0x0f)*10 +
(CurrentTime.Second&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(yue==1)) //调月份
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
month=bcdtoasc(CurrentTime.Month);
month+=1;
if(month>=13)
month=0;
month=asctobcd(month);
v_DS1302WriteByte_f(0x88,month);
CurrentTime.Month =
v_DS1302ReadByte_f( DS1302_MONTH_READ ) ;
month=(((CurrentTime.Month&0x10)>>4)&0x0f)*10 +
(CurrentTime.Month&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(tian==1)) //调日
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
day=bcdtoasc(CurrentTime.Day);
day+=1;
if(day>=32)
day=1;
day=asctobcd(day);
v_DS1302WriteByte_f(0x86,day);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Day =
v_DS1302ReadByte_f( DS1302_DAY_READ ) ;
day=(((CurrentTime.Day&0x30)>>4)&0x0f)*10 +
(CurrentTime.Day&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(xq==1)) //调星期
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
week=bcdtoasc(CurrentTime.Week);
week+=1;
if(week>=8)
week=1;
week=asctobcd(week);
v_DS1302WriteByte_f(0x8a,week);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Week =
v_DS1302ReadByte_f( DS1302_WEEK_READ ) ;
week=CurrentTime.Week;
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
};
}
break;
case (KEY_4|KEY_PRESS):
{
if((main_flag==0)&&(ss==1)) //调小时
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
shi=bcdtoasc(CurrentTime.Hour);
shi-=1;
if(shi<0)
shi=23;
shi=asctobcd(shi);
v_DS1302WriteByte_f(0x84,shi);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Hour =
v_DS1302ReadByte_f( DS1302_HOUR_READ ) ;
shi=(((CurrentTime.Hour&0x30)>>4)&0x0f)*10 +
(CurrentTime.Hour&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==0)&&(ff==1)) //调分钟
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
fen=bcdtoasc(CurrentTime.Minute);
fen-=1;
if(fen<0)
fen=59;
fen=asctobcd(fen);
v_DS1302WriteByte_f(0x82,fen);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Minute =
v_DS1302ReadByte_f(DS1302_MINUTE_READ ) ;
fen=(((CurrentTime.Minute&0x70)>>4)&0x0f)*10 +
(CurrentTime.Minute&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==0)&&(mm==1)) //调秒
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
miao=bcdtoasc(CurrentTime.Second);
miao-=1;
if(miao<0)
miao=59;
miao=asctobcd(miao);
v_DS1302WriteByte_f(0x80,miao);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Second=
v_DS1302ReadByte_f(DS1302_SECOND_READ ) ;
miao=(((CurrentTime.Second&0x70)>>4)&0x0f)*10 +
(CurrentTime.Second&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(yue==1)) //调月份
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
month=bcdtoasc(CurrentTime.Month);
month-=1;
if(month<1)
month=12;
month=asctobcd(month);
v_DS1302WriteByte_f(0x88,month);
CurrentTime.Month =
v_DS1302ReadByte_f( DS1302_MONTH_READ ) ;
month=(((CurrentTime.Month&0x10)>>4)&0x0f)*10 +
(CurrentTime.Month&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(tian==1)) //调日
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
day=bcdtoasc(CurrentTime.Day);
day-=1;
if(day<1)
day=31;
day=asctobcd(day);
v_DS1302WriteByte_f(0x86,day);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Day =
v_DS1302ReadByte_f( DS1302_DAY_READ ) ;
day=(((CurrentTime.Day&0x30)>>4)&0x0f)*10 +
(CurrentTime.Day&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(xq==1)) //调星期
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
week=bcdtoasc(CurrentTime.Week);
week-=1;
if(week<1)
week=7;
week=asctobcd(week);
v_DS1302WriteByte_f(0x8a,week);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Week =
v_DS1302ReadByte_f( DS1302_WEEK_READ ) ;
week=CurrentTime.Week;
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
};
}
break;
case (KEY_4|KEY_LONG):
{
if((main_flag==0)&&(ss==1)) //调小时
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
shi=bcdtoasc(CurrentTime.Hour);
shi-=1;
if(shi<0)
shi=23;
shi=asctobcd(shi);
v_DS1302WriteByte_f(0x84,shi);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Hour =
v_DS1302ReadByte_f( DS1302_HOUR_READ ) ;
shi=(((CurrentTime.Hour&0x30)>>4)&0x0f)*10 +
(CurrentTime.Hour&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==0)&&(ff==1)) //调分钟
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
fen=bcdtoasc(CurrentTime.Minute);
fen-=1;
if(fen<0)
fen=59;
fen=asctobcd(fen);
v_DS1302WriteByte_f(0x82,fen);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Minute =
v_DS1302ReadByte_f(DS1302_MINUTE_READ ) ;
fen=(((CurrentTime.Minute&0x70)>>4)&0x0f)*10 +
(CurrentTime.Minute&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==0)&&(mm==1)) //调秒
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
miao=bcdtoasc(CurrentTime.Second);
miao-=1;
if(miao<0)
miao=59;
miao=asctobcd(miao);
v_DS1302WriteByte_f(0x80,miao);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Second=
v_DS1302ReadByte_f(DS1302_SECOND_READ ) ;
miao=(((CurrentTime.Second&0x70)>>4)&0x0f)*10 +
(CurrentTime.Second&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(yue==1)) //调月份
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
month=bcdtoasc(CurrentTime.Month);
month-=1;
if(month<1)
month=12;
month=asctobcd(month);
v_DS1302WriteByte_f(0x88,month);
CurrentTime.Month =
v_DS1302ReadByte_f( DS1302_MONTH_READ ) ;
month=(((CurrentTime.Month&0x10)>>4)&0x0f)*10 +
(CurrentTime.Month&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(tian==1)) //调日
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
day=bcdtoasc(CurrentTime.Day);
day-=1;
if(day<1)
day=31;
day=asctobcd(day);
v_DS1302WriteByte_f(0x86,day);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Day =
v_DS1302ReadByte_f( DS1302_DAY_READ ) ;
day=(((CurrentTime.Day&0x30)>>4)&0x0f)*10 +
(CurrentTime.Day&0x0F);
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
}
else if((main_flag==1)&&(xq==1)) //调星期
{ v_DS1302WriteByte_f(0x8e,0x00);
//关写保护
week=bcdtoasc(CurrentTime.Week);
week-=1;
if(week<1)
week=7;
week=asctobcd(week);
v_DS1302WriteByte_f(0x8a,week);
//write_add(0x04,shi);// 写分数据
// shi=read_add(0x04)&0x3f;// 读取秒数据
CurrentTime.Week =
v_DS1302ReadByte_f( DS1302_WEEK_READ ) ;
week=CurrentTime.Week;
v_DS1302WriteByte_f(0x8e,0x80);
//开写保护
dispalyshijian();
};
}
break;
case (KEY_5|KEY_PRESS):
{main_flag=0;
ss=0;
mm=0;
ff=0;
yue=0;
tian=0;
xq=0;
dispalyshijian();
}
break;
}
}
}}
|
|