//break;case 6: //星期設(shè)置LCD_init();//清屏while(!(h==2))//再次按選擇鍵就進(jìn)入下個(gè)項(xiàng)目的設(shè)置{if(key_press()){h=key_scan(); //掃描并返回翻譯后的鍵碼if(h==1)time_data[1]++;//按一下小時(shí)加一if(time_data[1]==0x08)//一天24小時(shí)time_data[1]=1;}write_ds1302(write_add[2],time_data[2]);time_pro();//時(shí)間處理showweek();//顯示星期} default:LCD_init();//清屏showmessage();break;} }}}write_ds1302(0x8e,0x80);//(wp位置1)加入寫保護(hù)write_ds1302(0x80,0x00);//時(shí)鐘開始LCD_init();//清屏SEI();}key.c:#include iom16v.h>#include macros.h>#include "delay.h"#define uchar unsigned char#define uintn月設(shè)置LCD_init();//清屏while(!(m==2))//再次按選擇鍵就進(jìn)入下個(gè)項(xiàng)目的設(shè)置{if(key_press()){m=key_scan(); //掃描并返回翻譯后的鍵碼if(m==1)time_data[2]++;//按一下月加一if(time_data[2]==0x13)//一年有12個(gè)月time_data[2]=1;}//BCD碼處理if(time_data[2]==0x0a){time_data[2]=0x10;}if(time_data[2]==0x1a){time_data[2]=0x20;} write_ds1302(write_add[2],time_data[2]);time_pro();//時(shí)間處理showmonth();//顯示月}//break;case 3: //日設(shè)置LCD_init();//清屏while(!(u==2))//再次按選擇鍵就進(jìn)入下個(gè)項(xiàng)目的設(shè)置{if(key_press()){u=key_scan(); //掃描并返回翻譯后的鍵碼if(u==1)time_data[3]++;//按一下日加一if(time_data[3]==0x32)//一個(gè)月最多有31天time_data[3]=1;}//BCD碼處理if(time_data[3]==0x0a){time_data[3]=0x10;}if(time_data[3]==0x1a){time_data[3]=0x20;} if(time_data[3]==0x2a){time_data[3]=0x30;} write_ds1302(write_add[3],time_data[3]);time_pro();//時(shí)間處理showdaytime();//顯示日}//break;case 4: //小時(shí)設(shè)置LCD_init();//清屏while(!(k==2))//再次按選擇鍵就進(jìn)入下個(gè)項(xiàng)目的設(shè)置{if(key_press()){k=key_scan(); //掃描并返回翻譯后的鍵碼if(k==1)time_data[4]++;//按一下小時(shí)加一if(time_data[4]==0x25)//一天24小時(shí)time_data[4]=1;}//BCD碼處理if(time_data[4]==0x0a){time_data[4]=0x10;}if(time_data[4]==0x1a){time_data[4]=0x20;} write_ds1302(write_add[4],time_data[4]);time_pro();//時(shí)間處理showhour();//顯示小時(shí)} //break; case 5: //分鐘設(shè)置LCD_init();//清屏while(!(j==2))//再次按選擇鍵就進(jìn)入下個(gè)項(xiàng)目的設(shè)置{if(key_press()){j=key_scan(); //掃描并返回翻譯后的鍵碼if(j==1)time_data[5]++;//按一下分鐘加一if(time_data[5]==0x5a)//一小時(shí)60分鐘time_data[5]=0x00;}//BCD碼處理if(time_data[5]==0x0a){time_data[5]=0x10;}if(time_data[5]==0x1a){time_data[5]=0x20;}if(time_data[5]==0x2a){time_data[5]=0x30;}if(time_data[5]==0x3a){time_data[5]=0x40;}if(time_data[5]==0x4a){time_data[5]=0x50;}if(time_data[5]==0x5a){time_data[5]=0x60;} write_ds1302(write_add[5],time_data[5]);time_pro();//時(shí)間處理showminute();//顯示分鐘} //break;case 6: //星期設(shè)置LCD_init();//清屏while(!(h==2))//再次按選擇鍵就進(jìn)入下個(gè)項(xiàng)目的設(shè)置{if(key_press()){h=key_scan(); //掃描并返回翻譯后的鍵碼if(h==1)time_data[1]++;//按一下小時(shí)加一if(time_data[1]==0x08)//一天24小時(shí)time_data[1]=1;}write_ds1302(write_add[2],time_data[2]);time_pro();//時(shí)間處理showweek();//顯示星期} default:LCD_init();//清屏showmessage();break;} }}}write_ds1302(0x8e,0x80);//(wp位置1)加入寫保護(hù)write_ds1302(0x80,0x00);//時(shí)鐘開始LCD_init();//清屏SEI();}key.c:#include iom16v.h>#include macros.h>#include "delay.h"#define uchar unsigned char#define uint unsigned intuchar key_press()//判斷按鍵是否被按下(按下返回1,沒按下返回0){ uchar j;DDRD|=0X0F;//低四位為輸出PORTD|=0X0F;//低四位輸出低電平DDRD=0XF0;//低四位為輸入j=PIND;//讀一下輸入寄存器j=0X0F;//屏蔽高四位if(j==0X0F)//如果j沒變化,則返回0,否則返回1{return 0;}else{return 1;}}uchar key_scan()//按鍵掃描函數(shù){ uchar key;delay(10);if(key_press())//有按鍵按下時(shí)key_press()的值為1{key=PIND;//讀取PIND值key=0X0F;//去掉高4位switch(key)//開關(guān)語句,用于判斷{case 0X0E://1110連接PD0的按鍵按下key=1;break;case 0X0D://1101連接PD1的按鍵按下key=2;break; case 0X0B://1011連接PD2的按鍵按下key=3;break;case 0X07://0111連接PD3的按鍵按下key=4;break;default://都不是時(shí)隨便返回一個(gè)值key=16; }while(key_press());//在按鍵按下時(shí)執(zhí)行完上邊的程序后就執(zhí)行此循環(huán)//當(dāng)按鍵松開時(shí)就跳出循環(huán),目的是按一下就執(zhí)行一次程序}else//沒有按下時(shí)隨便返回一個(gè)值{key=16;}return key;}程序未完,完整代碼下載: http://www.51hei.com/f/1302cc.rar
評論