找回密码
 立即注册

QQ登录

只需一步,快速开始

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

电子万年历仿真中出现显示内容不断闪烁的问题,求助大神怎么解决

[复制链接]
跳转到指定楼层
楼主

  1. #include<reg52.h>  
  2. #include<intrins.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. #define LCDIO P0
  6. sbit rs=P2^4;
  7. sbit rd=P2^5;
  8. sbit lcden=P2^6;
  9. uchar second,minute,hour,day,month,year,week,count=0;
  10. uchar ReadValue,num;
  11. uchar tflag;
  12. uchar code table[]={"2018-06-15 FRI"};
  13. uchar code table1[]={"13:14:00    "};
  14. uchar code table2[]= "THUFRISATSUNMONTUEWES";
  15. uchar data disdata[5];
  16. sbit DATA=P1^1;
  17. sbit RST= P1^0;
  18. sbit SCLK=P1^2;
  19. sbit menu=P3^0;
  20. sbit add=P3^1;
  21. sbit dec=P3^2;
  22. void delay(uint z)
  23. {
  24. uint x,y;
  25. for(x=z;x>0;x--)
  26. for(y=110;y>0;y--);
  27. }
  28. void delay1(uint z)
  29. {
  30. for(;z>0;z--);
  31. }
  32. void write_com(uchar com)
  33. {
  34. rs=0;
  35. rd=0;
  36. lcden=0;
  37. P0=com;
  38. delay(20);
  39. lcden=1;
  40. delay(10);
  41. lcden=0;
  42. }
  43. void write_date(uchar date)
  44. {
  45.   rs=1;
  46.   rd=0;
  47.   lcden=0;
  48.   P0=date;
  49.   delay(20);
  50.   lcden=1;
  51.   delay(10);
  52.   lcden=0;
  53.   }
  54.    void init()
  55. {
  56. uchar num;
  57. lcden=0;
  58. write_com(0x38);
  59. write_com(0x0c);
  60. write_com(0x06);
  61. write_com(0x01);
  62. delay(5);
  63. write_com(0x80);
  64. for(num=0;num<15;num++)
  65. {
  66. write_date(table[num]);
  67. delay(5);
  68. }
  69. write_com(0x80+0x40);
  70. for(num=0;num<16;num++)
  71. {
  72. write_date(table1[num]);
  73. delay(5);
  74. }
  75. }
  76. void Write1302(uchar dat)
  77. {
  78. uchar i;
  79. SCLK=0;
  80. delay1(2);
  81. for(i=0;i<8;i++)
  82. {
  83. DATA = dat&0x01;
  84. delay1(2);
  85. SCLK=1;
  86. delay1(2);
  87. SCLK=0;
  88. dat>>=1;
  89. }
  90. }
  91. void WriteSet1302(uchar Cmd,uchar dat)
  92. {
  93. RST=0;
  94. SCLK=0;
  95. RST=1;
  96. delay1(2);
  97. Write1302(Cmd);
  98. Write1302(dat);
  99. SCLK=1;
  100. RST=0;
  101. }
  102. uchar Read1302(void)
  103. {
  104. uchar i,dat;
  105. delay(2);
  106. for(i=0;i<8;i++)
  107. {
  108. dat>>=1;
  109. if(DATA==1)
  110. dat|=0x80;
  111. SCLK=1;
  112. delay1(2);
  113. SCLK=0;
  114. delay1(2);
  115. }
  116. return dat;
  117. }
  118. uchar ReadSet1302(uchar Cmd)
  119. {
  120. uchar dat;
  121. RST=0;
  122. SCLK=0;
  123. RST=1;
  124. Write1302(Cmd);
  125. dat=Read1302();
  126. SCLK=1;
  127. RST=0;
  128. return dat;
  129. }
  130. void Ini_DS1302(void)
  131. {
  132. WriteSet1302(0x8E,0x00);
  133. WriteSet1302(0x80,((0/10)<<4|(0%10)));
  134. WriteSet1302(0x82,((45/10)<<4|(45%10)));
  135. WriteSet1302(0x84,((15/10)<<4|(15%10)));
  136. WriteSet1302(0x86,((29/10)<<4|(29%10)));
  137. WriteSet1302(0x88,((11/10)<<4|(11%10)));
  138. WriteSet1302(0x8c,((10/10)<<4|(10%10)));
  139. WriteSet1302(0x8a,((4/10)<<4|(4%10)));
  140. }
  141. void DisplaySecond(uchar x)
  142. {
  143. uchar i,j;
  144. i=x/10;
  145. j=x%10;
  146. write_com(0xc7);
  147. write_date(0x30+i);
  148. write_date(0x30+j);
  149. }
  150. void DisplayMinute(uchar x)
  151. {
  152. uchar i,j;
  153. i=x/10;
  154. j=x%10;
  155. write_com(0xc4);
  156. write_date(0x30+i);
  157. write_date(0x30+j);
  158. }
  159. void DisplayHour(uchar x)
  160. {
  161. uchar i,j;
  162. i=x/10;
  163. j=x%10;
  164. write_com(0xc1);
  165. write_date(0x30+i);
  166. write_date(0x30+j);
  167. }
  168. void DisplayDay(uchar x)
  169. {
  170. uchar i,j;
  171. i=x/10;
  172. j=x% 10;
  173. write_com(0x89);
  174. write_date(0x30+i);
  175. write_date(0x30+j);
  176. }
  177. void DisplayMonth(uchar x)
  178. {
  179. uchar i,j;
  180. i=x/10;
  181. j=x%10;
  182. write_com(0x86);
  183. write_date(0x30+i);
  184. write_date(0x30+j);
  185. }
  186. void DisplayYear(uchar x)
  187. {
  188. uchar i,j;
  189. i=x/10;
  190. j=x%10;
  191. write_com(0x83);
  192. write_date(0x30+i);
  193. write_date(0x30+j);
  194. }
  195.   void DisplayWeek(uchar x)
  196. {
  197.   uchar i;
  198.   x=x*3;
  199.   write_com(0x8c);
  200.   for(i=0;i<3;i++)
  201.   {
  202.   write_date(table2[x]);
  203.   x++;
  204.   }
  205.   }
  206.   void  read_date(void)
  207. {
  208. ReadValue = ReadSet1302(0x81);
  209. second=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
  210. ReadValue = ReadSet1302(0x83);
  211. minute=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
  212. ReadValue = ReadSet1302(0x85);
  213. hour=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
  214. ReadValue = ReadSet1302(0x87);
  215. day=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
  216. ReadValue = ReadSet1302(0x89);
  217. month=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
  218. ReadValue = ReadSet1302(0x8d);
  219. year=((ReadValue&0x70)>>4)*10+(ReadValue&0x0F);
  220. ReadValue = ReadSet1302(0x8b);
  221. week=ReadValue&0x70;
  222. DisplaySecond(second);
  223. DisplayMinute(minute);
  224. DisplayHour(hour);
  225. DisplayDay(day);
  226. DisplayMonth(month);
  227. DisplayYear(year);
  228. DisplayWeek(week);
  229. }
  230. void turn_val(char newval,uchar flag,uchar newaddr,uchar s1num)
  231. {
  232. newval=ReadSet1302(newaddr);
  233. newval=((newval&0x70)>>4)*10+(newval&0x0f);
  234. if(flag)
  235. {
  236. newval++;
  237. switch(s1num)
  238. {
  239. case 1:if(newval>99) newval=0;
  240. DisplayYear(newval);
  241. break;
  242. case 2:if(newval>12) newval=1;
  243. DisplayMonth(newval);
  244. break;
  245. case 3:if(newval>31) newval=1;
  246. DisplayDay(newval);
  247. break;
  248. case 4:if(newval>6) newval=0;
  249. DisplayWeek(newval);
  250. break;
  251. case 5:if(newval>23) newval=0;
  252. DisplayHour(newval);
  253. break;
  254. case 6:if(newval>59) newval=0;
  255. DisplayMinute(newval);
  256. break;
  257. case 7:if(newval>59) newval=0;
  258. DisplaySecond(newval);
  259. break;
  260. default:break;
  261. }
  262. }
  263. else
  264. {
  265. newval-- ;
  266. switch(s1num)
  267. {
  268. case 1:if(newval==0) newval=99;
  269. DisplayYear(newval);
  270. break;
  271. case 2:if(newval==0) newval=12;
  272. DisplayMonth(newval);
  273. break;
  274. case 3:if(newval==0) newval=31;
  275. DisplayDay(newval);
  276. break;
  277. case 4:if(newval==0) newval=6;
  278. DisplayWeek(newval);
  279. break;
  280. case 5:if(newval==0) newval=23;
  281. DisplayHour(newval);
  282. break;
  283. case 6:if(newval==0) newval=59;
  284. DisplayMinute(newval);
  285. break;
  286. case 7:if(newval==0) newval=59;
  287. DisplaySecond(newval);
  288. break;
  289. default:break;
  290. }
  291. }
  292. WriteSet1302((newaddr-1),((newval/10)<<4)|(newval%10));
  293. }


  294. void key_scan(void)
  295. {
  296. uchar miao, s1num=0;
  297. if(menu==0)
  298. {
  299. delay(5);
  300. if(menu==0)
  301. {
  302. while(!menu);
  303. s1num ++ ;
  304. while(1)
  305. {
  306. if(menu==0)
  307. {
  308. delay(5);
  309. if(menu==0)
  310. {
  311. while(!menu);
  312. s1num ++;
  313. }
  314. }
  315. rd=0;
  316. miao = ReadSet1302(0x81);
  317. second= miao;
  318. WriteSet1302(0x80, miao|0x80);
  319. write_com(0x0f);
  320. if(s1num==1)
  321. {
  322. year = ReadSet1302(0x8d);
  323. write_com(0x80+4);//????
  324. if(add==0)
  325. {
  326. delay(3);
  327. if(add==0)
  328. {
  329. while(!add);
  330. turn_val(year,1,0x8d,1);
  331. }
  332. }
  333. if(dec==0)
  334. {
  335. delay(3);
  336. if(dec==0)
  337. {
  338. while(!dec);
  339. turn_val(year,0,0x8d,1);
  340. }
  341. }
  342. }
  343. if(s1num==2)
  344. {
  345. month = ReadSet1302(0x89);
  346. write_com(0x80+7);
  347. if(add==0)
  348. {
  349. delay(3);
  350. if(add==0)
  351. {
  352. while(!add);
  353. turn_val(month, 1,0x89,2);
  354. }
  355. }
  356. if(dec==0)
  357. {
  358. delay(3);
  359. if(dec==0)
  360. {
  361. while(!dec);
  362. turn_val( month,0,0x89,2);
  363. }
  364. }
  365. }
  366. if(s1num==3)
  367. {
  368. day = ReadSet1302(0x87);
  369. write_com(0x80+10);
  370. if(add==0)
  371. {
  372. delay(3);
  373. if(add==0)
  374. {
  375. while(!add);
  376. turn_val(day,1,0x87,3);
  377. }
  378. }
  379. if(dec==0)
  380. {
  381. delay(3);
  382. if(dec==0)
  383. {
  384. while(!dec);
  385. turn_val(day,0,0x87,3);
  386. }
  387. }
  388. }
  389. if(s1num==4)
  390. {
  391. week=ReadSet1302(0x8b);
  392. write_com(0x80+14);
  393. if(add==0)
  394. {
  395. delay(3);
  396. if(add==0)
  397. {
  398. while(!add);
  399. turn_val(week,1,0x8b, 4);
  400. }
  401. }
  402. if(dec==0)
  403. {
  404. delay(3);
  405. if(dec==0)
  406. {
  407. while(!dec);
  408. turn_val(week, 0, 0x8b,4);
  409. }
  410. }
  411. }
  412. if(s1num==5)
  413. {
  414. hour = ReadSet1302(0x85);
  415. write_com(0x80+0x40+2);
  416. if(add==0)
  417. {
  418. delay(3);
  419. if(add==0)
  420. {
  421. while(!add);
  422. turn_val(hour,1,0x85,5);
  423. }
  424. }
  425. if(dec==0)
  426. {
  427. delay(3);
  428. if(dec==0)
  429. {
  430. while(!dec);
  431. turn_val(hour,0,0x85,5);
  432. }
  433. }
  434. }
  435. if(s1num==6)
  436. {
  437. minute=ReadSet1302(0x83);
  438. write_com(0x80+0x40+5);
  439. if(add==0)
  440. {
  441. delay(5);
  442. if(add==0)
  443. {
  444. while(!add);
  445. turn_val(minute,0,0x83,6);
  446. }
  447. }
  448. if(dec==0)
  449. {
  450. delay(3);
  451. if(dec==0)
  452. {
  453. while(!dec);
  454. turn_val(minute,0,0x83,6);
  455. }
  456. }
  457. }
  458. if(s1num==7)
  459. {
  460. second = ReadSet1302(0x81);
  461. write_com(0x80+0x40+8);
  462. if(add==0)
  463. {
  464. delay(3);
  465. if(add==0)
  466. {
  467. while(!add);
  468. if(second == 0x60)
  469. second =0x00;
  470. turn_val(second,1,0x81,7);
  471. }
  472. }
  473. if(dec==0)
  474. {
  475. delay(3);
  476. if(dec==0)
  477. {
  478. while(!dec);
  479. turn_val(second,0,0x81,7);
  480. }
  481. }
  482. }
  483. if(s1num==8)
  484. {
  485. miao=ReadSet1302(0x81);
  486. second = miao;
  487. WriteSet1302(0x80,second&0x7f);
  488. s1num=0; //
  489. write_com(0x0c);
  490. break;
  491. }
  492. }
  493. }
  494. }
  495. }
  496. void main( )
  497. {
  498. init();
  499. Init_DS1302();
  500. while(1)
  501. {
  502. read_date();
  503. key_scan();
  504. }
  505. }
复制代码



51hei.rar

19.03 KB, 下载次数: 3

仿真文件

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

使用道具 举报

沙发
ID:234075 发表于 2018-6-14 22:19 | 只看该作者
init_ds1302();子函数没有提前声明,有函数调用该子函数在该子函数之前,所以如果子函数很多的话,最好都提前进行声明。
回复

使用道具 举报

板凳
ID:351574 发表于 2018-6-14 23:42 | 只看该作者
zhb2004xp 发表于 2018-6-14 22:19
init_ds1302();子函数没有提前声明,有函数调用该子函数在该子函数之前,所以如果子函数很多的话,最好都提 ...

感谢感谢,本来已经声明过了,但是检查了一下发现是少打了一个字母,
回复

使用道具 举报

地板
ID:330198 发表于 2018-6-15 09:21 | 只看该作者
你程序里面错误太多,不如重写,论坛里有现成的程序,善用搜索功能
回复

使用道具 举报

5#
ID:351574 发表于 2018-6-15 10:54 | 只看该作者
zhanghyg 发表于 2018-6-15 09:21
你程序里面错误太多,不如重写,论坛里有现成的程序,善用搜索功能

好的好的,我来搜个好的模仿一波
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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