请选择 进入手机版 | 继续访问电脑版
查看: 389|回复: 0

[云计算] 单片机实时时钟

688

主题

688

帖子

2083

积分

猿er

Rank: 1

积分
2083
发表于 2016-8-14 18:57:31
单片机实时时钟软件,用数码管显示时间,按键调节时间、闹钟
  1. /*
  2. The function of this program is to set the time and judge the time is true or not
  3. the value "location" is marked the location what you are setting "1" represent setting day,"2" represent setting month,"3" represent setting year
  4. the struct "Time" is used to save the time
  5. when value "location" less than or equal 3,it means that you're setting time now,the digment will only show the time you're setting
  6. when value "location" more than 3,it means that you have done the setting of time,the digment will show the whole time
  7. push "left" or "right" button to move the number
  8. push "up" or "down" button to add or minus the number
  9. ###this time set program is just a primer version,it can't calculation the time such as you can set 99days a month###
  10. ###this time program is just a primer+ verson,it not can judge the time is true or not,but it still only can set the data not the time like 2014-03-08,not 22:37:40 ###
  11. this time program is a primer 1.3 verson,it can judge the time is true or not,and it not only can set the data ,it can set the time,too 2014.3.9
  12. this program is a realclock program,it can show the time and set the time with the up-down-left-right keys
  13. this program is a realclock with alarm fucntion ,it can brazzer for 1 minute 2014-03-17
  14. */
  15. #include <reg52.h>
  16. #include "Head.h"
  17. #include "DS1302_drv.h"
  18. #include "digitron_drv.h"
  19. unsigned char gSetTime[]={0x13,0x01,0x21,0x11,0x14,0x00};
  20. struct Time//time struct,save the time
  21. {
  22. unsigned char year;
  23. unsigned char month;
  24. unsigned char day;
  25. unsigned char hour;
  26. unsigned char minute;
  27. unsigned char second;
  28. }time,tempTime,temp;
  29. struct Alarm
  30. {
  31. unsigned char hour;
  32. unsigned char minute;
  33. unsigned char open;
  34. }alarm;
  35. /**********************************************
  36. 功能:
  37. 显示时间
  38. 输入参数:
  39. 输出参数:
  40. 返回值:
  41. **********************************************/
  42. void DisplayTime(void)
  43. {
  44. DigShowNumber(7,time.hour>>4,0);
  45. DigShowNumber(6,time.hour&0x0f,1);
  46. DigShowNumber(5,time.minute>>4,0);
  47. DigShowNumber(4,time.minute&0x0f,1);
  48. DigShowNumber(3,time.second>>4,0);
  49. DigShowNumber(2,time.second&0x0f,0);
  50. }
  51. /**********************************************
  52. 功能:
  53. 显示日期
  54. 输入参数:
  55. 输出参数:
  56. 返回值:
  57. **********************************************/
  58. void DisplayCalendar(void)
  59. {
  60. DigShowNumber(8,2,0);
  61. DigShowNumber(7,0,0);
  62. DigShowNumber(6,time.year>>4,0);
  63. DigShowNumber(5,time.year&0x0f,1);
  64. DigShowNumber(4,time.month>>4,0);
  65. DigShowNumber(3,time.month&0x0f,1);
  66. DigShowNumber(2,time.day>>4,0);
  67. DigShowNumber(1,time.day&0x0f,0);
  68. }
  69. /**********************************************
  70. 功能:
  71. 设置时间和日期
  72. 输入参数:
  73. 输出参数:
  74. 返回值:
  75. **********************************************/
  76. void SetTimeCalendar(void)
  77. {
  78. WriteDS1302(ADR_W_WP,WP_OFF); //关闭写保护功能,即允许对各寄存器进行写操作
  79. WriteDS1302(ADR_W_YEAR,time.year); //年
  80. WriteDS1302(ADR_W_MONTH,time.month); //月
  81. WriteDS1302(ADR_W_DATE,time.day); //日
  82. WriteDS1302(ADR_W_HOUR,time.hour); //时
  83. WriteDS1302(ADR_W_MINUTES,time.minute); //分
  84. WriteDS1302(ADR_W_SECONDS,time.second); //秒
  85. WriteDS1302(ADR_W_WP,WP_ON); //启动写保护功能,即禁止对各寄存器进行写操作
  86. }
  87. /**********************************************
  88. 功能:
  89. 读取时间
  90. 输入参数:
  91. 输出参数:
  92. 返回值:
  93. **********************************************/
  94. void ReadTime(void)
  95. {
  96. time.second = ReadDS1302(ADR_R_SECONDS); //读取秒
  97. time.minute = ReadDS1302(ADR_R_MINUTES); //读取分
  98. time.hour = ReadDS1302(ADR_R_HOUR); //读取时
  99. }
  100. /**********************************************
  101. 功能:
  102. 读取日期
  103. 输入参数:
  104. 输出参数:
  105. 返回值:
  106. **********************************************/
  107. void ReadCalendar(void)
  108. {
  109. time.day = ReadDS1302(ADR_R_DATE); //读取日
  110. time.month = ReadDS1302(ADR_R_MONTH); //读取月
  111. time.year = ReadDS1302(ADR_R_YEAR); //读取年
  112. }
  113. /*---------------------------------------------------------------------------------------------------------*/
  114. /**********************************************
  115. 主函数,显示时间和日期
  116. 设置时间
  117. **********************************************/
  118. void main()
  119. {
  120. //unsigned char location=4;
  121. unsigned char location;
  122. unsigned short cntDly;
  123. unsigned short tempYear;
  124. BUZZER=0;
  125. location=10;
  126. cntDly=0;
  127. tempYear=2014;
  128. ReadTime();
  129. ReadCalendar();
  130. if((time.hour/0x10*10+time.hour%0x10)>23)
  131. {
  132. //init the time struct
  133. time.year=0x14;
  134. time.month=0x03;
  135. time.day=0x08;
  136. time.hour=0x15;
  137. time.minute=0x41;
  138. time.second=0x20;
  139. SetTimeCalendar();
  140. }
  141. tempTime.year=14;
  142. tempTime.month=3;
  143. tempTime.day=8;
  144. tempTime.hour=15;
  145. tempTime.minute=41;
  146. tempTime.second=20;
  147. temp.year=14;
  148. temp.month=3;
  149. temp.day=8;
  150. temp.hour=15;
  151. temp.minute=41;
  152. temp.second=20;
  153. alarm.hour=0;
  154. alarm.minute=0;
  155. alarm.open=0;//闹钟默认为 关
  156. while(1)
  157. {
  158. //ReadTime();//读取时间
  159. //ReadCalendar();//读取日期
  160. if(location==10)//no setting now
  161. {
  162. if(cntDly<=2000)
  163. {
  164. ++cntDly;
  165. ReadTime(); //读取时间
  166. if(((time.hour/16*10+time.hour%16)==alarm.hour)&&((time.minute/16*10+time.minute%16)==alarm.minute)&&(alarm.open))
  167. {
  168. BUZZER=~BUZZER;
  169. //P1=0;
  170. }
  171. else
  172. BUZZER=0;
  173. DisplayTime();
  174. }
  175. else if(cntDly>2000&&cntDly<=2500)
  176. {
  177. ++cntDly;
  178. ReadCalendar(); //读取时间
  179. DisplayCalendar();
  180. }
  181. else
  182. cntDly=0;
  183. }
  184. else if(location==9)//set year
  185. {
  186. DigShowNumber(8,2,0);
  187. DigShowNumber(7,0,0);
  188. DigShowNumber(6,time.year>>4,0);
  189. DigShowNumber(5,time.year&0x0f,1);
  190. }
  191. else if(location==8)//set month
  192. {
  193. DigShowNumber(4,time.month>>4,0);
  194. DigShowNumber(3,time.month&0x0f,1);
  195. }
  196. else if(location==7)//set day
  197. {
  198. DigShowNumber(2,time.day>>4,0);
  199. DigShowNumber(1,time.day&0x0f,0);
  200. }
  201. else if(location==6)//set hour
  202. {
  203. DigShowNumber(7,time.hour>>4,0);
  204. DigShowNumber(6,time.hour&0x0f,1);
  205. }
  206. else if(location==5)//set minute
  207. {
  208. DigShowNumber(5,time.minute>>4,0);
  209. DigShowNumber(4,time.minute&0x0f,1);
  210. }
  211. else if(location==4)//set second
  212. {
  213. DigShowNumber(3,time.second>>4,0);
  214. DigShowNumber(2,time.second&0x0f,0);
  215. }
  216. else if(location==3)//set alarm open
  217. {
  218. DigShow(8,10,0);
  219. DigShow(7,11,1);
  220. DigShow(5,alarm.open,0);
  221. }
  222. else if(location==2)//set alarm hour
  223. {
  224. DigShow(8,10,0);
  225. DigShow(7,11,1);
  226. DigShow(4,alarm.hour/10,0);
  227. DigShow(3,alarm.hour%10,1);
  228. }
  229. else if(location==1)//set alarm second
  230. {
  231. DigShow(8,10,0);
  232. DigShow(7,11,1);
  233. DigShow(2,alarm.minute/10,0);
  234. DigShow(1,alarm.minute%10,0);
  235. }
  236. else
  237. ;
  238. if(!(KEY_UP&&KEY_DOWN&&KEY_LEFT&&KEY_RIGHT))
  239. {
  240. tempTime.year=time.year/0x10*10+time.year%0x10;
  241. tempTime.month=time.month/0x10*10+time.month%0x10;
  242. tempTime.day=time.day/0x10*10+time.day%0x10;
  243. tempTime.hour=time.hour/0x10*10+time.hour%0x10;
  244. tempTime.minute=time.minute/0x10*10+time.minute%0x10;
  245. tempTime.second=time.second/0x10*10+time.second%0x10;
  246. temp.year=tempTime.year;
  247. temp.month=tempTime.month;
  248. temp.day=tempTime.day;
  249. temp.hour=tempTime.hour;
  250. temp.minute=tempTime.minute;
  251. temp.second=tempTime.second;
  252. delay10ms();
  253. if(!KEY_UP)
  254. {
  255. if(location==9)//set year
  256. {
  257. if(tempTime.year==99)
  258. {
  259. tempTime.year=0;
  260. tempYear=2000;
  261. }
  262. else
  263. {
  264. ++tempTime.year;
  265. ++tempYear;
  266. }
  267. }
  268. else if(location==8)//set month
  269. {
  270. if(tempTime.month==12)
  271. tempTime.month=1;
  272. else
  273. ++tempTime.month;
  274. }
  275. else if(location==7)//set day
  276. {
  277. if(tempTime.month==1||tempTime.month==3||tempTime.month==5||tempTime.month==7||tempTime.month==8||tempTime.month==10||tempTime.month==12)//1,3,5,7,8,10,12 month 31 days each month
  278. {
  279. if(tempTime.day>=31)
  280. tempTime.day=1;
  281. else
  282. ++tempTime.day;
  283. }
  284. else if(tempTime.month==4||tempTime.month==6||tempTime.month==9||tempTime.month==11)//4,6,9,11 month 30 days each month
  285. {
  286. if(tempTime.day>=30)
  287. tempTime.day=1;
  288. else
  289. ++tempTime.day;
  290. }
  291. else//2 month
  292. {
  293. if((tempYear%4==0&&tempYear%100!=0)||tempYear%400==0)//leap year 29 day in 2 month
  294. {
  295. if(tempTime.day>=29)
  296. tempTime.day=1;
  297. else
  298. ++tempTime.day;
  299. }
  300. else//not leap year 28 days in 2 month
  301. {
  302. if(tempTime.day>=28)
  303. tempTime.day=1;
  304. else
  305. ++tempTime.day;
  306. }
  307. }
  308. }
  309. else if(location==6)//set hour
  310. {
  311. if(tempTime.hour==23)
  312. tempTime.hour=0;
  313. else
  314. ++tempTime.hour;
  315. }
  316. else if(location==5)//set minute
  317. {
  318. if(tempTime.minute==59)
  319. tempTime.minute=0;
  320. else
  321. ++tempTime.minute;
  322. }
  323. else if(location==4)//set second
  324. {
  325. if(tempTime.second==59)
  326. tempTime.second=0;
  327. else
  328. ++tempTime.second;
  329. }
  330. else if(location==3)//set alarm open
  331. {
  332. if(alarm.open)
  333. alarm.open=0;
  334. else
  335. alarm.open=1;
  336. }
  337. else if(location==2)//set alarm hour
  338. {
  339. if(alarm.hour==23)
  340. alarm.hour=0;
  341. else
  342. ++alarm.hour;
  343. }
  344. else if(location==1)//set alarm minute
  345. {
  346. if(alarm.minute==59)
  347. alarm.minute=0;
  348. else
  349. ++alarm.minute;
  350. }
  351. else
  352. ;
  353. if(tempTime.month==4||tempTime.month==6||tempTime.month==9||tempTime.month==11)//4,6,9,11 month 30 days each month
  354. {
  355. if(tempTime.day>30)
  356. tempTime.day=30;//time judge
  357. }
  358. else if(tempTime.month==02)
  359. {
  360. if((tempYear%4==0&&tempYear%100!=0)||tempYear%400==0)//leap year 29 day in 2 month
  361. {
  362. if(tempTime.day>29)//time judge
  363. tempTime.day=29;
  364. }
  365. else//not leap year 28 days in 2 month
  366. {
  367. if(tempTime.day>28)//time judge
  368. tempTime.day=28;
  369. }
  370. }
  371. else
  372. ;
  373. delay160ms();
  374. }
  375. else if(!KEY_DOWN)
  376. {
  377. if(location==9)//set year
  378. {
  379. if(tempTime.year==00)
  380. {
  381. tempTime.year=99;
  382. tempYear=2099;
  383. }
  384. else
  385. {
  386. --tempTime.year;
  387. --tempYear;
  388. }
  389. }
  390. else if(location==8)//set month
  391. {
  392. if(tempTime.month==01)
  393. tempTime.month=12;
  394. else
  395. --tempTime.month;
  396. }
  397. else if(location==7)//set day
  398. {
  399. if(tempTime.month==1||tempTime.month==3||tempTime.month==5||tempTime.month==7||tempTime.month==8||tempTime.month==10||tempTime.month==12)//1,3,5,7,8,10,12 month 31 days each month
  400. {
  401. if(tempTime.day==1)
  402. tempTime.day=31;
  403. else
  404. --tempTime.day;
  405. }
  406. else if(tempTime.month==4||tempTime.month==6||tempTime.month==9||tempTime.month==11)//4,6,9,11 month 30 days each month
  407. {
  408. if(tempTime.day==1)
  409. tempTime.day=30;
  410. else
  411. --tempTime.day;
  412. }
  413. else//2 month
  414. {
  415. if((tempYear%4==0&&tempYear%100!=0)||tempYear%400==0)//leap year 29 day in 2 month
  416. {
  417. if(tempTime.day==1)
  418. tempTime.day=29;
  419. else
  420. --tempTime.day;
  421. }
  422. else//not leap year 28 days in 2 month
  423. {
  424. if(tempTime.day==1)
  425. tempTime.day=28;
  426. else
  427. --tempTime.day;
  428. }
  429. }
  430. }
  431. else if(location==6)//set hour
  432. {
  433. if(tempTime.hour==0)
  434. tempTime.hour=23;
  435. else
  436. --tempTime.hour;
  437. }
  438. else if(location==5)//set minute
  439. {
  440. if(tempTime.minute==0)
  441. tempTime.minute=59;
  442. else
  443. --tempTime.minute;
  444. }
  445. else if(location==4)//set second
  446. {
  447. if(tempTime.second==0)
  448. tempTime.second=59;
  449. else
  450. --tempTime.second;
  451. }
  452. else if(location==3)//set alarm open
  453. {
  454. if(alarm.open)
  455. alarm.open=0;
  456. else
  457. alarm.open=1;
  458. }
  459. else if(location==2)//set alarm hour
  460. {
  461. if(alarm.hour==0)
  462. alarm.hour=23;
  463. else
  464. --alarm.hour;
  465. }
  466. else if(location==1)//set alarm minute
  467. {
  468. if(alarm.minute==0)
  469. alarm.minute=59;
  470. else
  471. --alarm.minute;
  472. }
  473. else
  474. ;
  475. if(tempTime.month==4||tempTime.month==6||tempTime.month==9||tempTime.month==11)//4,6,9,11 month 30 days each month
  476. {
  477. if(tempTime.day>30)
  478. tempTime.day=30;//time judge
  479. }
  480. else if(tempTime.month==02)
  481. {
  482. if((tempYear%4==0&&tempYear%100!=0)||tempYear%400==0)//leap year 29 day in 2 month
  483. {
  484. if(tempTime.day>29)//time judge
  485. tempTime.day=29;
  486. }
  487. else//not leap year 28 days in 2 month
  488. {
  489. if(tempTime.day>28)//time judge
  490. tempTime.day=28;
  491. }
  492. }
  493. else
  494. ;
  495. delay160ms();
  496. }
  497. else if(!KEY_LEFT)
  498. {
  499. if(location==10)
  500. location=1;
  501. else
  502. ++location;
  503. delay160ms();
  504. }
  505. else if(!KEY_RIGHT)
  506. {
  507. if(location==1)
  508. location=10;
  509. else
  510. --location;
  511. delay160ms();
  512. }
  513. else
  514. ;
  515. ReadTime();
  516. ReadCalendar();
  517. if(tempTime.year!=temp.year)
  518. time.year=tempTime.year/10*16+tempTime.year%10;
  519. if(tempTime.month!=temp.month)
  520. time.month=tempTime.month/10*16+tempTime.month%10;
  521. if(tempTime.day!=temp.day)
  522. time.day=tempTime.day/10*16+tempTime.day%10;
  523. if(tempTime.hour!=temp.hour)
  524. time.hour=tempTime.hour/10*16+tempTime.hour%10;
  525. if(tempTime.minute!=temp.minute)
  526. time.minute=tempTime.minute/10*16+tempTime.minute%10;
  527. if(tempTime.second!=temp.second)
  528. time.second=tempTime.second/10*16+tempTime.second%10;
  529. SetTimeCalendar();
  530. }
  531. }
  532. }
复制代码
  1. /*=======================================================
  2. <<<<<<<<<<<<<<<<<<< 爱普雷德技术团队 >>>>>>>>>>>>>>>>>>>>
  3. <<<<<<<<<<<<<<<<<<< 我们一起追求卓越 >>>>>>>>>>>>>>>>>>>>
  4. ---------------------------------------------------------
  5. 旗舰网店: http://aplid.taobao.com/
  6. 官方邮箱: [email protected]
  7. 技术QQ1: 535676091
  8. 技术QQ2: 1915624375
  9. 技术QQ3: 154211813
  10. ---------------------------------------------------------
  11. All right reserved.2013.01
  12. ---------------------------------------------------------
  13. 文件名: DS1302_drv.c
  14. 描述: 时钟万年历模块驱动c文件
  15. 作者:
  16. =========================================================
  17. -----------------------------------------------------------------------------------------------------------*/
  18. #include "DS1302_drv.h"
  19. /**********************************************
  20. 功能:
  21. 向DS1302发送一个字节
  22. 输入参数:
  23. TByte:发送的字节
  24. 输出参数:
  25. 返回值:
  26. **********************************************/
  27. static void TransmitByte(uint8 TByte)
  28. {
  29. uint8 i;
  30. for(i=0;i<8;++i)
  31. {
  32. DS1302_CLK=0;
  33. if(TByte&0x01)
  34. DS1302_IO=1;
  35. else
  36. DS1302_IO=0;
  37. DS1302_CLK=1;
  38. TByte>>=1;
  39. }
  40. }
  41. /**********************************************
  42. 功能:
  43. 从DS1302接收一个字节
  44. 输入参数:
  45. 输出参数:
  46. 返回值:
  47. RecByte:从DS1302所接收到的字节
  48. **********************************************/
  49. static uint8 ReceiveByte()
  50. {
  51. uint8 i,RetByte;
  52. for(i=0;i<8;++i)
  53. {
  54. DS1302_CLK=0;
  55. RetByte>>=1;
  56. if(DS1302_IO)
  57. RetByte|=0x80;
  58. else
  59. RetByte&=~0x80;
  60. DS1302_CLK=1;
  61. }
  62. return RetByte;
  63. }
  64. /**********************************************
  65. 功能:
  66. 写一字节数据到DS1302某地址
  67. 输入参数:
  68. Adr:寄存器地址
  69. WByte:要写到对应地址的字节
  70. 输出参数:
  71. 返回值:
  72. **********************************************/
  73. void WriteDS1302(uint8 Adr,uint8 WByte)
  74. {
  75. DS1302_RST=0;
  76. DS1302_CLK=0;
  77. DS1302_RST=1;
  78. TransmitByte(Adr);
  79. TransmitByte(WByte);
  80. DS1302_RST=0;
  81. }
  82. /**********************************************
  83. 功能:
  84. 从DS1302某地址读一字节数据
  85. 输入参数:
  86. Adr:寄存器地址
  87. 输出参数:
  88. 返回值:
  89. 从DS1302所读取的字节
  90. **********************************************/
  91. uint8 ReadDS1302(uint8 Adr)
  92. {
  93. uint8 RetByte;
  94. DS1302_RST=0;
  95. DS1302_CLK=0;
  96. DS1302_RST=1;
  97. TransmitByte(Adr);
  98. RetByte=ReceiveByte();
  99. DS1302_RST=0;
  100. return RetByte;
  101. }
复制代码
  1. /*=======================================================
  2. <<<<<<<<<<<<<<<<<<< 爱普雷德技术团队 >>>>>>>>>>>>>>>>>>>>
  3. <<<<<<<<<<<<<<<<<<< 我们一起追求卓越 >>>>>>>>>>>>>>>>>>>>
  4. ---------------------------------------------------------
  5. 旗舰网店: http://aplid.taobao.com/
  6. 官方邮箱: [email protected]
  7. 技术QQ1: 535676091
  8. 技术QQ2: 1915624375
  9. 技术QQ3: 154211813
  10. ---------------------------------------------------------
  11. All right reserved.2013.01
  12. ---------------------------------------------------------
  13. 文件名: digitron_drv.c
  14. 描述: 数码管模块c文件
  15. 作者:
  16. =========================================================
  17. -----------------------------------------------------------------------------------------------------------*/
  18. #include "digitron_drv.h"
  19. code uint8 Segment[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0xc7};
  20. code uint8 Select[] = {0xff,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
  21. code uint8 SegmentWithDot[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x47};
  22. /**********************************************
  23. 功能:
  24. 输出位选字节和段码字节
  25. 输入参数:
  26. SelectByte: 位选字节
  27. SegmentByte: 段码字节
  28. 输出参数:
  29. 返回值:
  30. **********************************************/
  31. static void DigOutput(uint8 SelectByte, uint8 SegmentByte)
  32. {
  33. uint8 i;
  34. DIG_SHCP = 0;
  35. DIG_STCP = 0;
  36. for(i=0; i<8; i++)
  37. {
  38. if(SegmentByte&0x80)
  39. {
  40. DIG_DATA = 1;
  41. }
  42. else
  43. {
  44. DIG_DATA = 0;
  45. }
  46. _nop_();
  47. DIG_SHCP = 1;
  48. _nop_();
  49. DIG_SHCP = 0;
  50. _nop_();
  51. SegmentByte <<= 1;
  52. }
  53. for(i=0; i<8; i++)
  54. {
  55. if(SelectByte&0x80)
  56. DIG_DATA = 1;
  57. else
  58. DIG_DATA = 0;
  59. _nop_();
  60. DIG_SHCP = 1;
  61. _nop_();
  62. DIG_SHCP = 0;
  63. _nop_();
  64. SelectByte <<= 1;
  65. }
  66. DIG_STCP = 1;
  67. _nop_();
  68. DIG_STCP = 0;
  69. _nop_();
  70. }
  71. /**********************************************
  72. 功能:
  73. 在某位显示自定义段码
  74. 输入参数:
  75. DigSelect: 数码管位选择(1——8,即最右到最左)
  76. CustomSeg: 自定义段码值
  77. 输出参数:
  78. 返回值:
  79. **********************************************/
  80. void DigShowCustom(uint8 DigSelect, uint8 CustomSeg)
  81. {
  82. if(0<DigSelect<9)
  83. {
  84. DigOutput(Select[DigSelect],CustomSeg);
  85. }
  86. }
  87. /**********************************************
  88. 功能:
  89. 在某位显示数字
  90. 输入参数:
  91. DigSelect: 数码管位选择(1——8,即最右到最左)
  92. Number: 数字(0——9)
  93. Dp: 小数点(1:显示;0:不显示)
  94. 输出参数:
  95. 返回值:
  96. **********************************************/
  97. void DigShowNumber(uint8 DigSelect, uint8 Number, bit Dp)
  98. {
  99. if((0<DigSelect<9)&&(Number<10))
  100. {
  101. if(Dp)
  102. {
  103. DigOutput(Select[DigSelect],(Segment[Number]&~0x80));
  104. }
  105. else
  106. {
  107. DigOutput(Select[DigSelect],(Segment[Number]|0x80));
  108. }
  109. }
  110. }
  111. /**********************************************
  112. 功能:
  113. 初始化数码管,使数码管全灭
  114. 输入参数:
  115. 输出参数:
  116. 返回值:
  117. **********************************************/
  118. void DigInit(void)
  119. {
  120. DIG_DATA = 0;
  121. DIG_SHCP = 0;
  122. DIG_STCP = 0;
  123. DigOutput(0x00,0xff);
  124. }
  125. void DigShow(unsigned char location,unsigned char number,unsigned char dot)
  126. {
  127. unsigned char seg,sel,i;
  128. sel=Select[location];
  129. if(dot)
  130. seg=SegmentWithDot[number];
  131. else
  132. seg=Segment[number];
  133. for(i=0; i<8; i++)
  134. {
  135. if(seg&0x80)
  136. {
  137. DIG_DATA = 1;
  138. }
  139. else
  140. {
  141. DIG_DATA = 0;
  142. }
  143. DIG_SHCP = 1;
  144. DIG_SHCP = 0;
  145. seg <<= 1;
  146. }
  147. for(i=0; i<8; i++)
  148. {
  149. if(sel&0x80)
  150. DIG_DATA = 1;
  151. else
  152. DIG_DATA = 0;
  153. DIG_SHCP = 1;
  154. DIG_SHCP = 0;
  155. sel <<= 1;
  156. }
  157. DIG_STCP = 1;
  158. DIG_STCP = 0;
  159. }
复制代码
  1. #include"Head.h"
  2. void delay10ms(void)
  3. {
  4. unsigned char a,b;
  5. for(b=249;b>0;b--)
  6. for(a=17;a>0;a--);
  7. }
  8. void delay160ms(void)
  9. {
  10. unsigned char a,b,c;
  11. for(c=69;c>0;c--)
  12. for(b=194;b>0;b--)
  13. for(a=4;a>0;a--);
  14. }
复制代码


回复

使用道具 举报