找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2689|回复: 0
打印 上一主题 下一主题
收起左侧

可调单片机LCD1602多功能电子钟Proteus仿真程序

[复制链接]
跳转到指定楼层
楼主
硬件设计上我们采用了STC89C52单片机作为主机,并且使用5V电源供电。时间信号由高性能、低功耗的DS1302串行时钟存储器提供,在按键的作用下可以进行调时,调分,调秒,我们的作品基本满足准确计时的要求。

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. #include<reg52.h>
  2. #include<intrins.h>
  3. unsigned char code displaywelcome[]={" Welcome To My Lcd Timer"};//欢迎界面
  4. unsigned char code displaywish[]={" Happy Every Day ^_^"}; //欢迎界面
  5. unsigned char code overtemperature[]={"OVERTEMPERATURE!"};
  6. unsigned char code digit[]={"0123456789"}; //数字代码
  7. unsigned char mode,TH,TL,TN,TD,length,tempswitch,Maxtemp=40,amode,alarmmode,minutes,hours,
  8. minutea,seconds,houra=12;
  9. sbit SCLK=P1^0; //DS1302 时钟输入
  10. sbit DATE=P1^1; //DS1302 数据输入
  11. sbit REST=P1^2; //DS1302 复位端口
  12. sbit SET=P1^4; //DS1302 设置模式选择位
  13. sbit ADD=P1^5; //增加
  14. sbit RED=P1^6; //减小
  15. sbit CANL=P1^7;
  16. void delay1ms(int i)//1 毫秒延时
  17. {
  18. int j,k;
  19. while(i--)
  20. for(j=76;j>1;j--);
  21. for(k=29;k>1;k--);
  22. }
  23. void delaynus(unsigned char n) //延时若干微秒
  24. {
  25. unsigned char i;
  26. for(i=0;i<n;i++);
  27. }
  28. /***********蜂鸣器模块************/
  29. sbit beep=P3^6; //位定义,定义P.6 位fmp
  30. void dely500(void)
  31. {
  32. unsigned char i;
  33. for(i=250;i>0;i--)
  34. {
  35. _nop_();
  36. }
  37. }
  38. void baojing(unsigned char n)
  39. {
  40. unsigned char x,i;
  41. while(n--)
  42. {
  43. for(i=0;i<5;i++)
  44. {
  45. for(x=0;x<200;x++)
  46. {
  47. beep=~beep;
  48. dely500();
  49. }
  50. }
  51. for(i=0;i<3;i++)
  52. {
  53. for(x=0;x<200;x++)
  54. {
  55. beep=~beep;
  56. dely500();
  57. dely500();
  58. }
  59. }
  60. }
  61. }
  62. /*******DS1302 模块*************/
  63. void Write1302(unsigned char date)//向1302 写数据
  64. {
  65. unsigned char i;
  66. SCLK=0;
  67. delaynus(2);
  68. for(i=0;i<8;i++)
  69. {
  70. DATE=date&0x01;
  71. SCLK=1;
  72. delaynus(2);
  73. SCLK=0;
  74. delaynus(2);
  75. date>>=1;
  76. }
  77. }
  78. void WriteSet1302(unsigned char cmd,unsigned char date) //根据相应的命令输入相应的数据
  79. {
  80. REST=0;
  81. SCLK=0;
  82. REST=1;
  83. Write1302(cmd);
  84. delaynus(5);
  85. Write1302(date);
  86. SCLK=1;
  87. REST=0;
  88. }
  89. unsigned char Read1302(void) //读取1302 数据
  90. {
  91. unsigned char i,date;
  92. delaynus(2);
  93. for(i=0;i<8;i++)
  94. {
  95. date>>=1;
  96. if(DATE==1)
  97. date|=0x80;
  98. SCLK=1;
  99. delaynus(2);
  100. SCLK=0;
  101. delaynus(2);
  102. }
  103. return date;
  104. }
  105. unsigned char ReadSet1302(unsigned char cmd)//根据命令读取1302 相应的值
  106. {
  107. unsigned char date;
  108. REST=0;
  109. SCLK=0;
  110. REST=1;
  111. Write1302(cmd);
  112. delaynus(2);
  113. date=Read1302();
  114. SCLK=1;
  115. REST=0;
  116. return date;
  117. }
  118. void IntDS1302(void) //DS1302 初始化
  119. {
  120. unsigned char flag;
  121. flag= ReadSet1302(0x81);
  122. if(flag&0x80) { //判断时钟芯片是否关闭
  123. WriteSet1302(0x8E,0x00); //根据写状态寄存器命令字,写入不保护指令
  124. WriteSet1302(0x80,((0/10)<<4|(0%10))); //根据写秒寄存器命令字,写入秒的初始值
  125. WriteSet1302(0x82,((0/10)<<4|(0%10))); //根据写分寄存器命令字,写入分的初始值
  126. WriteSet1302(0x84,((0/10)<<4|(0%10))); //根据写小时寄存器命令字,写入小时的初始值
  127. WriteSet1302(0x86,((0/10)<<4|(0%10))); //根据写日寄存器命令字,写入日的初始值
  128. WriteSet1302(0x88,((0/10)<<4|(0%10))); //根据写月寄存器命令字,写入月的初始值
  129. WriteSet1302(0x8c,((10/10)<<4|(10%10))); //根据写年寄存器命令字,写入年的初始值
  130. WriteSet1302(0x90,0xa5); //打开充电功能选择2K 电阻充电方式
  131. WriteSet1302(0x8E,0x80); //根据写状态寄存器命令字,写入保护指令
  132. }
  133. }
  134. /***************液晶显示模块*****************/
  135. sbit RS=P2^5;
  136. sbit RW=P2^6;
  137. sbit E=P2^7;
  138. sbit BF=P0^7;
  139. /*液晶忙检测*/
  140. bit BusyTest(void)
  141. {
  142. bit result;
  143. RS=0;
  144. RW=1;
  145. E=1;
  146. _nop_();
  147. _nop_();
  148. _nop_();
  149. result=BF;
  150. _nop_();
  151. _nop_();
  152. _nop_();
  153. E=0;
  154. return result;
  155. }
  156. /*写指令*/
  157. void Write_com(unsigned char command)
  158. {
  159. while(BusyTest()!=0);
  160. RS=0;
  161. RW=0;
  162. E=0;
  163. _nop_();
  164. _nop_();
  165. _nop_();
  166. P0=command;
  167. _nop_();
  168. _nop_();
  169. _nop_();
  170. E=1;
  171. _nop_();
  172. _nop_();
  173. _nop_();
  174. E=0;
  175. }
  176. /*写地址*/
  177. void Write_Address(unsigned char address)
  178. {
  179. Write_com(address|0x80);
  180. delay1ms(1);
  181. }
  182. /*写数据*/
  183. void Write_Date(unsigned char date)
  184. {
  185. RS=1;
  186. RW=0;
  187. E=0;
  188. _nop_();
  189. _nop_();
  190. _nop_();
  191. P0=date;
  192. _nop_();
  193. _nop_();
  194. _nop_();
  195. E=1;
  196. _nop_();
  197. _nop_();
  198. _nop_();
  199. E=0;
  200. delay1ms(1);
  201. }
  202. /*初始化*/
  203. void Lcd_Int(void)
  204. {
  205. Write_com(0x38);
  206. delay1ms(1);
  207. Write_com(0x38);
  208. delay1ms(1);
  209. Write_com(0x06);
  210. delay1ms(1);
  211. Write_com(0x0c);
  212. delay1ms(1);
  213. Write_com(0x01);
  214. }
  215. void displaymainpart(void)//显示液晶主要部分(不变化部分)
  216. {
  217. Write_Address(0x01);
  218. delay1ms(1);
  219. Write_Date('D');
  220. Write_Date('A');
  221. Write_Date('T');
  222. Write_Date('E');
  223. Write_Date(':');
  224. delay1ms(1);
  225. Write_Address(0x0A);
  226. delay1ms(1);
  227. Write_Date('-');
  228. Write_Address(0x0D);
  229. Write_Date('-');
  230. Write_Address(0x42);
  231. Write_Date('T');
  232. Write_Date('I');
  233. Write_Date('M');
  234. Write_Date('E');
  235. Write_Date(':');
  236. Write_Address(0x4A);
  237. Write_Date('-');
  238. Write_Address(0x4D);
  239. Write_Date('-');
  240. Write_Address(0x06);
  241. Write_Date('2');
  242. Write_Date('0');
  243. }
  244. void display_Second(unsigned char second) //在液晶上显示秒
  245. {
  246. unsigned char i,j;
  247. i=second/10;
  248. j=second%10;
  249. Write_Address(0x4E);
  250. Write_Date(digit[i]);
  251. Write_Date(digit[j]);
  252. delay1ms(1);
  253. }
  254. void display_Minute(unsigned char minute) //在液晶上显示分
  255. {
  256. unsigned char i,j;
  257. i=minute/10;
  258. j=minute%10;
  259. Write_Address(0x4B);
  260. Write_Date(digit[i]);
  261. Write_Date(digit[j]);
  262. delay1ms(1);
  263. }
  264. void display_Hour(unsigned char hour) //在液晶上显示时
  265. {
  266. unsigned char i,j;
  267. i=hour/10;
  268. j=hour%10;
  269. Write_Address(0x48);
  270. Write_Date(digit[i]);
  271. Write_Date(digit[j]);
  272. delay1ms(1);
  273. }
  274. void display_Day(unsigned char day) //在液晶上显示日
  275. {
  276. unsigned char i,j;
  277. i=day/10;
  278. j=day%10;
  279. Write_Address(0x0E);
  280. Write_Date(digit[i]);
  281. Write_Date(digit[j]);
  282. delay1ms(1);
  283. }
  284. void display_Month(unsigned char month) //在液晶上显示月
  285. {
  286. unsigned char i,j;
  287. i=month/10;
  288. j=month%10;
  289. Write_Address(0x0B);
  290. Write_Date(digit[i]);
  291. Write_Date(digit[j]);
  292. delay1ms(1);
  293. }
  294. void display_Year(unsigned char year) //在液晶上显示年
  295. {
  296. unsigned char i,j;
  297. i=year/10;
  298. j=year%10;
  299. Write_Address(0x08);
  300. Write_Date(digit[i]);
  301. Write_Date(digit[j]);
  302. delay1ms(1);
  303. }
  304. void display_houra(unsigned char x) //闹钟小时部分显示
  305. {
  306. unsigned char i,j;
  307. i=x/10;
  308. j=x%10;
  309. Write_Address(0x44);
  310. Write_Date(digit[i]);
  311. Write_Date(digit[j]);
  312. }
  313. void display_minutea(unsigned char x)//闹钟分钟部分显示
  314. {
  315. unsigned char i,j;
  316. i=x/10;
  317. j=x%10;
  318. Write_Address(0x47);
  319. Write_Date(digit[i]);
  320. Write_Date(digit[j]);
  321. }
  322. void display_Time(void) //显示实时时间
  323. {
  324. unsigned char value,day,month,year;
  325. Write_com(0x0c);
  326. value=ReadSet1302(0x81);
  327. seconds=(((value&0x70)>>4)*10+(value&0x0f));
  328. display_Second(seconds);
  329. value=ReadSet1302(0x83);
  330. minutes=(((value&0x70)>>4)*10+(value&0x0f));
  331. display_Minute(minutes);
  332. value=ReadSet1302(0x85);
  333. hours=(((value&0x70)>>4)*10+(value&0x0f));
  334. display_Hour(hours);
  335. value=ReadSet1302(0x87);
  336. day=(((value&0x70)>>4)*10+(value&0x0f));
  337. display_Day(day);
  338. value=ReadSet1302(0x89);
  339. month=(((value&0x70)>>4)*10+(value&0x0f));
  340. display_Month(month);
  341. value=ReadSet1302(0x8D);
  342. year=(((value&0xf0)>>4)*10+(value&0x0f));
  343. display_Year(year);
  344. }
  345. void displaystar(void) //显示欢迎界面
  346. {
  347. unsigned char i,j;
  348. Write_Address(0x0f);
  349. while(displaywelcome[i]!='\0')
  350. {
  351. Write_Date(displaywelcome[i]);
  352. i++;
  353. delay1ms(1);
  354. }
  355. i=0;
  356. Write_Address(0x4f);
  357. while(displaywish[i]!='\0')
  358. {
  359. Write_Date(displaywish[i]);
  360. i++;
  361. delay1ms(1);
  362. }
  363. j=40;
  364. while(j--)
  365. {
  366. Write_com(0x18);//循环左移
  367. delay1ms(700);
  368. }
  369. Write_com(0x01);
  370. delay1ms(10);
  371. }
  372. void gbdisplay(unsigned char address) //时间调整时光标闪烁
  373. {
  374. Write_Address(address);
  375. delay1ms(5);
  376. Write_com(0x0f);
  377. delay1ms(5);
  378. }
  379. void displaymaxt(unsigned char x) //显示最大温度
  380. {
  381. unsigned char i,j,k;
  382. Write_com(0x0c);
  383. delay1ms(2);
  384. Write_Address(0x44);
  385. i=x/100;
  386. j=x/10;
  387. k=x%10;
  388. Write_Date(digit[i]);
  389. Write_Date(digit[j]);
  390. Write_Date(digit[k]);
  391. }
  392. /*****************时间调整部分*********************/
  393. void hourset(void) //调时
  394. {
  395. unsigned char timevalue,hour;
  396. delay1ms(500); //防止多次触发
  397. WriteSet1302(0x8e,0x00);//将写保护去掉,确保能正常将调整后的数值写入DS1302
  398. timevalue=ReadSet1302(0x85); //读取此时的数值
  399. hour=(((timevalue&0x70)>>4)*10+(timevalue&0x0f));
  400. while(1)
  401. {
  402. if(ADD==0)
  403. {
  404. delay1ms(50);
  405. if(ADD==0)
  406. {
  407. hour++;
  408. delay1ms(300);
  409. while(ADD==0);
  410. }
  411. }
  412. if(RED==0)
  413. {
  414. delay1ms(50);
  415. if(RED==0)
  416. {
  417. hour--;
  418. delay1ms(300);
  419. if(hour==0) hour=23;
  420. while(RED==0);
  421. }
  422. }
  423. timevalue=(((hour)/10)<<4|(hour%10));
  424. WriteSet1302(0x84,timevalue);
  425. delay1ms(1);
  426. display_Hour(hour);
  427. Write_Address(0x49);
  428. delay1ms(5);
  429. if(hour>=24) hour=0;
  430. delay1ms(5);
  431. if(CANL==0)
  432. {
  433. mode=0;
  434. Write_com(0x0c);
  435. break;
  436. }
  437. if(SET==0) break;
  438. }
  439. WriteSet1302(0x8e,0x80);
  440. }
  441. void minuteset(void) //调分
  442. {
  443. unsigned char timevalue,minute;
  444. delay1ms(500);
  445. WriteSet1302(0x8e,0x00);
  446. timevalue=ReadSet1302(0x83);
  447. minute=(((timevalue&0x70)>>4)*10+(timevalue&0x0f));
  448. while(1)
  449. {
  450. if(ADD==0)
  451. {
  452. delay1ms(10);
  453. if(ADD==0)
  454. {
  455. minute++;
  456. if(minute>=60) timevalue=0;
  457. while(ADD==0);
  458. }
  459. }
  460. if(RED==0)
  461. {
  462. delay1ms(10);
  463. if(RED==0)
  464. {
  465. minute--;
  466. delay1ms(300);
  467. if(minute==0) minute=59;
  468. while(RED==0);
  469. }
  470. }
  471. timevalue=((minute/10)<<4|(minute%10));
  472. WriteSet1302(0x82,timevalue);
  473. delay1ms(1);
  474. display_Minute(minute);
  475. Write_Address(0x4c);
  476. delay1ms(5);
  477. if(minute>=60) minute=0;
  478. delay1ms(5);
  479. if(CANL==0)
  480. {
  481. mode=0;
  482. Write_com(0x0c);
  483. break;
  484. }
  485. if(SET==0) break;
  486. }
  487. WriteSet1302(0x8e,0x80);
  488. }
  489. void secondset(void) //秒归零
  490. {
  491. unsigned char second;
  492. delay1ms(500);
  493. WriteSet1302(0x8e,0x00);
  494. while(1)
  495. {
  496. if(ADD==0)
  497. {
  498. delay1ms(10);
  499. if(ADD==0)
  500. {
  501. second=0;
  502. WriteSet1302(0x80,0x00);
  503. while(ADD==0);
  504. }
  505. }
  506. delay1ms(1);
  507. display_Second(second);
  508. Write_Address(0x4f);
  509. delay1ms(5);
  510. if(CANL==0)
  511. {
  512. mode=0;
  513. Write_com(0x0c);
  514. break;
  515. }
  516. if(SET==0) break;
  517. }
  518. WriteSet1302(0x8e,0x80);
  519. }
  520. void yearset(void) //调年
  521. {
  522. unsigned char datevalue,year;
  523. delay1ms(500);
  524. WriteSet1302(0x8e,0x00);
  525. datevalue=ReadSet1302(0x8d);
  526. year=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));
  527. while(1)
  528. {
  529. if(ADD==0)
  530. {
  531. delay1ms(10);
  532. if(ADD==0)
  533. {
  534. year++;
  535. while(ADD==0);
  536. }
  537. }
  538. if(RED==0)
  539. {
  540. delay1ms(10);
  541. if(RED==0)
  542. {
  543. year--;
  544. delay1ms(300);
  545. while(RED==0);
  546. }
  547. }
  548. datevalue=((year/10)<<4|(year%10));
  549. WriteSet1302(0x8c,datevalue);
  550. delay1ms(5);
  551. display_Year(year);
  552. delay1ms(5);
  553. Write_Address(0x09);
  554. delay1ms(5);
  555. if(CANL==0)
  556. {
  557. mode=0;
  558. Write_com(0x0c);
  559. break;
  560. }
  561. if(SET==0) break;
  562. }
  563. WriteSet1302(0x8e,0x80);
  564. }
  565. void monthset(void) //调月
  566. {
  567. unsigned char datevalue,month;

  568. delay1ms(500);
  569. WriteSet1302(0x8e,0x00);
  570. datevalue=ReadSet1302(0x89);
  571. month=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));
  572. while(1)
  573. {
  574. if(ADD==0)
  575. {
  576. delay1ms(10);
  577. if(ADD==0)
  578. {
  579. month++;
  580. if(month>12) month=1;
  581. while(ADD==0);
  582. }
  583. }
  584. if(RED==0)
  585. {
  586. delay1ms(10);
  587. if(RED==0)
  588. {
  589. month--;
  590. delay1ms(300);
  591. if(month==0) month=12;
  592. while(RED==0);
  593. }
  594. }
  595. datevalue=((month/10)<<4|(month%10));
  596. WriteSet1302(0x88,datevalue);
  597. delay1ms(1);
  598. display_Month(month);
  599. Write_Address(0x0c);
  600. delay1ms(5);
  601. if(CANL==0)
  602. {
  603. mode=0;
  604. Write_com(0x0c);
  605. break;
  606. }
  607. if(SET==0) break;
  608. }
  609. WriteSet1302(0x8e,0x80);
  610. }
  611. void dayset(void) //调日
  612. {
  613. unsigned char datevalue,day;
  614. delay1ms(500);
  615. WriteSet1302(0x8e,0x00);
  616. datevalue=ReadSet1302(0x87);
  617. day=(((datevalue&0x70)>>4)*10+(datevalue&0x0f));
  618. while(1)
  619. {
  620. if(ADD==0)
  621. {
  622. delay1ms(10);
  623. if(ADD==0)
  624. {
  625. day++;
  626. if(day>31) day=1;
  627. while(ADD==0);
  628. }
  629. }
  630. if(RED==0)
  631. {
  632. delay1ms(10);
  633. if(RED==0)
  634. {
  635. day--;
  636. delay1ms(300);
  637. if(day==0) day=31;
  638. while(RED==0);
  639. }
  640. }
  641. datevalue=((day/10)<<4|(day%10));
  642. WriteSet1302(0x86,datevalue);
  643. delay1ms(1);
  644. display_Day(day);
  645. Write_Address(0x0f);
  646. delay1ms(5);
  647. if(CANL==0)
  648. {
  649. mode=0;
  650. Write_com(0x0c);
  651. break;
  652. }
  653. if(SET==0) break;
  654. }
  655. WriteSet1302(0x8e,0x80);
  656. }
  657. void alarmhourset(void) //闹钟小时调整
  658. {
  659. unsigned char value;
  660. value=houra;
  661. while(1)
  662. {
  663. if(ADD==0)
  664. {
  665. delay1ms(20);
  666. if(ADD==0)
  667. {
  668. Write_com(0x0c);
  669. delay1ms(5);
  670. value++;
  671. if(value>23) value=0;
  672. while(ADD==0);
  673. }
  674. }
  675. if(RED==0)
  676. {
  677. delay1ms(20);
  678. if(RED==0)
  679. {
  680. value--;
  681. if(value==0) value=23;
  682. while(RED==0);
  683. }
  684. }
  685. houra=value;
  686. display_houra(houra);
  687. gbdisplay(0x45);
  688. if(CANL==0)
  689. {
  690. delay1ms(500);
  691. break;
  692. }
  693. }
  694. }
  695. void alarmminuteset(void) //闹钟分钟调整
  696. {
  697. while(1)
  698. {
  699. if(ADD==0)
  700. {
  701. delay1ms(20);
  702. if(ADD==0)
  703. {
  704. minutea++;
  705. if(minutea>59) minutea=0;
  706. while(ADD==0) ;
  707. }
  708. }
  709. if(RED==0)
  710. {
  711. delay1ms(20);
  712. if(RED==0)
  713. {
  714. minutea--;
  715. if(minutea==0) minutea=59;
  716. while(RED==0) ;
  717. }
  718. }
  719. display_minutea(minutea);
  720. gbdisplay(0x48);
  721. if(CANL==0)
  722. {
  723. delay1ms(500);
  724. break;
  725. }
  726. }
  727. }
  728. void TimeSet(void) //时间调整函数
  729. {
  730. display_Time();
  731. if(SET==0)
  732. {
  733. delay1ms(10);
  734. if(SET==0)
  735. {
  736. mode++;
  737. delay1ms(20);
  738. switch(mode)
  739. {
  740. case(1):{gbdisplay(0x48);hourset(); delay1ms(500);} break;
  741. case(2):{gbdisplay(0x4c);minuteset();delay1ms(500);} break;
  742. case(3):{gbdisplay(0x4f);secondset();delay1ms(500);} break;
  743. case(4):{gbdisplay(0x09);yearset();delay1ms(500);} break;
  744. case(5):{gbdisplay(0x0c);monthset();delay1ms(500);} break;
  745. case(6):{gbdisplay(0x0f);dayset();delay1ms(500);} break;
  746. }
  747. if(mode==7) mode=0;
  748. }
  749. }
  750. }
  751. /***************显示温度模块********************/
  752. unsigned char code displayexplain[]={"Digit thermometer"};
  753. unsigned char code displayerror[]={"DS18B20 ERROR"};
  754. unsigned char code displayerror1[]={"PLEASE CHECK "};
  755. unsigned char code displaypart1[]={"WenDu:"};
  756. unsigned char code displaypart2[]={"Cent"};
  757. unsigned char flag;
  758. /**********************操作DS18B20 模块***************************/
  759. sbit DQ=P3^3;
  760. unsigned char time;
  761. /**********************DS18B20 初始化****************************/
  762. bit IntDS18B20(void)
  763. {
  764. bit temp;
  765. DQ=1;
  766. for(time=0;time<2;time++);
  767. DQ=0;
  768. for(time=0;time<200;time++);
  769. DQ=1;
  770. for(time=0;time<10;time++);
  771. temp=DQ;
  772. for(time=0;time<200;time++);
  773. return temp;
  774. }
  775. /**************************读DS18B20**********************/
  776. unsigned char ReadDS18B20(void)
  777. {
  778. unsigned char i;
  779. unsigned char dat;
  780. for(i=0;i<8;i++)
  781. {
  782. DQ=1;
  783. _nop_();
  784. DQ=0;
  785. _nop_();
  786. DQ=1;
  787. for(time=0;time<2;time++);
  788. dat>>=1;
  789. if(DQ==1)
  790. dat=dat|0x80;
  791. else
  792. dat=dat|0x00;
  793. for(time=0;time<10;time++);
  794. }
  795. return dat;
  796. }
  797. /*********************向DS18B20 写数据**************************/
  798. void WriteDS18B20(unsigned char date)
  799. {
  800. unsigned char i;
  801. for(i=0;i<8;i++)
  802. {
  803. DQ=1;
  804. _nop_();
  805. DQ=0;
  806. DQ=date&0x01;
  807. for(time=0;time<10;time++);
  808. DQ=1;
  809. for(time=0;time<1;time++);
  810. date>>=1;
  811. }
  812. for(time=0;time<4;time++);
  813. }
  814. /*******************为读取温度做好准备************************/
  815. void ReadyreadDS18B20(void)
  816. {
  817. IntDS18B20();
  818. WriteDS18B20(0XCC);
  819. WriteDS18B20(0X44);
  820. delay1ms(200);
  821. IntDS18B20();
  822. WriteDS18B20(0XCC);
  823. WriteDS18B20(0XBE);
  824. }
  825. /*************在1602 液晶上显示相关信息模块**********************/
  826. /****************当未检测到DS18B20 时显示信息模块****************/
  827. void Display_Error(void)
  828. {
  829. unsigned char i=0;
  830. Write_Address(0x00);
  831. while(displayerror[i]!='\0')
  832. {
  833. Write_Date(displayerror[i]);
  834. i++;
  835. delay1ms(100);
  836. }
  837. delay1ms(5);
  838. i=0;
  839. Write_Address(0x40);
  840. while(displayerror1[i]!='\0')
  841. {
  842. Write_Date(displayerror1[i]);
  843. i++;
  844. delay1ms(100);
  845. }
  846. delay1ms(3000) ;
  847. }
  848. /*************温度显示模块****************/
  849. void Display_Explain(void)
  850. {
  851. unsigned char i=0;
  852. Write_Address(0x00);
  853. delay1ms(5);
  854. while(displayexplain[i]!='\0')
  855. {
  856. Write_Date(displayexplain[i]);
  857. i++;
  858. delay1ms(5);
  859. }
  860. i=0;
  861. Write_Address(0x40);
  862. while(displaypart1[i]!='\0')
  863. {
  864. Write_Date(displaypart1[i]);
  865. i++;
  866. delay1ms(5);
  867. }
  868. i=0;
  869. Write_Address(0x4C);
  870. while(displaypart2[i]!='\0')
  871. {
  872. Write_Date(displaypart2[i]);
  873. i++;
  874. delay1ms(5);
  875. }
  876. Write_Address(0x49);
  877. Write_Date('.');
  878. delay1ms(5);
  879. }
  880. /*********************显示温度整数部分*************************/
  881. void Display_Integer(unsigned char x)
  882. {
  883. unsigned char i,j,k;
  884. i=x/100;
  885. j=x%100/10;
  886. k=x%10;
  887. Write_Address(0x46);
  888. if(flag==1) Write_Date('-');
  889. Write_Date(digit[i]);
  890. Write_Date(digit[j]);
  891. Write_Date(digit[k]);
  892. delay1ms(50) ;
  893. }
  894. /************显示温度小数部分*********************/
  895. void Display_Decimal(unsigned char x)
  896. {
  897. Write_Address(0x4A);
  898. delay1ms(5);
  899. Write_Date(digit[x]);
  900. delay1ms(50);
  901. }
  902. void displayovert(void)
  903. {
  904. unsigned char i;
  905. Write_com(0x01);
  906. delay1ms(10);
  907. Write_Address(0x03);
  908. delay1ms(10);
  909. Write_Date('W');
  910. Write_Date('A');
  911. Write_Date('R');
  912. Write_Date('N');
  913. Write_Date('I');
  914. Write_Date('N');
  915. Write_Date('G');
  916. Write_Date('!');
  917. Write_Address(0x40);
  918. while(overtemperature[i]!='\0')
  919. {
  920. Write_Date(overtemperature[i]);
  921. i++;
  922. delay1ms(10);
  923. }
  924. }
  925. /*******************温度主函数********************/
  926. void Temperature(void)
  927. {
  928. unsigned char i;
  929. delay1ms(5);
  930. if(IntDS18B20()!=0) Display_Error();
  931. else
  932. {
  933. delay1ms(1);
  934. Write_com(0x01);
  935. delay1ms(5);
  936. Display_Explain();
  937. for(length=50;length>0;length--)
  938. {
  939. for(i=0;i<6;i++)
  940. {
  941. flag=0;
  942. ReadyreadDS18B20();
  943. TL=ReadDS18B20();
  944. TH=ReadDS18B20();
  945. if((TH&0XF8)!=0X00)
  946. {
  947. flag=1;
  948. TL=~TL;
  949. TH=~TH;
  950. TL+=1;
  951. if(TL>255) TH+=1;
  952. TN=TH*16+TL/16;
  953. TD=(TL%16)*10/16;
  954. }
  955. TN=TH*16+TL/16;
  956. TD=(TL%16)*10/16;
  957. Display_Integer(TN);
  958. Display_Decimal(TD);
  959. delay1ms(10);
  960. }
  961. if(tempswitch==1)
  962. {
  963. if(TN>=Maxtemp)
  964. {
  965. Write_Address(0x03);
  966. delay1ms(5);
  967. displayovert();
  968. while(1)
  969. {
  970. baojing(1);
  971. if(CANL==0)
  972. {
  973. Write_com(0x01);
  974. delay1ms(5);
  975. break;
  976. }
  977. }
  978. }
  979. }
  980. if(CANL==0) break;
  981. }
  982. }
  983. }
  984. void tempset(void)
  985. {
  986. if(ADD==0)
  987. {
  988. delay1ms(10);
  989. if(ADD==0)
  990. {
  991. Maxtemp++;
  992. while(ADD==0);
  993. }
  994. }
  995. if(RED==0)
  996. {
  997. delay1ms(10);
  998. if(RED==0)
  999. {
  1000. Maxtemp--;
  1001. while(RED==0);
  1002. }
  1003. }
  1004. }
  1005. void Maxtempset(void) //高温报警设置
  1006. {
  1007. Write_com(0x01);
  1008. delay1ms(10);
  1009. Write_Address(0x03);
  1010. delay1ms(10);
  1011. Write_Date('T');
  1012. Write_Date('E');
  1013. Write_Date('M');
  1014. Write_Date('P');
  1015. Write_Address(0x09);
  1016. Write_Date('S');
  1017. Write_Date('E');
  1018. Write_Date('T');
  1019. Write_Address(0x40);
  1020. Write_Date('M');
  1021. Write_Date('A');
  1022. Write_Date('X');
  1023. Write_Date(':');
  1024. while(1)
  1025. {
  1026. if(SET==0)
  1027. {
  1028. delay1ms(20);
  1029. if(SET==0)
  1030. {
  1031. Write_Address(0x4e);
  1032. Write_com(0x0f);
  1033. while(1)
  1034. {
  1035. if(ADD==0)
  1036. {
  1037. delay1ms(20);
  1038. if(ADD==0)
  1039. {
  1040. Write_Address(0x4b);
  1041. delay1ms(5);
  1042. Write_Date('O');
  1043. Write_Date('N');
  1044. Write_Date(' ');
  1045. tempswitch=1;
  1046. }
  1047. }
  1048. if(RED==0)
  1049. {
  1050. delay1ms(20);
  1051. if(RED==0)
  1052. {
  1053. Write_Address(0x4b);
  1054. Write_Date('O');
  1055. Write_Date('F');
  1056. Write_Date('F');
  1057. tempswitch=0;
  1058. }
  1059. }
  1060. if(CANL==0)
  1061. {
  1062. delay1ms(500);
  1063. break;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. if(tempswitch==1)
  1069. {
  1070. Write_Address(0x4b);
  1071. Write_Date('O');
  1072. Write_Date('N');
  1073. Write_Date(' ');
  1074. tempset();
  1075. displaymaxt(Maxtemp);
  1076. if(CANL==0)
  1077. {
  1078. Write_com(0x01);
  1079. displaymainpart();
  1080. break;
  1081. }
  1082. }
  1083. if(tempswitch==0)
  1084. {
  1085. Write_com(0x0c);
  1086. Write_Address(0x4b);
  1087. Write_Date('O');
  1088. Write_Date('F');
  1089. Write_Date('F');
  1090. }
  1091. if(CANL==0)
  1092. {
  1093. Write_com(0x01);
  1094. displaymainpart();
  1095. break;
  1096. }
  1097. }
  1098. }
  1099. void alarmswitchset(void)
  1100. {
  1101. while(1)
  1102. {
  1103. if(ADD==0)
  1104. {
  1105. delay1ms(100);
  1106. if(ADD==0)
  1107. {
  1108. alarmmode=1;
  1109. Write_Address(0x4c);
  1110. Write_Date('O');
  1111. Write_Date('N');
  1112. Write_Date(' ');
  1113. }
  1114. }
  1115. if(RED==0)
  1116. {
  1117. alarmmode=0;
  1118. Write_Address(0x4c);
  1119. Write_Date('O');
  1120. Write_Date('F');
  1121. Write_Date('F');
  1122. }
  1123. if(CANL==0)
  1124. {
  1125. delay1ms(500);
  1126. break;
  1127. }
  1128. }
  1129. }
  1130. /**************** 设置模块******************/
  1131. void Set(void)
  1132. {
  1133. if(SET==0)
  1134. {
  1135. delay1ms(10);
  1136. if(SET==0)
  1137. {

  1138. Write_Address(0x40);
  1139. Write_Date('T');
  1140. Write_com(0x0f);
  1141. delay1ms(1000);
  1142. Write_com(0x0c);
  1143. while(1)
  1144. {
  1145. TimeSet();
  1146. if(ADD==0)
  1147. {
  1148. delay1ms(20);
  1149. if(ADD==0)
  1150. {
  1151. Write_com(0x01);
  1152. Write_Address(0x00);
  1153. delay1ms(5);
  1154. Write_Date('S');
  1155. ……………………

  1156. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
多功能电子钟.zip (9.2 MB, 下载次数: 48)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表