找回密码
 立即注册

QQ登录

只需一步,快速开始

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

利用PIC16F877单片机的c语言写一个时闹钟程序

[复制链接]
跳转到指定楼层
楼主
用c语言写的一个时闹钟程序

单片机用16F877,主时钟用20MHz,用32768作定时时间。可以实现2路定闹,每一路都可分别设置和开关,采用4x4键盘,16x2的字符型LCD显示。连线在程序开头有说明。

程序的功能:

(1)上电后LCD背光打开,并显示倒计时5秒,然后时钟开始工作。
(2)用模式键(*)切换模式,如显示时间、日期、闹钟1、闹钟2等,并且可以用上、下键控制加1、减1或是闹钟的On、Off。
(3)原程序有16个键,包括0~9数字键,可以直接输入要设置的时间值,但后来将数字键取消了,你仍然可以通过修改程序的部分注释恢复此功能。
(4)闹钟有2路,时间到后闹2分钟,可按任意键取消本次闹钟。闹钟响时有2种音调,是用PIC的PWM实现的。
(5)按任意键可打开背光,1分钟后自动关闭背光。
(6)RA0~RA3为按键扫描输入,应接下拉电阻。


主程序:
  1. // FileName: Main.c
  2. // MCU: MicroChipPIC16F877
  3. // Tool: CCS-C compiler
  4. // Author: KingEDA, MSN:kingeda@163.com, skype:kingeda, E-mail:kingeda@163.com
  5. // Description:
  6. // A TImer program
  7. // Ver 0.1: 2003-03-31, all cLOCk funcTIon with date display, 2 way alarm.
  8. // Ver 0.2: 2003-05-05, (1) Alarm default is on,modify alarm1 TIme to 7:00:00,
  9. // and alarm2 to 13:30:00.
  10. // (2) Backlight will be enabLEDwhen alarming.
  11. // (3) AutomaTIc adjust day(28,30,31)。
  12. // (4) Automatic move cursor to next location when set item.
  13. // PINConnection:
  14. // RC0~1 : 32768Hz crystal
  15. // RC2 : Buzzer
  16. // RC3 : LCD Back Light,drive aPNPBJT
  17. // RD0~RD7 : to LCD DB0~DB7
  18. // RA0~RA3 : keypad col in
  19. // RC4~RC7 : keypad line out
  20. // 7 8 9 #
  21. // 4 5 6 ↑
  22. // 1 2 3 ↓
  23. // 0 ← → *
  24. // RE0 : LCD RS
  25. // RE1 : LCD RW
  26. // RE2 : LCD E
  27. #include “my16f877.h”
  28. #device ICD=true
  29. //#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT
  30. #use delay(clock = 24000000)
  31. //#use fast_io(C)
  32. #use fast_io(E)
  33. #define lcd_busy (lcd_read_addr()&0x80) == 0x80
  34. #define time_start_addr 0x80+0x04
  35. #define time_hourh_addr time_start_addr
  36. #define time_hourl_addr time_start_addr+1
  37. #define time_minuteh_addr time_start_addr+3
  38. #define time_minutel_addr time_start_addr+4
  39. #define time_secondh_addr time_start_addr+6
  40. #define time_secondl_addr time_start_addr+7
  41. #define key_0 0x11
  42. #define key_1 0x21
  43. #define key_2 0x22
  44. #define key_3 0x24
  45. #define key_4 0x41
  46. #define key_5 0x42
  47. #define key_6 0x44
  48. #define key_7 0x81
  49. #define key_8 0x82
  50. #define key_9 0x84
  51. #define key_left 0x12
  52. #define key_right 0x14
  53. #define key_up 0x48
  54. #define key_down 0x28
  55. #define key_mode 0x18
  56. #define key_CANcel 0x88
  57. char StrPower1[] = “ *Power on* ”;
  58. char StrSetTime[] = “ * Adjust time* ”;
  59. char StrSetDate[] = “ * Adjust date* ”;
  60. char StrAlarm1[] = “ * Set alarm 1* ”;
  61. char StrAlarm2[] = “ * Set alarm 2* ”;
  62. unsigned char PORTC_MAP;
  63. #bit BackLightEn = PORTC_MAP.3
  64. unsigned char BackLightTimer;
  65. int1 led;
  66. #bit lcd_rs = PORTE.0
  67. #bit lcd_rw = PORTE.1
  68. #bit lcd_e= PORTE.2
  69. #byte lcd_bus = PORTD
  70. #byte lcd_dir = TRISD
  71. #define PWM_on 0x0c
  72. #define PWM_off 0x00
  73. #define PWM_period 200
  74. #define PWM_DC 100
  75. unsigned char lcd_addr;
  76. unsigned char KeyLine;
  77. unsigned char KeyOld;
  78. unsigned char KeyNew;
  79. struct mTime {
  80. unsigned char hourh; // hour,0~23
  81. unsigned char hourl;
  82. unsigned char minuteh; // minute,0~59
  83. unsigned char minutel;
  84. unsigned char secondh; // second,0~59
  85. unsigned char secondl;
  86. };
  87. struct mTime CurrentTime = {1,2,0,0,0,0};
  88. struct mTime AlarmTime1 = {0,7,0,0,0,0}; // 07:00:00
  89. struct mTime AlarmTime2 = {1,3,3,0,0,0}; // 13:30:00
  90. unsigned char AlarmStatus;
  91. #bit Alarm1Enable = AlarmStatus.0
  92. #bit Alarm2Enable = AlarmStatus.1
  93. #bit Alarm1Alarm = AlarmStatus.2
  94. #bit Alarm2Alarm = AlarmStatus.3
  95. unsigned char Alarm1Cnt; // alarm1 second count
  96. unsigned char Alarm2Cnt;
  97. unsigned char CurrentMode;
  98. #define mode_time 0
  99. #define mode_set_time 1
  100. #define mode_set_date 2
  101. #define mode_set_alarm1 3
  102. #define mode_set_alarm2 4
  103. unsigned char adjust_item;
  104. struct mDate {
  105. unsigned char year1; //
  106. unsigned char year2;
  107. unsigned char year3;
  108. unsigned char year4;
  109. unsigned char monthh;
  110. unsigned char monthl;
  111. unsigned char dayh;
  112. unsigned char dayl;
  113. };
  114. struct mDate CurrentDate = {2,0,0,3,0,1,0,1};
  115. unsigned char *pStr;
  116. // -------------------------------------------------------
  117. unsigned char lcd_read_addr()
  118. {
  119. unsigned char ch;
  120. lcd_dir = 0xff; // read from lcd
  121. lcd_rs = 0;
  122. lcd_rw = 1; // inst
  123. lcd_e = 1;
  124. #asm
  125. nop
  126. nop
  127. nop
  128. #endasm
  129. ch = lcd_bus;
  130. lcd_e = 0;
  131. lcd_dir = 0x00; //set write to lcd
  132. return ch;
  133. }
  134. // -------------------------------------------------------
  135. unsigned char lcd_write_data(unsigned char ch)
  136. {
  137. while (lcd_busy)
  138. { restart_wdt(); }
  139. lcd_rs = 1; // data
  140. lcd_rw = 0; // write
  141. lcd_bus = ch; // write out
  142. lcd_e = 1;
  143. #asm
  144. nop
  145. nop
  146. nop
  147. #endasm
  148. lcd_e = 0;
  149. return ‘Y’;
  150. }
  151. // -------------------------------------------------------
  152. unsigned char lcd_write_inst(unsigned char ch)
  153. {
  154. while (lcd_busy)
  155. { restart_wdt(); }
  156. lcd_rs = 0; // inst
  157. lcd_rw = 0; // write
  158. lcd_bus = ch;
  159. lcd_e = 1;
  160. #asm
  161. nop
  162. nop
  163. nop
  164. #endasm
  165. lcd_e = 0;
  166. return ‘Y’;
  167. }
  168. // -------------------------------------------------------
  169. unsigned char lcd_read_data()
  170. {
  171. unsigned char ch;
  172. while (lcd_busy)
  173. { restart_wdt(); }
  174. lcd_dir = 0xff; // read from lcd
  175. lcd_rs = 1; // data
  176. lcd_rw = 1; // read
  177. lcd_e = 1;
  178. #asm
  179. nop
  180. nop
  181. nop
  182. #endasm
  183. ch = lcd_bus; // read in
  184. lcd_e = 0;
  185. lcd_dir = 0x00; //set write to lcd
  186. return ch;
  187. }
  188. // -------------------------------------------------------
  189. void lcd_init()
  190. {
  191. unsigned char Tempch;
  192. lcd_addr = 0;
  193. delay_ms(100);
  194. Tempch = 0x38; // 1-line mode,5x8 dots
  195. lcd_write_inst(Tempch); // Function set
  196. Tempch = 0x0f; // lcd on,cursor on,blink on
  197. lcd_write_inst(Tempch); // Display on/off
  198. Tempch = 0x06; // Increment mode,Entire shift off
  199. lcd_write_inst(Tempch);
  200. Tempch = 0x01; // clear display
  201. lcd_write_inst(Tempch);
  202. delay_ms(3);
  203. }
  204. // -------------------------------------------------------
  205. //#int_timer1
  206. //void timer1_interrupt(void)
  207. #int_ccp2
  208. void ccp2_interrupt(void)
  209. {
  210. //TMR1H = 0x80;
  211. if (CurrentTime.secondl==9)
  212. {
  213. CurrentTime.secondl=0;
  214. if (CurrentTime.secondh==5)
  215. {
  216. CurrentTime.secondh=0;
  217. if (CurrentTime.minutel==9)
  218. {
  219. CurrentTime.minutel=0;
  220. if (CurrentTime.minuteh==5)
  221. {
  222. CurrentTime.minuteh=0;
  223. if (CurrentTime.hourl==9)
  224. {
  225. CurrentTime.hourl=0;
  226. CurrentTime.hourh++;
  227. }
  228. else if((CurrentTime.hourl==3) && (CurrentTime.hourh==2))
  229. {
  230. CurrentTime.hourl=0;
  231. CurrentTime.hourh=0;
  232. if ((((CurrentDate.dayl == 8) || (CurrentDate.dayl == 9)) && (CurrentDate.dayh == 2) && (CurrentDate.monthl == 2) && (CurrentDate.monthh == 0)) ||
  233. ((CurrentDate.dayl == 0) && (CurrentDate.dayh == 3) && ((((CurrentDate.monthl == 4) || (CurrentDate.monthl == 6)
  234. || (CurrentDate.monthl == 9)) && (CurrentDate.monthh == 0)) || ((CurrentDate.monthl == 1) && (CurrentDate.monthh == 1)))) ||
  235. ((CurrentDate.dayl == 1) && (CurrentDate.dayh == 3)))
  236. {
  237. CurrentDate.dayl=1;
  238. CurrentDate.dayh=0;
  239. if ((CurrentDate.monthl == 2) && (CurrentDate.monthh == 1))
  240. {
  241. CurrentDate.monthl = 1;
  242. CurrentDate.monthh = 0;
  243. if (CurrentDate.year4 == 9)
  244. {
  245. CurrentDate.year4 = 0;
  246. if (CurrentDate.year3 == 9)
  247. {
  248. CurrentDate.year3 = 0;
  249. if (CurrentDate.year2 == 9)
  250. {
  251. CurrentDate.year2 = 0;
  252. CurrentDate.year1++;
  253. }
  254. else
  255. CurrentDate.year2++;
  256. }
  257. else
  258. CurrentDate.year3++;
  259. }
  260. else
  261. CurrentDate.year4++;
  262. }
  263. else if(CurrentDate.monthl == 9)
  264. {
  265. CurrentDate.monthl = 0;
  266. CurrentDate.monthh++;
  267. }
  268. else
  269. CurrentDate.monthl++;
  270. }
  271. else if(CurrentDate.dayl == 9)
  272. {
  273. CurrentDate.dayl=0;
  274. CurrentDate.dayh++;
  275. }
  276. else
  277. CurrentDate.dayl++;
  278. }
  279. else
  280. CurrentTime.hourl++;
  281. }
  282. else
  283. CurrentTime.minuteh++;
  284. }
  285. else
  286. CurrentTime.minutel++;
  287. }
  288. else
  289. CurrentTime.secondh++;
  290. }
  291. else
  292. CurrentTime.secondl++;
  293. if ((Alarm1Alarm == false) & (Alarm2Alarm == false))
  294. {
  295. led = 0;
  296. CCP1CON = PWM_off;
  297. }
  298. else
  299. {
  300. if (led == 1)
  301. {
  302. led = 0;
  303. PR2 = PWM_period; // set pwm period
  304. CCPR1L = PWM_DC; // set pwm duty cycle
  305. //CCP1CON = PWM_on;
  306. }
  307. else
  308. {
  309. led = 1;
  310. PR2 = PWM_period/2; // set pwm period
  311. CCPR1L = PWM_DC/2; // set pwm duty cycle
  312. //CCP1CON = PWM_off;
  313. }
  314. }
  315. Alarm1Cnt++;
  316. Alarm2Cnt++;
  317. if (BackLightEn == 0)
  318. if (((BackLightTimer++)》=60) & (Alarm1Alarm == false) & (Alarm1Alarm == false))
  319. BackLightEn = 1; // dISAble backlight
  320. PORTC = PORTC_MAP;
  321. //TMR1IF = 0;
  322. //PIR1 = PIR2 = 0x00;
  323. CCP2IF = 0;
  324. }
  325. // -------------------------------------------------------
  326. unsigned char get_key(void)
  327. {
  328. unsigned char key_in,tmp;
  329. TRISC = 0x03;
  330. KeyLine = 0xf0;
  331. PORTC = KeyLine | PORTC_MAP;
  332. #asm
  333. nop
  334. nop
  335. nop
  336. #endasm
  337. if ((PORTA & 0x0f) != 0)
  338. {
  339. tmp = 0x10;
  340. for (KeyLine = tmp;KeyLine!=0;KeyLine = tmp)
  341. {
  342. PORTC = KeyLine | PORTC_MAP;
  343. tmp = KeyLine 《《1;
  344. #asm
  345. nop
  346. nop
  347. nop
  348. #endasm
  349. key_in = PORTA & 0x0f;
  350. if (key_in != 0)
  351. {
  352. return (key_in | KeyLine);
  353. }
  354. }
  355. return 0;
  356. }
  357. else
  358. return 0;
  359. }
  360. // -------------------------------------------------------
  361. void set_mode(void)
  362. {
  363. if (CurrentMode == mode_set_alarm2)
  364. CurrentMode = mode_time;
  365. else
  366. {
  367. CurrentMode++;
  368. adjust_item = 0;
  369. }
  370. lcd_write_inst(0x01); // clear LCD display
  371. lcd_write_inst(time_start_addr); // set LCD line1
  372. if (CurrentMode == mode_set_time)
  373. {
  374. lcd_write_data(CurrentTime.hourh + ‘0’);
  375. lcd_write_data(CurrentTime.hourl + ‘0’);
  376. lcd_write_data(‘:’);
  377. lcd_write_data(CurrentTime.minuteh + ‘0’);
  378. lcd_write_data(CurrentTime.minutel + ‘0’);
  379. lcd_write_data(‘:’);
  380. lcd_write_data(CurrentTime.secondh + ‘0’);
  381. lcd_write_data(CurrentTime.secondl + ‘0’);
  382. pStr = StrSetTime;
  383. }
  384. else if(CurrentMode == mode_set_date)
  385. {
  386. lcd_write_data(CurrentDate.year1 + ‘0’);
  387. lcd_write_data(CurrentDate.year2 + ‘0’);
  388. lcd_write_data(CurrentDate.year3 + ‘0’);
  389. lcd_write_data(CurrentDate.year4 + ‘0’);
  390. lcd_write_data(‘/’);
  391. lcd_write_data(CurrentDate.monthh + ‘0’);
  392. lcd_write_data(CurrentDate.monthl + ‘0’);
  393. lcd_write_data(‘/’);
  394. lcd_write_data(CurrentDate.dayh + ‘0’);
  395. lcd_write_data(CurrentDate.dayl + ‘0’);
  396. pStr = StrSetDate;
  397. }
  398. else if(CurrentMode == mode_set_alarm1)
  399. {
  400. lcd_write_data(AlarmTime1.hourh + ‘0’);
  401. lcd_write_data(AlarmTime1.hourl + ‘0’);
  402. lcd_write_data(‘:’);
  403. lcd_write_data(AlarmTime1.minuteh + ‘0’);
  404. lcd_write_data(AlarmTime1.minutel + ‘0’);
  405. lcd_write_data(‘:’);
  406. lcd_write_data(AlarmTime1.secondh + ‘0’);
  407. lcd_write_data(AlarmTime1.secondl + ‘0’);
  408. lcd_write_data(‘ ’);
  409. lcd_write_data(‘O’);
  410. if (Alarm1Enable)
  411. {
  412. lcd_write_data(‘n’);
  413. }
  414. else
  415. {
  416. lcd_write_data(‘f’);
  417. lcd_write_data(‘f’);
  418. }
  419. pStr = StrAlarm1;
  420. Alarm1Cnt =0;
  421. }
  422. else if(CurrentMode == mode_set_alarm2)
  423. {
  424. lcd_write_data(AlarmTime2.hourh + ‘0’);
  425. lcd_write_data(AlarmTime2.hourl + ‘0’);
  426. lcd_write_data(‘:’);
  427. lcd_write_data(AlarmTime2.minuteh + ‘0’);
  428. lcd_write_data(AlarmTime2.minutel + ‘0’);
  429. lcd_write_data(‘:’);
  430. lcd_write_data(AlarmTime2.secondh + ‘0’);
  431. lcd_write_data(AlarmTime2.secondl + ‘0’);
  432. lcd_write_data(‘ ’);
  433. lcd_write_data(‘O’);
  434. if (Alarm2Enable)
  435. {
  436. lcd_write_data(‘n’);
  437. }
  438. else
  439. {
  440. lcd_write_data(‘f’);
  441. lcd_write_data(‘f’);
  442. }
  443. pStr = StrAlarm2;
  444. Alarm2Cnt = 0;
  445. }
  446. lcd_write_inst(0xc0); // set LCD line2
  447. if (CurrentMode != mode_time)
  448. {
  449. for (;*pStr!=0;pStr++)
  450. { // write hint message
  451. lcd_write_data(*pStr);
  452. }
  453. lcd_write_inst(0x0f); // LCD cursor on
  454. lcd_write_inst(time_start_addr); // move cursor to start
  455. }
  456. else // time mode,write date to second line
  457. {
  458. lcd_write_inst(0x0c); // LCD sursor off
  459. /* lcd_write_inst(0xc0 + 3); // set date start address
  460. lcd_write_data(CurrentDate.year1 + ‘0’);
  461. lcd_write_data(CurrentDate.year2 + ‘0’);
  462. lcd_write_data(CurrentDate.year3 + ‘0’);
  463. lcd_write_data(CurrentDate.year4 + ‘0’);
  464. lcd_write_data(‘/’);
  465. lcd_write_data(CurrentDate.monthh + ‘0’);
  466. lcd_write_data(CurrentDate.monthl + ‘0’);
  467. lcd_write_data(‘/’);
  468. lcd_write_data(CurrentDate.dayh + ‘0’);
  469. lcd_write_data(CurrentDate.dayl + ‘0’);
  470. */}
  471. if (CurrentMode == mode_set_time)
  472. {
  473. lcd_write_inst(time_start_addr); // move cursor to start
  474. }
  475. else if (CurrentMode == mode_set_date)
  476. {
  477. lcd_write_inst(time_start_addr); // move cursor to start
  478. }
  479. else if (CurrentMode == mode_set_alarm1)
  480. {
  481. lcd_write_inst(time_secondl_addr+3);
  482. adjust_item = 6;
  483. }
  484. else if (CurrentMode == mode_set_alarm2)
  485. {
  486. lcd_write_inst(time_secondl_addr+3);
  487. adjust_item = 6;
  488. }
  489. else
  490. {
  491. lcd_write_inst(0x0c); // LCD cursor off
  492. }
  493. }
  494. // -------------------------------------------------------
  495. void set_date(void)
  496. {
  497. if (adjust_item == 0) // adjust year
  498. {
  499. if ((KeyNew 》=0) & (KeyNew 《= 9))
  500. {
  501. CurrentDate.year1 = KeyNew;
  502. lcd_write_data(CurrentDate.year1 + ‘0’);
  503. //lcd_write_inst(time_start_addr);
  504. adjust_item ++;
  505. }
  506. else if (KeyNew == key_left)
  507. {
  508. adjust_item = 7;
  509. lcd_write_inst(time_start_addr + 9);
  510. }
  511. else if(KeyNew == key_right)
  512. {
  513. adjust_item ++;
  514. lcd_write_inst(time_start_addr + 1);
  515. }
  516. }
  517. else if(adjust_item == 1)
  518. {
  519. if ((KeyNew 》=0) & (KeyNew 《= 9))
  520. {
  521. CurrentDate.year2 = KeyNew;
  522. lcd_write_data(CurrentDate.year2 + ‘0’);
  523. //lcd_write_inst(time_start_addr + 1);
  524. adjust_item ++;
  525. }
  526. else if (KeyNew == key_left)
  527. {
  528. adjust_item --;
  529. lcd_write_inst(time_start_addr + 0);
  530. }
  531. else if(KeyNew == key_right)
  532. {
  533. adjust_item ++;
  534. lcd_write_inst(time_start_addr + 2);
  535. }
  536. }
  537. else if(adjust_item == 2)
  538. {
  539. if ((KeyNew 》=0) & (KeyNew 《= 9))
  540. {
  541. CurrentDate.year3 = KeyNew;
  542. lcd_write_data(CurrentDate.year3 + ‘0’);
  543. //lcd_write_inst(time_start_addr + 2);
  544. adjust_item ++;
  545. }
  546. else if (KeyNew == key_left)
  547. {
  548. adjust_item --;
  549. lcd_write_inst(time_start_addr + 1);
  550. }
  551. else if(KeyNew == key_right)
  552. {
  553. adjust_item ++;
  554. lcd_write_inst(time_start_addr + 3);
  555. }
  556. }
  557. else if(adjust_item == 3)
  558. {
  559. if ((KeyNew 》=0) & (KeyNew 《= 9))
  560. {
  561. CurrentDate.year4 = KeyNew;
  562. lcd_write_data(CurrentDate.year4 + ‘0’);
  563. //lcd_write_inst(time_start_addr + 3);
  564. adjust_item ++;
  565. lcd_write_inst(time_start_addr + 5);
  566. }
  567. else if (KeyNew == key_left)
  568. {
  569. adjust_item --;
  570. lcd_write_inst(time_start_addr + 2);
  571. }
  572. else if(KeyNew == key_right)
  573. {
  574. adjust_item ++;
  575. lcd_write_inst(time_start_addr + 5);
  576. }
  577. }
  578. else if(adjust_item == 4)
  579. {
  580. if (((CurrentDate.monthl》2) & (KeyNew == 0)) | ((CurrentDate.monthl == 0) & (KeyNew == 1))
  581. | (((CurrentDate.monthl == 1) | (CurrentDate.monthl == 2)) & (KeyNew 《2)))
  582. {
  583. CurrentDate.monthh = KeyNew;
  584. lcd_write_data(CurrentDate.monthh + ‘0’);
  585. //lcd_write_inst(time_start_addr + 5);
  586. adjust_item ++;
  587. }
  588. else if (KeyNew == key_left)
  589. {
  590. adjust_item --;
  591. lcd_write_inst(time_start_addr + 3);
  592. }
  593. else if (KeyNew == key_right)
  594. {
  595. adjust_item ++;
  596. lcd_write_inst(time_start_addr + 6);
  597. }
  598. }
  599. else if(adjust_item == 5)
  600. {
  601. if (((CurrentDate.monthh == 3) & (KeyNew 《2)) | ((CurrentDate.monthh != 3) & (KeyNew 》=0) & (KeyNew 《=9)))
  602. {
  603. CurrentDate.monthl = KeyNew;
  604. lcd_write_data(CurrentDate.monthl + ‘0’);
  605. //lcd_write_inst(time_start_addr + 6);
  606. adjust_item ++;
  607. lcd_write_inst(time_start_addr + 8);
  608. }
  609. else if (KeyNew == key_left)
  610. {
  611. adjust_item --;
  612. lcd_write_inst(time_start_addr + 5);
  613. }
  614. else if (KeyNew == key_right)
  615. {
  616. adjust_item ++;
  617. lcd_write_inst(time_start_addr + 8);
  618. }
  619. }
  620. else if(adjust_item == 6)
  621. {
  622. if (((CurrentDate.dayl》1) & ((KeyNew == 1) | (KeyNew == 2))) | ((CurrentDate.dayl == 0) & (KeyNew 》0) & (KeyNew《4))
  623. | ((CurrentDate.dayl == 1) & (KeyNew 《4)))
  624. {
  625. CurrentDate.dayh = KeyNew;
  626. lcd_write_data(CurrentDate.dayh + ‘0’);
  627. //lcd_write_inst(time_start_addr + 8);
  628. adjust_item ++;
  629. }
  630. else if (KeyNew == key_left)
  631. {
  632. adjust_item --;
  633. lcd_write_inst(time_start_addr + 6);
  634. }
  635. else if (KeyNew == key_right)
  636. {
  637. adjust_item ++;
  638. lcd_write_inst(time_start_addr + 9);
  639. }
  640. }
  641. else if(adjust_item == 7)
  642. {
  643. if (((CurrentDate.dayh == 3) & (KeyNew 《2)) | ((CurrentDate.dayh != 3) & (KeyNew 》=0) & (KeyNew 《=9)))
  644. {
  645. CurrentDate.dayl = KeyNew;
  646. lcd_write_data(CurrentDate.dayl + ‘0’);
  647. //lcd_write_inst(time_start_addr + 9);
  648. adjust_item ++;
  649. lcd_write_inst(time_start_addr + 0);
  650. }
  651. else if (KeyNew == key_left)
  652. {
  653. adjust_item --;
  654. lcd_write_inst(time_start_addr + 8);
  655. }
  656. else if (KeyNew == key_right)
  657. {
  658. adjust_item = 0;
  659. lcd_write_inst(time_start_addr + 0);
  660. }
  661. }
  662. }
  663. // -------------------------------------------------------
  664. void set_time(void)
  665. {
  666. if (adjust_item == 0) // set hourh
  667. {
  668. if (((CurrentTime.hourl 《4) & (KeyNew 《 3)) | ((CurrentTime.hourl 》3) & (KeyNew 《2)))
  669. {
  670. CurrentTime.hourh = KeyNew;
  671. lcd_write_data(CurrentTime.hourh + ‘0’); // refresh hourh
  672. //lcd_write_inst(0x10); // move cursor back
  673. adjust_item ++;
  674. }
  675. else if(KeyNew == key_left)
  676. {
  677. adjust_item = 5;
  678. lcd_write_inst(time_secondl_addr);
  679. }
  680. else if(KeyNew == key_right)
  681. {
  682. adjust_item ++;
  683. lcd_write_inst(time_hourl_addr);
  684. }
  685. }
  686. else if (adjust_item == 1) // set hourl
  687. {
  688. if (((CurrentTime.hourh == 2) & (KeyNew 《 4)) | ((CurrentTime.hourh 《 2) & (KeyNew 《=9)))
  689. {
  690. CurrentTime.hourl = KeyNew;
  691. lcd_write_data(CurrentTime.hourl + ‘0’); // refresh hourl
  692. //lcd_write_inst(0x10); // move cursor back
  693. adjust_item ++;
  694. lcd_write_inst(time_minuteh_addr);
  695. }
  696. else if(KeyNew == key_left)
  697. {
  698. adjust_item --;
  699. lcd_write_inst(time_hourh_addr);
  700. }
  701. else if(KeyNew == key_right)
  702. {
  703. adjust_item ++;
  704. lcd_write_inst(time_minuteh_addr);
  705. }
  706. }
  707. else if (adjust_item == 2) // set minuteh
  708. {
  709. if (KeyNew 《6)
  710. {
  711. CurrentTime.minuteh = KeyNew;
  712. lcd_write_data(CurrentTime.minuteh + ‘0’);
  713. //lcd_write_inst(0x10); // move cursor back
  714. adjust_item ++;
  715. }
  716. else if(KeyNew == key_left)
  717. {
  718. adjust_item --;
  719. lcd_write_inst(time_hourl_addr);
  720. }
  721. else if(KeyNew == key_right)
  722. {
  723. adjust_item ++;
  724. lcd_write_inst(time_minutel_addr);
  725. }
  726. }
  727. else if (adjust_item == 3) // set minutel
  728. {
  729. if ((KeyNew 》=0) & (KeyNew 《=9))
  730. {
  731. CurrentTime.minutel = KeyNew;
  732. lcd_write_data(CurrentTime.minutel + ‘0’);
  733. //lcd_write_inst(0x10); // move cursor back
  734. adjust_item ++;
  735. lcd_write_inst(time_secondh_addr);
  736. }
  737. else if(KeyNew == key_left)
  738. {
  739. adjust_item --;
  740. lcd_write_inst(time_minuteh_addr);
  741. }
  742. else if(KeyNew == key_right)
  743. {
  744. adjust_item ++;
  745. lcd_write_inst(time_secondh_addr);
  746. }
  747. }
  748. else if (adjust_item == 4) // set secondh
  749. {
  750. if (KeyNew 《6)
  751. {
  752. CurrentTime.secondh = KeyNew;
  753. lcd_write_data(CurrentTime.secondh + ‘0’);
  754. //lcd_write_inst(0x10); // move cursor back
  755. adjust_item ++;
  756. }
  757. else if(KeyNew == key_left)
  758. {
  759. adjust_item --;
  760. &nb, sp; lcd_write_inst(time_minutel_addr);
  761. }
  762. else if(KeyNew == key_right)
  763. {
  764. adjust_item ++;
  765. lcd_write_inst(time_secondl_addr);
  766. }
  767. }
  768. else if (adjust_item == 5) // set secondl
  769. {
  770. if ((KeyNew 》=0) & (KeyNew 《=9))
  771. {
  772. CurrentTime.secondl = KeyNew;
  773. lcd_write_data(CurrentTime.secondl + ‘0’);
  774. //lcd_write_inst(0x10); // move cursor back
  775. adjust_item = 0;
  776. lcd_write_inst(time_hourh_addr);
  777. }
  778. else if(KeyNew == key_left)
  779. {
  780. adjust_item --;
  781. lcd_write_inst(time_secondh_addr);
  782. }
  783. else if(KeyNew == key_right)
  784. {
  785. adjust_item = 0;
  786. lcd_write_inst(time_hourh_addr);
  787. }
  788. }
  789. }
  790. // -------------------------------------------------------
  791. void set_alarm1(void)
  792. {
  793. if (adjust_item == 0) // set hourh
  794. {
  795. if (((AlarmTime1.hourl 《4) & (KeyNew 《 3)) | ((AlarmTime1.hourl 》3) & (KeyNew 《2)))
  796. {
  797. AlarmTime1.hourh = KeyNew;
  798. lcd_write_data(AlarmTime1.hourh + ‘0’); // refresh hourh
  799. //lcd_write_inst(0x10); // move cursor back
  800. adjust_item ++;
  801. }
  802. else if(KeyNew == key_left)
  803. {
  804. adjust_item = 6;
  805. lcd_write_inst(time_secondl_addr + 3);
  806. }
  807. else if(KeyNew == key_right)
  808. {
  809. adjust_item ++;
  810. lcd_write_inst(time_hourl_addr);
  811. }
  812. }
  813. else if (adjust_item == 1) // set hourl
  814. {
  815. if (((AlarmTime1.hourh == 2) & (KeyNew 《 4)) | ((AlarmTime1.hourh 《 2) & (KeyNew 《=9)))
  816. {
  817. AlarmTime1.hourl = KeyNew;
  818. lcd_write_data(AlarmTime1.hourl + ‘0’); // refresh hourl
  819. //lcd_write_inst(0x10); // move cursor back
  820. adjust_item ++;
  821. lcd_write_inst(time_minuteh_addr);
  822. }
  823. else if(KeyNew == key_left)
  824. {
  825. adjust_item --;
  826. lcd_write_inst(time_hourh_addr);
  827. }
  828. else if(KeyNew == key_right)
  829. {
  830. adjust_item ++;
  831. lcd_write_inst(time_minuteh_addr);
  832. }
  833. }
  834. else if (adjust_item == 2) // set minuteh
  835. {
  836. if (KeyNew 《6)
  837. {
  838. AlarmTime1.minuteh = KeyNew;
  839. lcd_write_data(AlarmTime1.minuteh + ‘0’);
  840. //lcd_write_inst(0x10); // move cursor back
  841. adjust_item ++;
  842. }
  843. else if(KeyNew == key_left)
  844. {
  845. adjust_item --;
  846. lcd_write_inst(time_hourl_addr);
  847. }
  848. else if(KeyNew == key_right)
  849. {
  850. adjust_item ++;
  851. lcd_write_inst(time_minutel_addr);
  852. }
  853. }
  854. else if (adjust_item == 3) // set minutel
  855. {
  856. if ((KeyNew 》=0) & (KeyNew 《=9))
  857. {
  858. AlarmTime1.minutel = KeyNew;
  859. lcd_write_data(AlarmTime1.minutel + ‘0’);
  860. //lcd_write_inst(0x10); // move cursor back
  861. adjust_item ++;
  862. lcd_write_inst(time_secondh_addr);
  863. }
  864. else if(KeyNew == key_left)
  865. {
  866. adjust_item --;
  867. lcd_write_inst(time_minuteh_addr);
  868. }
  869. else if(KeyNew == key_right)
  870. {
  871. adjust_item ++;
  872. lcd_write_inst(time_secondh_addr);
  873. }
  874. }
  875. else if (adjust_item == 4) // set secondh
  876. {
  877. if (KeyNew 《6)
  878. {
  879. AlarmTime1.secondh = KeyNew;
  880. lcd_write_data(AlarmTime1.secondh + ‘0’);
  881. //lcd_write_inst(0x10); // move cursor back
  882. adjust_item ++;
  883. }
  884. else if(KeyNew == key_left)
  885. {
  886. adjust_item --;
  887. lcd_write_inst(time_minutel_addr);
  888. }
  889. else if(KeyNew == key_right)
  890. {
  891. adjust_item ++;
  892. lcd_write_inst(time_secondl_addr);
  893. }
  894. }
  895. else if (adjust_item == 5) // set secondl
  896. {
  897. if ((KeyNew 》=0) & (KeyNew 《=9))
  898. {
  899. AlarmTime1.secondl = KeyNew;
  900. lcd_write_data(AlarmTime1.secondl + ‘0’);
  901. //lcd_write_inst(0x10); // move cursor back
  902. adjust_item ++;
  903. lcd_write_inst(time_secondl_addr+3);
  904. }
  905. else if(KeyNew == key_left)
  906. {
  907. adjust_item --;
  908. lcd_write_inst(time_secondh_addr);
  909. }
  910. else if(KeyNew == key_right)
  911. {
  912. adjust_item ++;
  913. lcd_write_inst(time_secondl_addr+3);
  914. }
  915. }
  916. else if (adjust_item == 6) // set on/off
  917. {
  918. if ((KeyNew == key_up) | (KeyNew == key_down))
  919. {
  920. if (Alarm1Enable)
  921. {
  922. Alarm1Enable =false; // disable alarm1
  923. lcd_write_data(‘f’);
  924. lcd_write_data(‘f’);
  925. }
  926. else
  927. {
  928. Alarm1Enable =true; // enable alarm1
  929. lcd_write_data(‘n’);
  930. lcd_write_data(‘ ’);
  931. }
  932. //lcd_write_inst(time_secondl_addr+3);
  933. adjust_item = 0;
  934. lcd_write_inst(time_hourh_addr);
  935. Alarm1Cnt = 0;
  936. }
  937. else if(KeyNew == key_left)
  938. {
  939. adjust_item --;
  940. lcd_write_inst(time_secondl_addr);
  941. }
  942. else if(KeyNew == key_right)
  943. {
  944. adjust_item = 0;
  945. lcd_write_inst(time_hourh_addr);
  946. }
  947. }
  948. }
  949. // -------------------------------------------------------
  950. void set_alarm2(void)
  951. {
  952. if (adjust_item == 0) // set hourh
  953. {
  954. if (((AlarmTime2.hourl 《4) & (KeyNew 《 3)) | ((AlarmTime2.hourl 》3) & (KeyNew 《2)))
  955. {
  956. AlarmTime2.hourh = KeyNew;
  957. lcd_write_data(AlarmTime2.hourh + ‘0’); // refresh hourh
  958. //lcd_write_inst(0x10); // move cursor back
  959. adjust_item ++;
  960. }
  961. else if(KeyNew == key_left)
  962. {
  963. adjust_item = 6;
  964. lcd_write_inst(time_secondl_addr+3);
  965. }
  966. else if(KeyNew == key_right)
  967. {
  968. adjust_item ++;
  969. lcd_write_inst(time_hourl_addr);
  970. }
  971. }
  972. else if (adjust_item == 1) // set hourl
  973. {
  974. if (((AlarmTime2.hourh == 2) & (KeyNew 《 4)) | ((AlarmTime2.hourh 《 2) & (KeyNew 《=9)))
  975. {
  976. AlarmTime2.hourl = KeyNew;
  977. lcd_write_data(AlarmTime2.hourl + ‘0’); // refresh hourl
  978. //lcd_write_inst(0x10); // move cursor back
  979. adjust_item ++;
  980. lcd_write_inst(time_minuteh_addr);
  981. }
  982. else if(KeyNew == key_left)
  983. {
  984. adjust_item --;
  985. lcd_write_inst(time_hourh_addr);
  986. }
  987. else if(KeyNew == key_right)
  988. {
  989. adjust_item ++;
  990. lcd_write_inst(time_minuteh_addr);
  991. }
  992. }
  993. else if (adjust_item == 2) // set minuteh
  994. {
  995. if (KeyNew 《6)
  996. {
  997. AlarmTime2.minuteh = KeyNew;
  998. lcd_write_data(AlarmTime2.minuteh + ‘0’);
  999. //lcd_write_inst(0x10); // move cursor back
  1000. adjust_item ++;
  1001. }
  1002. else if(KeyNew == key_left)
  1003. {
  1004. adjust_item --;
  1005. lcd_write_inst(time_hourl_addr);
  1006. }
  1007. else if(KeyNew == key_right)
  1008. {
  1009. adjust_item ++;
  1010. lcd_write_inst(time_minutel_addr);
  1011. }
  1012. }
  1013. else if (adjust_item == 3) // set minutel
  1014. {
  1015. if ((KeyNew 》=0) & (KeyNew 《=9))
  1016. {
  1017. AlarmTime2.minutel = KeyNew;
  1018. lcd_write_data(AlarmTime2.minutel + ‘0’);
  1019. //lcd_write_inst(0x10); // move cursor back
  1020. adjust_item ++;
  1021. lcd_write_inst(time_secondh_addr);
  1022. }
  1023. else if(KeyNew == key_left)
  1024. {
  1025. adjust_item --;
  1026. lcd_write_inst(time_minuteh_addr);
  1027. }
  1028. else if(KeyNew == key_right)
  1029. {
  1030. adjust_item ++;
  1031. lcd_write_inst(time_secondh_addr);
  1032. }
  1033. }
  1034. else if (adjust_item == 4) // set secondh
  1035. {
  1036. if (KeyNew 《6)
  1037. {
  1038. AlarmTime2.secondh = KeyNew;
  1039. lcd_write_data(AlarmTime2.secondh + ‘0’);
  1040. //lcd_write_inst(0x10); // move cursor back
  1041. adjust_item ++;
  1042. }
  1043. else if(KeyNew == key_left)
  1044. {
  1045. adjust_item --;
  1046. lcd_write_inst(time_minutel_addr);
  1047. }
  1048. else if(KeyNew == key_right)
  1049. {
  1050. adjust_item ++;
  1051. lcd_write_inst(time_secondl_addr);
  1052. }
  1053. }
  1054. else if (adjust_item == 5) // set secondl
  1055. {
  1056. if ((KeyNew 》=0) & (KeyNew 《=9))
  1057. {
  1058. AlarmTime2.secondl = KeyNew;
  1059. lcd_write_data(AlarmTime2.secondl + ‘0’);
  1060. //lcd_write_inst(0x10); // move cursor back
  1061. adjust_item ++;
  1062. lcd_write_inst(time_secondl_addr+3);
  1063. }
  1064. else if(KeyNew == key_left)
  1065. {
  1066. adjust_item --;
  1067. lcd_write_inst(time_secondh_addr);
  1068. }
  1069. else if(KeyNew == key_right)
  1070. {
  1071. adjust_item ++;
  1072. lcd_write_inst(time_secondl_addr+3);
  1073. }
  1074. }
  1075. else if (adjust_item == 6) // set on/off
  1076. {
  1077. if ((KeyNew == key_up) | (KeyNew == key_down))
  1078. {
  1079. if (Alarm2Enable)
  1080. {
  1081. Alarm2Enable =false; // disable alarm2
  1082. lcd_write_data(‘f’);
  1083. lcd_write_data(‘f’);
  1084. }
  1085. else
  1086. {
  1087. Alarm2Enable =true; // enable alarm2
  1088. lcd_write_data(‘n’);
  1089. lcd_write_data(‘ ’);
  1090. }
  1091. //lcd_write_inst(time_secondl_addr+3);
  1092. adjust_item = 0;
  1093. lcd_write_inst(time_hourh_addr);
  1094. Alarm2Cnt = 0;
  1095. }
  1096. else if(KeyNew == key_left)
  1097. {
  1098. adjust_item --;
  1099. lcd_write_inst(time_secondl_addr);
  1100. }
  1101. else if(KeyNew == key_right)
  1102. {
  1103. adjust_item = 0;
  1104. lcd_write_inst(time_hourh_addr);
  1105. }
  1106. }
  1107. }
  1108. // -------------------------------------------------------
  1109. void main(void)
  1110. {
  1111. unsigned char cnt;
  1112. TRISC = 0x03; // PORTC.3 drive led,low active
  1113. PORTC_MAP = 0x00;
  1114. led = 0;
  1115. BackLightEn = 0;
  1116. BackLightTimer = 0;
  1117. PORTC = PORTC_MAP;
  1118. TRISA = 0xff; // low half byte as keyscan in
  1119. TRISE = 0x00;
  1120. ADCON0 = 0x00;
  1121. ADCON1 = 0x06; // all digital I/Os
  1122. lcd_init();
  1123. INTCON = 0x00;
  1124. lcd_write_inst(0x80); // set lcd ddram address
  1125. for (pStr = StrPower1;*pStr!=0;pStr++)
  1126. {
  1127. lcd_write_data(*pStr);
  1128. }
  1129. lcd_write_inst(0x0c); // LCD cursor off
  1130. PIR1 = PIR2 = 0x00;
  1131. T1CON = 0x0f; // T1CON: -- T1CKPS1 T1CPS0 T1OSCEN /T1SYNC TMR1CS TMR1ON
  1132. TMR1H = 0x80;
  1133. TMR1L = 0x00;
  1134. TMR1IF = 0;
  1135. CCPR2H = 0x7f;
  1136. CCPR2L = 0xff;
  1137. CCP2CON = 0x0b; // compare mode,set ccp2if,reset tmr1
  1138. CCP2IF = 0;
  1139. for (cnt=5;cnt》0;cnt--) // test for 5sec.
  1140. {
  1141. lcd_write_inst(0xc0+8); // set LCD adress
  1142. lcd_write_data(cnt + ‘0’);
  1143. while (CCP2IF == 0) // wait for timer1 overflow
  1144. {
  1145. restart_wdt();
  1146. }
  1147. //TMR1H = 0x80;
  1148. CCP2IF = 0;
  1149. }
  1150. // write current date to lcd line 2
  1151. lcd_write_inst(0x01); // clear LCD display
  1152. lcd_write_inst(0xc0+3); // set LCD line2
  1153. lcd_write_data(CurrentDate.year1 + ‘0’);
  1154. lcd_write_data(CurrentDate.year2 + ‘0’);
  1155. lcd_write_data(CurrentDate.year3 + ‘0’);
  1156. lcd_write_data(CurrentDate.year4 + ‘0’);
  1157. lcd_write_data(‘/’);
  1158. lcd_write_data(CurrentDate.monthh + ‘0’);
  1159. lcd_write_data(CurrentDate.monthl + ‘0’);
  1160. lcd_write_data(‘/’);
  1161. lcd_write_data(CurrentDate.dayh + ‘0’);
  1162. lcd_write_data(CurrentDate.dayl + ‘0’);
  1163. Alarm1Enable = true; //false;
  1164. Alarm2Enable = true; //false;
  1165. INTCON = 0xc0; // set GIE & PEIE
  1166. //TMR1IE = 1; // enable timer1 interrupt
  1167. CCP2IE = 1; // enable ccp2 interrupt
  1168. CurrentMode = mode_time;
  1169. Alarm1Cnt = Alarm2Cnt = 0;
  1170. T2CON = 0x07; // T2CON: - TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0
  1171. // timer2 enable,prescaler = 16
  1172. PR2 = PWM_period; // set pwm period
  1173. CCPR1L = PWM_DC; // set pwm duty cycle
  1174. CCP1CON = PWM_off;
  1175. while(1)
  1176. {
  1177. restart_wdt();
  1178. KeyOld = get_key();
  1179. delay_ms(20);
  1180. KeyNew = get_key();
  1181. if (((KeyNew & 0x0f) != 0x00) & (KeyNew == KeyOld))
  1182. { // some key pressed
  1183. if (BackLightEn == 1) // back light not on
  1184. {
  1185. BackLightEn = 0; // enable back-light
  1186. BackLightTimer = 0; // start delay counter
  1187. }
  1188. else
  1189. {
  1190. if (KeyNew == key_0)
  1191. KeyNew = 0;
  1192. else if(KeyNew == key_1)
  1193. KeyNew = 1;
  1194. else if(KeyNew == key_2)
  1195. KeyNew = 2;
  1196. else if(KeyNew == key_3)
  1197. KeyNew = 3;
  1198. else if(KeyNew == key_4)
  1199. KeyNew = 4;
  1200. else if(KeyNew == key_5)
  1201. KeyNew = 5;
  1202. else if(KeyNew == key_6)
  1203. KeyNew = 6;
  1204. else if(KeyNew == key_7)
  1205. KeyNew = 7;
  1206. else if(KeyNew == key_8)
  1207. KeyNew = 8;
  1208. else if(KeyNew == key_9)
  1209. KeyNew = 9;
  1210. else if (KeyNew == key_mode) // MODE key pressed
  1211. set_mode();
  1212. else if (KeyNew == key_cancel) // cancel buzzy
  1213. {
  1214. led = 0;
  1215. CCP1CON = PWM_off;
  1216. BackLightEn = 1;
  1217. BackLightTimer = 0;
  1218. }
  1219. if (CurrentMode == mode_set_time)
  1220. {
  1221. set_time();
  1222. }
  1223. else if (CurrentMode == mode_set_date)
  1224. {
  1225. set_date();
  1226. }
  1227. else if (CurrentMode == mode_set_alarm1)
  1228. {
  1229. set_alarm1();
  1230. }
  1231. else if (CurrentMode == mode_set_alarm2)
  1232. {
  1233. set_alarm2();
  1234. }
  1235. }
  1236. do // wait for key released
  1237. {
  1238. delay_ms(30);
  1239. KeyNew = get_key();
  1240. restart_wdt();
  1241. BackLightTimer = 0; // key pressed yet
  1242. }while ((KeyNew& 0x0f) != 0x00);
  1243. KeyOld = KeyNew = 0x00;
  1244. }
  1245. if ((CurrentMode == mode_set_time) | (CurrentMode == mode_time))
  1246. { // refresh time display,bacause int_timer1 dosn‘t do this
  1247. lcd_addr = lcd_read_addr() & 0x7f; // save cursor location
  1248. lcd_write_inst(time_start_addr); // set LCD line1
  1249. lcd_write_data(CurrentTime.hourh + ’0‘);
  1250. lcd_write_data(CurrentTime.hourl + ’0‘);
  1251. lcd_write_data(’:‘);
  1252. lcd_write_data(CurrentTime.minuteh + ’0‘);
  1253. lcd_write_data(CurrentTime.minutel + ’0‘);
  1254. lcd_write_data(’:‘);
  1255. lcd_write_data(CurrentTime.secondh + ’0‘);
  1256. lcd_write_data(CurrentTime.secondl + ’0‘);
  1257. lcd_write_inst(lcd_addr | 0x80); // resume cursor location
  1258. }
  1259. if (CurrentMode == mode_time)
  1260. { // refresh date display
  1261. lcd_addr = lcd_read_addr() & 0x7f; // save cursor location
  1262. lcd_write_inst(0xc0 + 3); // set LCD line2
  1263. lcd_write_data(CurrentDate.year1 + ’0‘);
  1264. lcd_write_data(CurrentDate.year2 + ’0‘);
  1265. lcd_write_data(CurrentDate.year3 + ’0‘);
  1266. lcd_write_data(CurrentDate.year4 + ’0‘);
  1267. lcd_write_data(’/‘);
  1268. lcd_write_data(CurrentDate.monthh + ’0‘);
  1269. lcd_write_data(CurrentDate.monthl + ’0‘);
  1270. lcd_write_data(’/‘);
  1271. lcd_write_data(CurrentDate.dayh + ’0‘);
  1272. lcd_write_data(CurrentDate.dayl + ’0‘);
  1273. lcd_write_inst(lcd_addr | 0x80); // resume cursor location
  1274. }
  1275. if (Alarm1Enable)
  1276. {
  1277. if((AlarmTime1.hourh == CurrentTime.hourh) & (AlarmTime1.hourl == CurrentTime.hourl)
  1278. & (AlarmTime1.minuteh == CurrentTime.minuteh) & (AlarmTime1.minutel == CurrentTime.minutel)
  1279. & (AlarmTime1.secondh == CurrentTime.secondh) & (AlarmTime1.secondl == CurrentTime.secondl))
  1280. {
  1281. Alarm1Alarm = true;
  1282. CCP1CON = PWM_on;
  1283. BackLightEn = 0;
  1284. }
  1285. if (Alarm1Cnt 》 120) // two minutes
  1286. {
  1287. Alarm1Alarm = false;
  1288. Alarm1Cnt = 0;
  1289. BackLightEn = 1;
  1290. BackLightTimer = 0;
  1291. }
  1292. }
  1293. else
  1294. {
  1295. Alarm1Alarm = false;
  1296. Alarm1Cnt = 0;
  1297. }
  1298. if (Alarm2Enable)
  1299. {
  1300. if((AlarmTime2.hourh == CurrentTime.hourh) & (AlarmTime2.hourl == CurrentTime.hourl)
  1301. & (AlarmTime2.minuteh == CurrentTime.minuteh) & (AlarmTime2.minutel == CurrentTime.minutel)
  1302. & (AlarmTime2.secondh == CurrentTime.secondh) & (AlarmTime2.secondl == CurrentTime.secondl))
  1303. {
  1304. Alarm2Alarm = true;
  1305. CCP1CON = PWM_on;
  1306. BackLightEn = 0;
  1307. }
  1308. if (Alarm2Cnt 》 120) // two minutes
  1309. {
  1310. Alarm2Alarm = false;
  1311. Alarm2Cnt = 0;
  1312. BackLightEn = 1;
  1313. BackLightTimer = 0;
  1314. }
  1315. }
  1316. else
  1317. {
  1318. Alarm2Alarm = false;
  1319. Alarm2Cnt = 0;
  1320. }
  1321. }
  1322. }
复制代码


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

使用道具 举报

沙发
ID:1 发表于 2018-11-30 19:10 | 只看该作者
补全原理图或者详细说明一下电路连接即可获得100+黑币
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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