void outkey() //P1.4 { uchar Second; if(out==0) { delay(10); if(out==0) { while(out==0); } count=0; //count_up=0; //count_down=0; hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0; Second=Read1302(DS1302_SECOND); write1302(0x8e,0x00); //寫入允許 write1302(0x80,Second&0x7f); write1302(0x8E,0x80); //禁止寫入 done=0; } }
//************************************************************************** //升序按鍵****************************************************************** //************************************************************************** void Upkey() //P1.2 { // static char count_up=0; Up=1; if(Up==0) { delay(10); if(Up==0) { //count_up=count_up+1; / tkey按一次,count就加1 count++; P21=~P21; done=1; //進入調整模式 while(Up==0); } switch(count_up) { case 1: P01=~P01; temp=Read1302(DS1302_SECOND); //讀取秒數 temp=((temp&0x70)>>4)*10 + (temp&0x0F); temp=temp+1; //秒數加1 up_flag=1; //數據調整后更新標志 if((temp)>59) //超過59秒,清零 { temp=0; } temp=temp/10*16+temp%10; break; case 2: P02=~P02; temp=Read1302(DS1302_MINUTE); //讀取分數 temp=((temp&0x70)>>4)*10 + (temp&0x0F); temp=temp+1; //分數加1 up_flag=1; if(temp>59) //超過59分,清零 { temp=0; } temp=temp/10*16+temp%10; break; case 3:
case 4:
default:break; } // while(Up==0); } }
//********************************************************************** //降序按鍵************************************************************** //********************************************************************** void Downkey() //P1.0 { //static char count_down=0; Down=1; if(Down==0) { delay(10); if(Down==0) { //count_down=count_down+1; / tkey按一次,count就加1 count++; done=1; //進入調整模式 while(Down==0); //P01=~P01; } switch(count) { case 1: temp=Read1302(DS1302_SECOND); //讀取秒數 temp=((temp&0x70)>>4)*10 + (temp&0x0F); temp=temp-1; //秒數減1 down_flag=1; //數據調整后更新標志 if(temp==-1) //小于0秒,返回59秒 temp=59; temp=temp/10*16+temp%10; break;
case 2: temp=Read1302(DS1302_MINUTE); //讀取分數 temp=((temp&0x70)>>4)*10 + (temp&0x0F); temp=temp-1; //分數減1 down_flag=1; if(temp==-1) temp=59; //小于0秒,返回59秒 temp=temp/10*16+temp%10; break;
case 3:
case 4: case 5:
default:break; }
// while(Down==0); } }
void show_time() //數碼管顯示程序 { // DS1302_GetTime(&CurrentTime); //獲取時鐘芯片的時間數據 // Print(CurrentTime.TimeString); //顯示時間 // GotoXY(0,0); // Print(CurrentTime.DateString); //顯示日期 // GotoXY(15,0); // Print(week_value); //顯示星期 // GotoXY(11,0); // Print("Week"); //在液晶上顯示 字母 week delay(400); //掃描延時 }
//**************************************************** //**************模式選擇按鍵************************** //**************************************************** void Setkey() { Set=1; if(Set==0) //P1.1 { delay(10); if(Set==0) { //static char count=0; count=count+1; / tkey按一次,count就加1 P13=~P13; P20=~P20; done=1; //進入調整模式 while(Set==0); } }
//***************************************************** //**************按鍵功能執行*************************** //***************************************************** void keydone() { uchar Second; /* if(flag==0) //關閉時鐘,停止計時 { Write1302(0x8e,0x00); //寫入允許 temp=Read1302(0x80); Write1302(0x80,temp|0x80); Write1302(0x8e,0x80); //禁止寫入 flag=1; }*/ Setkey(); //掃描模式切換按鍵 switch(count) { case 1:do //count=1,調整秒 { outkey(); //掃描跳出按鈕 Upkey(); //掃描加按鈕 Downkey(); //掃描減按鈕 if(up_flag==1||down_flag==1) //數據更新,重新寫入新的數據 { write1302(0x8e,0x00); //寫入允許 write1302(0x80,temp); //寫入新的秒數 write1302(0x8e,0x80); //禁止寫入 up_flag=0; down_flag=0; } if(Down!=0&&Up!=0) { hide_sec++; if(hide_sec>3) hide_sec=0; } else hide_sec=0; show_time(); //顯示數據 }while(count==2);break; case 2:do //count=2,調整分 { P22=~P22; hide_sec=0; outkey(); Upkey(); Downkey(); if(temp>0x60) temp=0; if(up_flag==1||down_flag==1) { write1302(0x8e,0x00); //寫入允許 write1302(0x82,temp); //寫入新的分數 write1302(0x8e,0x80); //禁止寫入 up_flag=0; down_flag=0; } if(Down!=0&&Up!=0) { hide_min++; if(hide_min>3) hide_min=0; } else hide_min=0; show_time(); }while(count==3);break; case 8: count=0;hide_year=0; //count8, 跳出調整模式,返回默認顯示狀態 Second=Read1302(DS1302_SECOND); write1302(0x8e,0x00); //寫入允許 write1302(0x80,Second&0x7f); write1302(0x8E,0x80); //禁止寫入 done=0; break; //count=7,開啟中斷,標志位置0并退出 default:break;
}
} void main(void) { flag=1; //時鐘停止標志 P2=0x00; //消隱 // P1=0xff; up_flag=0; //數據是否更新標志 down_flag=0; //數據是否更新標志 Initial_inti_time(); //中斷初始化 Initial_DS1302(); //時鐘芯片DS_1302初始化 while(1) { // while(done==0) // while(done==1) keydone(); //進入調整模式 { show_time(); //數碼管顯示數據 flag=0; Setkey(); //掃描各功能鍵 } } }
|