1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
#include <at89x52.h>
#define uchar unsigned char #define uint unsigned int
uint tt=0; uint i;
uint time_table[6]={2000,1,1,0,0,0};
void display_time_day(uint cl_year,uint cl_month,uint cl_date,uint d_local); void display_time_clock(uint cl_hour,uint cl_min,uint cl_sec,uint c_local); void display_time_week(uint cl_week,uint w_local); uint judge_date(uint jd_month,uint jd_year); uint judge_week(uint jd_w_day,uint jd_w_month,uint jd_w_year); void set_time();
void _1602_busy(); void _1602_init(); void _1602_command(uint com); void _1602_data(uint data);
#define LED_IO P0
__sbit __at 0xA6 _LED_RS; __sbit __at 0xA5 _LED_RW; __sbit __at 0xA7 _LED_E;
uchar dis_wel[]="Welcome!"; uchar dis_blog[]="www.erf172.tk"; uchar dis_set[]="SETTIME";
uchar dis_week[]="SunMonTueWedThuFriSat";
__sbit __at 0x97 _KEY_P; __sbit __at 0x95 _KEY_D; __sbit __at 0xB5 _KEY_NEXT;
int main() {
EA=1; TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; ET0=1; TR0=1;
_1602_init(); _1602_command(0x80); while(dis_blog[i]!='\0') { _1602_data(dis_blog[i]); i++; }
_1602_command(0xc0); i=0; while(dis_wel[i]!='\0') { _1602_data(dis_wel[i]); i++; } while(tt!=80);
_1602_command(0x01); set_time(); tt=0; while (1) { if(tt==20) { tt=0; time_table[5]++; if (time_table[5]==60) { time_table[5]=0; time_table[4]++; if (time_table[4]==60) { time_table[4]=0; time_table[3]++; if (time_table[3]==24) { time_table[3]=0; time_table[2]++; if (time_table[2]==judge_date(time_table[1],time_table[0])) { time_table[2]=1; time_table[1]++; if (time_table[1]==13) { time_table[1]=1; time_table[0]++; } } } } } display_time_day(time_table[0],time_table[1],time_table[2],0x83); display_time_clock(time_table[3],time_table[4],time_table[5],0xc6); uint week=judge_week(time_table[2],time_table[1],time_table[0]); display_time_week(week,0xc2); } }
}
void set_time() { i=0; uint time_table_simple[6]={2100,12,31,23,59,59,59}; _1602_command(0x80);
while(i<3) { _1602_data(dis_set[i]); i++; }
_1602_command(0xc0); while(dis_set[i]!='\0') { _1602_data(dis_set[i]); i++; }
display_time_day(time_table[0],time_table[1],time_table[2],0x86); display_time_clock(time_table[3],time_table[4],time_table[5],0xc8);
for ( i = 0; i < 6; i++) {
time_table_simple[3]=judge_date(time_table[1],time_table[0])-1;
while (_KEY_NEXT) { while (_KEY_P&&_KEY_NEXT&&_KEY_D); if(!_KEY_P) time_table[i]++; if(!_KEY_D) time_table[i]--; if(time_table[i]>time_table_simple[i]||time_table[i]==0&&i<3) time_table[i]=1; if(time_table[i]>time_table_simple[i]&&i>2) time_table[i]=0; display_time_day(time_table[0],time_table[1],time_table[2],0x86); display_time_clock(time_table[3],time_table[4],time_table[5],0xc8); while (!_KEY_P||!_KEY_D); if (!_KEY_NEXT) break; } while (!_KEY_NEXT); } _1602_command(0x01); }
void display_time_day(uint cl_year,uint cl_month,uint cl_date,uint d_local) { uint dis_date_table[10]={0,0,0,0,0x2D,0,0,0x2D,0,0}; uint i=0;
dis_date_table[0]=cl_year/1000+0x30; dis_date_table[1]=cl_year%1000/100+0x30; dis_date_table[2]=cl_year%1000%100/10+0x30; dis_date_table[3]=cl_year%1000%100%10+0x30;
dis_date_table[5]=cl_month/10+0x30; dis_date_table[6]=cl_month%10+0x30;
dis_date_table[8]=cl_date/10+0x30; dis_date_table[9]=cl_date%10+0x30;
_1602_command(d_local);
for ( i = 0; i < 10; i++) { _1602_data(dis_date_table[i]); }
}
void display_time_clock(uint cl_hour,uint cl_min,uint cl_sec,uint c_local) { uint dis_clock_table[]={0,0,0x3A,0,0,0x3A,0,0}; uint i=0;
dis_clock_table[0]=cl_hour/10+0x30; dis_clock_table[1]=cl_hour%10+0x30;
dis_clock_table[3]=cl_min/10+0x30; dis_clock_table[4]=cl_min%10+0x30;
dis_clock_table[6]=cl_sec/10+0x30; dis_clock_table[7]=cl_sec%10+0x30;
_1602_command(c_local); for ( i = 0; i < 8; i++) { _1602_data(dis_clock_table[i]); }
}
void display_time_week(uint cl_week,uint w_local) {
_1602_command(w_local); uint j=0; j=cl_week*3; while(j<=cl_week*3+2) { _1602_data(dis_week[j]); j++; }
}
uint judge_date(uint jd_month,uint jd_year) { uint jd_date=0; switch (jd_month) { case 1: jd_date=31; break; case 2: { if((jd_year%4==0&&jd_year%100!=0)||jd_year%400==0) jd_date=29; else jd_date=28; break; } case 3: jd_date=31; break; case 4: jd_date=30; break; case 5: jd_date=31; break; case 6: jd_date=30; break; case 7: jd_date=31; break; case 8: jd_date=31; break; case 9: jd_date=30; break; case 10: jd_date=31; break; case 11: jd_date=30; break; case 12: jd_date=31; break; default: break; } return jd_date; }
uint judge_week(uint jd_w_day,uint jd_w_month,uint jd_w_year) {
uint Year_Sum=0,Month_Sum=0,This_Year_Month=0,jd_w_week=0; Year_Sum=(jd_w_year-1900)+(jd_w_year-1901)/4;
for (This_Year_Month = 1; This_Year_Month < jd_w_month; This_Year_Month++) { Month_Sum=Month_Sum+judge_date(This_Year_Month, jd_w_year); }
jd_w_week=(Year_Sum+jd_w_day+Month_Sum)%7;
return jd_w_week; }
void _1602_init() { P1=P1&0xf0; _1602_command(0x38); _1602_command(0x0c); _1602_command(0x06); _1602_command(0x01);
}
void _1602_command(uint com) { _1602_busy(); _LED_RS=0; _LED_RW=0; LED_IO=com; _LED_E=1; _LED_E=0;
}
void _1602_data(uint data) { _1602_busy(); _LED_RS=1; _LED_RW=0; LED_IO=data; _LED_E=1; _LED_E=0;
}
void _1602_busy() { unsigned char sta; LED_IO = 0xff; _LED_RS = 0; _LED_RW = 1; do { _LED_E = 1; sta = LED_IO; _LED_E = 0; }while(sta & 0x80); }
void exter1() __interrupt 1 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; tt++; }
|