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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
| #include <REGX52.H> #include "intrins.h"
#define LCD_RS P2_6 #define LCD_RW P2_5 #define LCD_EN P2_7 #define LCD_DataPort P0
#define DS1302_SCLK P3_6 #define DS1302_IO P3_4 #define DS1302_CE P3_5
#define OneWire_DQ P3_7
#define DS1302_SECOND 0x80 #define DS1302_MINUTE 0x82 #define DS1302_HOUR 0x84 #define DS1302_DATE 0x86 #define DS1302_MONTH 0x88 #define DS1302_DAY 0x8A #define DS1302_YEAR 0x8C #define DS1302_WP 0x8E
unsigned char Key();
void Delay(unsigned int xms);
void Timer0Init(void);
void CheckAlarm();
void DS1302_Init(void); void DS1302_WriteByte(unsigned char Command,Data); unsigned char DS1302_ReadByte(unsigned char Command); void DS1302_SetTime(void); void DS1302_ReadTime(void);
void DS18B20_ConvertT(void); float DS18B20_ReadT(void); void DS18B20_Init(void);
unsigned char OneWire_Init(void); void OneWire_SendBit(unsigned char Bit); unsigned char OneWire_ReceiveBit(void); void OneWire_SendByte(unsigned char Byte); unsigned char OneWire_ReceiveByte(void);
#define DS18B20_SKIP_ROM 0xCC #define DS18B20_CONVERT_T 0x44 #define DS18B20_READ_SCRATCHPAD 0xBE
char DS1302_Time[]={25,01,01,11,31,55,3};
char AlarmTime[] = {25,01,01,11,31,59,3};
unsigned char KeyNum,MODE,TimeSetSelect,TimeSetFlashFlag,AlarmFlag,BuzzerState,BuzzerCount,StopwatchMode,StopwatchState; unsigned int StopwatchTime = 0; float T; #define Buzzer P1_5
unsigned char CalculateWeekday(int year, int month, int day);
void LCD_Init(); void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char); void LCD_ShowString(unsigned char Line,unsigned char Column,char *String); void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length); int LCD_Pow(int X,int Y);
void TimeSet(char* TimeArray,unsigned char* SelectPtr); void TimeShow(void);
void ShowStopwatch(); void StopwatchControl();
void Show_Temperature();
void main() { LCD_Init(); DS1302_Init(); DS18B20_ConvertT(); DS18B20_Init(); Timer0Init(); LCD_ShowString(1,1," - - "); LCD_ShowString(2,1," : : "); DS1302_Time[6] = CalculateWeekday(DS1302_Time[0], DS1302_Time[1], DS1302_Time[2]); DS1302_SetTime(); while(1) { KeyNum=Key(); if(KeyNum==1) { if(MODE==0){MODE=1;TimeSetSelect=0;} else if(MODE==1){MODE=0;DS1302_SetTime();} else if(MODE==3){MODE=0;} } if(KeyNum==5){ if(MODE==0){ MODE=2; LCD_Init(); LCD_ShowString(1,1,"Stopwatch"); StopwatchTime = 0; StopwatchState = 0; } else if(MODE==2){ MODE=3; LCD_Init(); LCD_ShowString(1,1," - - "); LCD_ShowString(2,1," : : "); } else if(MODE == 3) { MODE=0; LCD_Init(); LCD_ShowString(1,1," - - "); LCD_ShowString(2,1," : : "); } } switch(MODE) { case 0:TimeShow();CheckAlarm();Show_Temperature();break; case 1:TimeSet(DS1302_Time,&TimeSetSelect);break; case 2:StopwatchControl();break; case 3:TimeSet(AlarmTime,&TimeSetSelect);LCD_ShowString(2, 10, "W:");LCD_ShowString(1, 10, "SETTING");break; } if (BuzzerState) { BuzzerCount++; if (BuzzerCount >= 50) { BuzzerState = 0; BuzzerCount = 0; Buzzer = 0; } Buzzer = !Buzzer; Delay(5); Buzzer = !Buzzer; Delay(5); Buzzer = !Buzzer; Delay(5); } else { Buzzer = 0; } } }
void Show_Temperature(){ DS18B20_ConvertT(); T=DS18B20_ReadT(); if(T<0) { LCD_ShowChar(1,10,'-'); T=-T; } else { LCD_ShowChar(1,10,'+'); } LCD_ShowNum(1,11,(unsigned int)T,2); LCD_ShowChar(1,13,'.'); LCD_ShowNum(1,14,(unsigned int)(T*100)%100,2); }
void StopwatchControl() { if (KeyNum == 6) { StopwatchState = !StopwatchState; } if (KeyNum == 7) { StopwatchTime = 0; StopwatchState = 0; } ShowStopwatch(); }
void ShowStopwatch() { unsigned int seconds = StopwatchTime / 1000; unsigned int milliseconds = StopwatchTime % 1000;
LCD_ShowNum(2, 1, seconds / 60, 2); LCD_ShowChar(2, 3, ':'); LCD_ShowNum(2, 4, seconds % 60, 2); LCD_ShowChar(2, 6, '.'); LCD_ShowNum(2, 7, milliseconds / 10, 2); } void TimeShow(void) { DS1302_ReadTime(); LCD_ShowNum(1,1,DS1302_Time[0],2); LCD_ShowNum(1,4,DS1302_Time[1],2); LCD_ShowNum(1,7,DS1302_Time[2],2); LCD_ShowNum(2,1,DS1302_Time[3],2); LCD_ShowNum(2,4,DS1302_Time[4],2); LCD_ShowNum(2,7,DS1302_Time[5],2); LCD_ShowString(2, 10, "W:"); LCD_ShowNum(2, 12, DS1302_Time[6], 1); }
void TimeSet(char* TimeArray,unsigned char* SelectPtr) { if(KeyNum==2) { (*SelectPtr)++; *SelectPtr%=7; } if(KeyNum==3) { TimeArray[*SelectPtr]++; if(TimeArray[0]>99){TimeArray[0]=0;} if(TimeArray[1]>12){TimeArray[1]=1;}
if( TimeArray[1]==1 || TimeArray[1]==3 || TimeArray[1]==5 || TimeArray[1]==7 || TimeArray[1]==8 || TimeArray[1]==10 || TimeArray[1]==12) { if(TimeArray[2]>31){TimeArray[2]=1;} } else if(TimeArray[1]==4 || TimeArray[1]==6 || TimeArray[1]==9 || TimeArray[1]==11) { if(TimeArray[2]>30){TimeArray[2]=1;} } else if(TimeArray[1]==2) { if(TimeArray[0]%4==0) { if(TimeArray[2]>29){TimeArray[2]=1;} } else { if(TimeArray[2]>28){TimeArray[2]=1;} } } TimeArray[6] = CalculateWeekday(TimeArray[0], TimeArray[1], TimeArray[2]); if(TimeArray[3]>23){TimeArray[3]=0;} if(TimeArray[4]>59){TimeArray[4]=0;} if(TimeArray[5]>59){TimeArray[5]=0;} if(TimeArray[6]>6){TimeArray[6]=0;}
} if(KeyNum==4) { TimeArray[*SelectPtr]--; if(TimeArray[0]<0){TimeArray[0]=99;} if(TimeArray[1]<1){TimeArray[1]=12;} if( TimeArray[1]==1 || TimeArray[1]==3 || TimeArray[1]==5 || TimeArray[1]==7 || TimeArray[1]==8 || TimeArray[1]==10 || TimeArray[1]==12) { if(TimeArray[2]<1){TimeArray[2]=31;} if(TimeArray[2]>31){TimeArray[2]=1;} } else if(TimeArray[1]==4 || TimeArray[1]==6 || TimeArray[1]==9 || TimeArray[1]==11) { if(TimeArray[2]<1){TimeArray[2]=30;} if(TimeArray[2]>30){TimeArray[2]=1;} } else if(TimeArray[1]==2) { if(TimeArray[0]%4==0) { if(TimeArray[2]<1){TimeArray[2]=29;} if(TimeArray[2]>29){TimeArray[2]=1;} } else { if(TimeArray[2]<1){TimeArray[2]=28;} if(TimeArray[2]>28){TimeArray[2]=1;} } } if(TimeArray[3]<0){TimeArray[3]=23;} if(TimeArray[4]<0){TimeArray[4]=59;} if(TimeArray[5]<0){TimeArray[5]=59;} if(TimeArray[6]<0){TimeArray[6]=6;} TimeArray[6] = CalculateWeekday(TimeArray[0], TimeArray[1], TimeArray[2]); } if(*SelectPtr==0 && TimeSetFlashFlag==1){LCD_ShowString(1,1," ");} else {LCD_ShowNum(1,1,TimeArray[0],2);} if(*SelectPtr==1 && TimeSetFlashFlag==1){LCD_ShowString(1,4," ");} else {LCD_ShowNum(1,4,TimeArray[1],2);} if(*SelectPtr==2 && TimeSetFlashFlag==1){LCD_ShowString(1,7," ");} else {LCD_ShowNum(1,7,TimeArray[2],2);} if(*SelectPtr==3 && TimeSetFlashFlag==1){LCD_ShowString(2,1," ");} else {LCD_ShowNum(2,1,TimeArray[3],2);} if(*SelectPtr==4 && TimeSetFlashFlag==1){LCD_ShowString(2,4," ");} else {LCD_ShowNum(2,4,TimeArray[4],2);} if(*SelectPtr==5 && TimeSetFlashFlag==1){LCD_ShowString(2,7," ");} else {LCD_ShowNum(2,7,TimeArray[5],2);} if(*SelectPtr==6 && TimeSetFlashFlag==1){LCD_ShowString(2,12," ");} else {LCD_ShowNum(2,12,TimeArray[6],1);} }
void Timer0_Routine() interrupt 1 { static unsigned int T0Count; static unsigned char station_toggle; TL0 = 0x18; TH0 = 0xFC; T0Count++; if(T0Count>=100) { T0Count=0; station_toggle++; if (station_toggle>=4) { station_toggle = 0; TimeSetFlashFlag=!TimeSetFlashFlag; } if (StopwatchState) { StopwatchTime += 100; } } }
void LCD_WriteCommand(unsigned char Command) { LCD_RS=0; LCD_RW=0; LCD_DataPort=Command; LCD_EN=1; Delay(1); LCD_EN=0; Delay(1); }
void LCD_WriteData(unsigned char Data) { LCD_RS=1; LCD_RW=0; LCD_DataPort=Data; LCD_EN=1; Delay(1); LCD_EN=0; Delay(1); }
void LCD_SetCursor(unsigned char Line,unsigned char Column) { if(Line==1) { LCD_WriteCommand(0x80|(Column-1)); } else if(Line==2) { LCD_WriteCommand(0x80|(Column-1+0x40)); } }
void LCD_Init() { LCD_WriteCommand(0x38); LCD_WriteCommand(0x0c); LCD_WriteCommand(0x06); LCD_WriteCommand(0x01); }
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char) { LCD_SetCursor(Line,Column); LCD_WriteData(Char); }
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String) { unsigned char i; LCD_SetCursor(Line,Column); for(i=0;String[i]!='\0';i++) { LCD_WriteData(String[i]); } }
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) { unsigned char i; LCD_SetCursor(Line,Column); for(i=Length;i>0;i--) { LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0'); } }
int LCD_Pow(int X,int Y) { unsigned char i; int Result=1; for(i=0;i<Y;i++) { Result*=X; } return Result; }
unsigned char Key() { unsigned char KeyNumber=0; if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=1;} if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=2;} if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;} if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;} if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=5;} if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=6;} if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=7;} if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=8;} return KeyNumber; }
void Delay(unsigned int xms) { unsigned char i, j; while(xms--) { i = 2; j = 239; do { while (--j); } while (--i); } }
void Timer0Init(void) { TMOD &= 0xF0; TMOD |= 0x01; TL0 = 0x18; TH0 = 0xFC; TF0 = 0; TR0 = 1; ET0=1; EA=1; PT0=0; }
void DS1302_Init(void) { DS1302_CE=0; DS1302_SCLK=0; }
void DS1302_WriteByte(unsigned char Command,Data) { unsigned char i; DS1302_CE=1; for(i=0;i<8;i++) { DS1302_IO=Command&(0x01<<i); DS1302_SCLK=1; DS1302_SCLK=0; } for(i=0;i<8;i++) { DS1302_IO=Data&(0x01<<i); DS1302_SCLK=1; DS1302_SCLK=0; } DS1302_CE=0; }
unsigned char DS1302_ReadByte(unsigned char Command) { unsigned char i,Data=0x00; Command|=0x01; DS1302_CE=1; for(i=0;i<8;i++) { DS1302_IO=Command&(0x01<<i); DS1302_SCLK=0; DS1302_SCLK=1; } for(i=0;i<8;i++) { DS1302_SCLK=1; DS1302_SCLK=0; if(DS1302_IO){Data|=(0x01<<i);} } DS1302_CE=0; DS1302_IO=0; return Data; }
void DS1302_SetTime(void) { DS1302_WriteByte(DS1302_WP,0x00); DS1302_WriteByte(DS1302_YEAR,DS1302_Time[0]/10*16+DS1302_Time[0]%10); DS1302_WriteByte(DS1302_MONTH,DS1302_Time[1]/10*16+DS1302_Time[1]%10); DS1302_WriteByte(DS1302_DATE,DS1302_Time[2]/10*16+DS1302_Time[2]%10); DS1302_WriteByte(DS1302_HOUR,DS1302_Time[3]/10*16+DS1302_Time[3]%10); DS1302_WriteByte(DS1302_MINUTE,DS1302_Time[4]/10*16+DS1302_Time[4]%10); DS1302_WriteByte(DS1302_SECOND,DS1302_Time[5]/10*16+DS1302_Time[5]%10); DS1302_WriteByte(DS1302_DAY,DS1302_Time[6]/10*16+DS1302_Time[6]%10); DS1302_WriteByte(DS1302_WP,0x80); }
void DS1302_ReadTime(void) { unsigned char Temp; Temp=DS1302_ReadByte(DS1302_YEAR); DS1302_Time[0]=Temp/16*10+Temp%16; Temp=DS1302_ReadByte(DS1302_MONTH); DS1302_Time[1]=Temp/16*10+Temp%16; Temp=DS1302_ReadByte(DS1302_DATE); DS1302_Time[2]=Temp/16*10+Temp%16; Temp=DS1302_ReadByte(DS1302_HOUR); DS1302_Time[3]=Temp/16*10+Temp%16; Temp=DS1302_ReadByte(DS1302_MINUTE); DS1302_Time[4]=Temp/16*10+Temp%16; Temp=DS1302_ReadByte(DS1302_SECOND); DS1302_Time[5]=Temp/16*10+Temp%16; Temp=DS1302_ReadByte(DS1302_DAY); DS1302_Time[6]=Temp/16*10+Temp%16; }
void CheckAlarm() { if (DS1302_Time[0] == AlarmTime[0] && DS1302_Time[1] == AlarmTime[1] && DS1302_Time[2] == AlarmTime[2] && DS1302_Time[3] == AlarmTime[3] && DS1302_Time[4] == AlarmTime[4] && DS1302_Time[5] == AlarmTime[5] && DS1302_Time[6] == AlarmTime[6]) { AlarmFlag = 1; BuzzerState = 1; }else { AlarmFlag = 0; } }
void DS18B20_Init(void){ DS18B20_ConvertT(); Delay(1000); }
void DS18B20_ConvertT(void) { OneWire_Init(); OneWire_SendByte(DS18B20_SKIP_ROM); OneWire_SendByte(DS18B20_CONVERT_T); }
float DS18B20_ReadT(void) { unsigned char TLSB,TMSB; int Temp; float T; OneWire_Init(); OneWire_SendByte(DS18B20_SKIP_ROM); OneWire_SendByte(DS18B20_READ_SCRATCHPAD); TLSB=OneWire_ReceiveByte(); TMSB=OneWire_ReceiveByte(); Temp=(TMSB<<8)|TLSB; T=Temp/16.0; return T; }
unsigned char OneWire_Init(void) { unsigned char i; unsigned char AckBit; OneWire_DQ=1; OneWire_DQ=0; i = 247;while (--i); OneWire_DQ=1; i = 32;while (--i); AckBit=OneWire_DQ; i = 247;while (--i); return AckBit; }
void OneWire_SendBit(unsigned char Bit) { unsigned char i; OneWire_DQ=0; i = 4;while (--i); OneWire_DQ=Bit; i = 24;while (--i); OneWire_DQ=1; }
unsigned char OneWire_ReceiveBit(void) { unsigned char i; unsigned char Bit; OneWire_DQ=0; i = 2;while (--i); OneWire_DQ=1; i = 2;while (--i); Bit=OneWire_DQ; i = 24;while (--i); return Bit; }
void OneWire_SendByte(unsigned char Byte) { unsigned char i; for(i=0;i<8;i++) { OneWire_SendBit(Byte&(0x01<<i)); } }
unsigned char OneWire_ReceiveByte(void) { unsigned char i; unsigned char Byte=0x00; for(i=0;i<8;i++) { if(OneWire_ReceiveBit()){Byte|=(0x01<<i);} } return Byte; }
unsigned char CalculateWeekday(int year, int month, int day) { unsigned char weekday; if (month < 3) { month += 12; year--; } weekday = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7; return (weekday + 1); }
|