一、利用的MCU为STC89C52RC
二、DS1302驱动
[code] 1 #include 2 #include "DS1302.h" 3 #include "LCD1602.h" 4 #include "Delay.h" 5 #include "Timer0.h" 6 #include "SKey.h" 7 8 9 void setMode1(); 10 void setMode2(); 11 void Show_Time(); 12 void case0(); 13 void case1(); 14 void case2(); 15 void case3(); 16 void case4(); 17 void flashChoose(); 18 //启动设置时间的入口 19 char times[] = {0x24, 0x11, 0x24 , 0x19,0x56,0x32, 0x07}; 20 //秒前面的冒号的显隐开关 21 unsigned char SecondFlashFlag = 0x01; 22 //修改时间时控制选择的位置的闪烁开关 23 unsigned char chooseFlashFlag = 0x01; 24 //模式: 0默认,1,显示模式 2,修改模式 25 unsigned int MODE = 0; 26 //通过独立键盘来控制时钟:1显示; 2修改 3加,4减。 27 unsigned char KeyN = 0; 28 //选择要修改的位置: 年月日时分秒(1-6) 29 char selectLoc = 0; 30 //要修改时存放时间的临时数组。 31 unsigned char tempArr[7] ; 32 //标记是否修改的标志 0 未修改 1 修改了 33 unsigned char modifyFlag = 0; 34 void main() 35 { 36 37 LCD_Init(); 38 39 LCD_ShowString(1,1," - - "); 40 LCD_ShowString(2,1," : : "); 41 42 Timer0Init(); 43 //初始化DS1302 44 DS1302_Init(); 45 //DS1302_SetTime2(times); 46 DS1302_ReadTime(); 47 48 while(1){ 49 50 KeyN = getSKey(); 51 if(KeyN!=0){ 52 if(KeyN == 1){ setMode1();} 53 if(KeyN == 2){ setMode2();} 54 //LCD_ShowNum(2,12,KeyN,2); 55 56 } 57 58 switch(MODE){ 59 case 0: 60 case 1: case1(); break; 61 case 2: case2(); break; 62 63 } 64 65 switch(KeyN){ 66 67 case 3: case3(); break; 68 case 4: case4(); break; 69 70 } 71 72 73 Delay(400); 74 75 } 76 77 } 78 79 80 void case0(){ 81 Show_Time(); 82 } 83 84 void case1(){ 85 selectLoc = 0; 86 Show_Time(); 87 88 } 89 90 void case2(){ 91 92 SecondFlashFlag = 1; 93 chooseFlashFlag = !chooseFlashFlag; 94 flashChoose(); 95 } 96 97 98 /** 99 * @bref 对要修改的区域进行闪烁操作100 * @param 无101 * @retval 无102 */103 void flashChoose(){104 LCD_ShowString(1,1," - - ");105 LCD_ShowString(2,1," : : ");106 LCD_ShowNum(1,1,tempArr[0]/16*10 + tempArr[0]%16, 2); 107 LCD_ShowNum(1,4,tempArr[1]/16*10 + tempArr[1]%16, 2); 108 LCD_ShowNum(1,7,tempArr[2]/16*10 + tempArr[2]%16, 2); 109 LCD_ShowNum(2,1,tempArr[3]/16*10 + tempArr[3]%16, 2); 110 LCD_ShowNum(2,4,tempArr[4]/16*10 + tempArr[4]%16, 2); 111 LCD_ShowNum(2,7,tempArr[5]/16*10 + tempArr[5]%16, 2);112 113 114 switch(selectLoc){115 case 1: {116 if(chooseFlashFlag){117 LCD_ShowString(1,1," ");118 }119 break;120 }121 case 2: {122 if(chooseFlashFlag){123 LCD_ShowString(1,4," "); 124 }125 break;126 }127 case 3: {128 if(chooseFlashFlag){129 LCD_ShowString(1,7," "); 130 }131 break;132 } 133 case 4: {134 if(chooseFlashFlag){135 LCD_ShowString(2,1," "); 136 }137 break;138 }139 case 5: {140 if(chooseFlashFlag){141 LCD_ShowString(2,4," "); 142 }143 break;144 }145 case 6: {146 if(chooseFlashFlag){147 LCD_ShowString(2,7," "); 148 }149 break;150 } 151 152 }153 154 }155 156 157 158 void case3(){159 if(MODE == 2){160 unsigned int temp = 0;161 162 switch(selectLoc){163 case 1:{164 //年165 temp = tempArr[0]/16*10 + tempArr[0]%16;166 temp++;167 if(temp>99){168 temp=1;169 } 170 tempArr[0] = (temp/10)29){206 temp=1;207 }208 }else{209 if(temp>28){210 temp=1;211 }212 }213 214 }215 tempArr[2] = (temp/10) |