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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
| #include <intrins.h> #include <STC15F104E.H> #include "iic.h" #include "onewire.h" #include "ds1302.h"
#define attach(x, y, z) P25=(x); P26=(y); P27=(z); #define detach() P27=0; P25=0; P26=0; #define write(x, y, z, dat) P0 = 0xFF; attach(x, y, z); P0 = (dat); detach();
typedef unsigned char int8_t; typedef unsigned char uint8_t; typedef unsigned int uint16_t; typedef unsigned long uint32_t;
uint8_t timer_key;
uint8_t led_statue = 0xFF;
#define led_on(index) led_statue &= 0xFF ^ (1 << (index) ); write(0, 0, 1, led_statue); #define led_off(index) led_statue |= 1 << (index); write(0, 0, 1, led_statue); #define led_invert(index) led_statue ^= 1 << (index); write(0, 0, 1, led_statue);
#define NT_BLANK 16 #define NT_INTERVAL 17 #define NT_H 18 #define NT_P 19 #define NT_U 20 #define NT_L 21 #define NT_N 22
code uint8_t nt_code[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e, 0xFF, 0xBF, 0x89, 0x8C, 0xC1, 0xC7, 0xC8}; uint8_t nt_buffer[8] = {NT_BLANK, NT_BLANK, NT_BLANK, NT_BLANK, NT_BLANK, NT_BLANK, NT_BLANK, NT_BLANK}; uint8_t nt_index;
#define nt_show(pos, dat) write(0, 1, 1, (1 << (pos))); write(1, 1, 1, (nt_code[(dat)])); #define nt_showdot(pos, dat) write(0, 1, 1, (1 << (pos))); write(1, 1, 1, (nt_code[(dat)] & 0x7F));
uint8_t hour, minute, second;
uint8_t temper_max; float temper_ave; uint8_t temper; uint8_t temper_para; uint8_t temper_last;
uint8_t humid_max; float humid_ave; uint8_t humid; uint8_t humid_last;
uint8_t trigger_counter; uint8_t trigger_hour; uint8_t trigger_minute; bit trigger_invalid; bit trigger_ready;
uint16_t timer_trigger; uint16_t timer_s9; uint16_t timer_freq; uint8_t timer_l4;
uint16_t freq_counter; uint16_t freq;
void delay_ms(uint8_t ms) {
while (ms--) { unsigned char data i, j;
_nop_(); _nop_(); i = 12; j = 168; do { while (--j); } while (--i); } }
void nt_shownum_len(uint8_t pos, uint32_t dat, uint8_t len) { do { nt_buffer[pos] = dat % 10; dat /= 10; pos--; } while (--len); }
void nt_shownum_dot(uint8_t pos, uint32_t dat, uint8_t len, uint8_t dot_pos) { do { if (pos == dot_pos) { nt_buffer[pos] = (dat % 10) | 0x80; } else { nt_buffer[pos] = dat % 10; } dat /= 10; pos--; } while (--len); }
void nt_shownum_blank(uint8_t pos, uint32_t dat, uint8_t len) { do { nt_buffer[pos] = dat % 10; dat /= 10; pos--; } while (--len && dat); while (len--) { nt_buffer[pos] = NT_BLANK; pos--; } }
void pcf_write(uint8_t ctrl, uint8_t dat) { IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(ctrl); IIC_WaitAck(); IIC_SendByte(dat); IIC_WaitAck(); IIC_Stop(); }
float pcf_read(uint8_t ctrl) { uint8_t res; IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(ctrl); IIC_WaitAck(); IIC_Stop(); IIC_Start(); IIC_SendByte(0x91); IIC_WaitAck(); res = IIC_RecByte(); IIC_SendAck(0); IIC_Stop(); return res * 5.0f / 255.0f; }
#define ds1302_r_reg_hour 0x85 #define ds1302_w_reg_hour 0x84 #define ds1302_r_reg_minute 0x83 #define ds1302_w_reg_minute 0x82 #define ds1302_r_reg_second 0x81 #define ds1302_w_reg_second 0x80
float temper_read() { uint16_t res = 0; uint8_t high, low; init_ds18b20(); Write_DS18B20(0xCC); Write_DS18B20(0x44); Delay_OneWire(200); init_ds18b20(); Write_DS18B20(0xCC); Write_DS18B20(0xBE); Delay_OneWire(200); low = Read_DS18B20(); high = Read_DS18B20(); res = high & 0x0F; res = (res << 8) | low; return res * 0.0625f; }
void e2prom_write(uint8_t addr, uint8_t* dat, uint8_t len) { IIC_Start(); IIC_SendByte(0xA0); IIC_WaitAck(); IIC_SendByte(addr); IIC_WaitAck(); while (len--) { IIC_SendByte(dat[len]); IIC_WaitAck(); } IIC_Stop(); }
void e2prom_read(uint8_t addr, uint8_t* dat, uint8_t len) { uint8_t i; IIC_Start(); IIC_SendByte(0xA0); IIC_WaitAck(); IIC_SendByte(addr); IIC_WaitAck(); IIC_Start(); IIC_SendByte(0xA1); IIC_WaitAck(); for (i = 0; i < len; ++i) { dat[i] = IIC_RecByte(); IIC_SendAck(0); } }
sbit TX = P1^0; sbit RX = P1^1;
void sonic_send_signal() { uint8_t i; for (i = 0; i < 8; ++i) { TX = 1; _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); TX = 0; _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); } }
uint8_t sonic_get_dist() { uint16_t tim; TF0 = 0; TH0 = 0x00; TL0 = 0x00; sonic_send_signal(); TR0 = 1; while ( (RX == 1) && (TF1 == 0) ); TR0 = 0; if (TF1 == 0) { tim = TH0; tim = (tim << 8) | TL0; return (uint8_t)(tim * 0.017); } TF0 = 0; return 0; }
uint8_t keys_scan() { uint8_t temp = 0x00; P3 = 0x30; P42 = 1; P44 = 1; if (P3 != 0x0C || P42 != 0 || P44 != 0) { temp = P3 | (uint8_t)P42 << 6 | (uint8_t)P44 << 7; P3 = 0x0F; P42 = 0; P44 = 0; temp |= P3; switch (temp) { case 0x7E: return 1; case 0xBE: return 2; case 0xDE: return 3; case 0xEE: return 4; case 0x7D: return 5; case 0xBD: return 6; case 0xDD: return 7; case 0xED: return 8; case 0x7B: return 9; case 0xBB: return 10; case 0xDB: return 11; case 0xEB: return 12; case 0x77: return 13; case 0xB7: return 14; case 0xD7: return 15; case 0xE7: return 16; default: return 0; } } return 0; }
void timer0_Init(void) { AUXR |= 0x80; TMOD &= 0xF0; TMOD |= 0x04; TL0 = 0xFF; TH0 = 0xFF; TF0 = 0; TR0 = 1; ET0 = 1; }
void timer1_Init(void) { AUXR |= 0x40; TMOD &= 0x0F; TL1 = 0x20; TH1 = 0xD1; TF1 = 0; TR1 = 1; ET1 = 1; }
uint8_t interface, interface_echo;
void draw_time() { nt_shownum_len(1, hour, 2); nt_buffer[2] = NT_INTERVAL; nt_shownum_len(4, minute, 2); nt_buffer[5] = NT_INTERVAL; nt_shownum_len(7, second, 2); }
void draw_echo_temper() { nt_buffer[0] = 0x0C; nt_buffer[1] = NT_BLANK; nt_shownum_len(3, temper_max, 2); nt_buffer[4] = NT_INTERVAL; nt_shownum_dot(7, temper_ave * 10, 3, 6); }
void draw_echo_humid() { nt_buffer[0] = NT_H; nt_buffer[1] = NT_BLANK; nt_shownum_len(3, humid_max, 2); nt_buffer[4] = NT_INTERVAL; nt_shownum_dot(7, humid_ave * 10, 3, 6); }
void draw_echo_time() { nt_buffer[0] = 0x0F; nt_shownum_len(2, trigger_counter, 2); nt_shownum_len(4, trigger_hour, 2); nt_buffer[5] = NT_INTERVAL; nt_shownum_len(7, trigger_minute, 2); }
void draw_para() { nt_buffer[0] = NT_P; nt_shownum_blank(7, temper_para, 7); }
void draw_temper_humid() { nt_buffer[0] = 0x0E; nt_buffer[1] = NT_BLANK; nt_buffer[2] = NT_BLANK; nt_shownum_len(4, temper, 2); nt_buffer[5] = NT_INTERVAL; if (trigger_invalid) { nt_buffer[6] = 0x0A; nt_buffer[7] = 0x0A; } else { nt_shownum_len(7, humid, 2); } }
void draw() { if (timer_trigger > 0) { draw_temper_humid(); } else { switch (interface) { case 0: draw_time(); break; case 1: switch (interface_echo) { case 0: draw_echo_temper(); break; case 1: draw_echo_humid(); break; case 2: draw_echo_time(); break; } break; case 2: draw_para(); break; } } if (interface == 0) { led_on(0); } else { led_off(0); } if (interface == 1) { led_on(1); } else { led_off(1); } if (timer_trigger > 0) { led_on(2); } else { led_off(2); } if (timer_l4 > 0) { led_invert(3); timer_l4 = 1; } if (trigger_counter >= 2) { if (humid > humid_last && temper > temper_last) { led_on(5); } else { led_off(5); } } else { led_off(5); } }
void key_event(uint8_t key) { static bit press_flag = 0; if (timer_key < 50) { return; } timer_key = 0; switch (key) { case 13: if (press_flag) return; press_flag = 1;
if (timer_trigger == 0) { ++interface; interface %= 3; } if (interface == 1) { interface_echo = 0; } break; case 9: if (press_flag) return; press_flag = 1; if (timer_trigger == 0 && interface == 1) { ++interface_echo; interface_echo %= 3; } break; case 14: if (press_flag) return; press_flag = 1; if (timer_trigger == 0 && interface == 2) { temper_para++; } break; case 10: if (press_flag) return; press_flag = 1; if (timer_trigger == 0 && interface == 2) { temper_para--; } if (timer_trigger == 0 && interface == 1 && timer_s9 == 0) { timer_s9++; } break; default: press_flag = 0; timer_s9 = 0; break; } }
void update() { float volt; if (timer_s9 >= 2000) { timer_s9 = 0; temper_max = 0; temper_ave = 0; humid_max = 0; humid_ave = 0; trigger_hour = 0; trigger_minute = 0; trigger_counter = 0; trigger_ready = 1; trigger_invalid = 0; } volt = pcf_read(0x41); hour = Read_Ds1302_Byte(ds1302_r_reg_hour); hour = (hour & 0x0F) + ((hour & 0x30) >> 4) * 10; minute = Read_Ds1302_Byte(ds1302_r_reg_minute); minute = (minute & 0x0F) + ((minute & 0x70) >> 4) * 10; second = Read_Ds1302_Byte(ds1302_r_reg_second); second = (second & 0x0F) + ((second & 0x70) >> 4) * 10; if (volt <= 3 && trigger_ready) { trigger_ready = 0; if (freq < 200 || freq > 2000) { trigger_invalid = 1; } else { humid_last = humid; humid = ((float)freq) * 0.0444 + 1.11; trigger_invalid = 0; } if (timer_trigger == 0) { timer_trigger++; } if (!trigger_invalid) { temper_last = temper; temper = temper_read(); temper_max = temper > temper_max ? temper : temper_max; temper_ave = (temper_ave * trigger_counter + temper) / (trigger_counter + 1); humid_max = humid > humid_max ? humid : humid_max; humid_ave = (humid_ave * trigger_counter + humid) / (trigger_counter + 1); if (temper > temper_para && timer_l4 == 0) { timer_l4++; } else if (temper <= temper_para) { timer_l4 = 0; } trigger_hour = hour; trigger_minute = minute; trigger_counter++; } } else if (timer_trigger >= 3000) { timer_trigger = 0; } else if (volt > 4){ trigger_ready = 1; } }
void init(void) { write(0, 0, 1, 0xFF); write(1, 0, 1, 0x00); write(0, 1, 1, 0x00); write(1, 1, 1, 0xFF);
init_ds18b20(); pcf_write(0x41, 0x00); timer0_Init(); timer1_Init(); Write_Ds1302_Byte(0x8E, 0x00); Write_Ds1302_Byte(ds1302_w_reg_second, 0x55); Write_Ds1302_Byte(ds1302_w_reg_minute, 0x59); Write_Ds1302_Byte(ds1302_w_reg_hour, 0x23); Write_Ds1302_Byte(0x8E, 0x80); temper_para = 30; EA = 1; }
void main(void) { init(); while(1) { draw(); update(); key_event(keys_scan()); } }
void timer0_int(void) interrupt 1 { freq_counter++; }
void timer1_int(void) interrupt 3 { nt_index %= 8; if (nt_buffer[nt_index] & 0x80) { nt_showdot(nt_index, nt_buffer[nt_index] & 0x7F); } else { nt_show(nt_index, nt_buffer[nt_index]); } nt_index++; timer_key++; timer_freq++; if (timer_freq >= 1000) { freq = freq_counter; freq_counter = 0; timer_freq = 0; } if (timer_s9 > 0) { timer_s9++; } if (timer_trigger > 0) { timer_trigger++; } }
|