当前位置:   article > 正文

4T第十五届蓝桥杯单片机模拟考试I_十五届蓝桥杯eda模拟题

十五届蓝桥杯eda模拟题

  1. #include "reg52.h"
  2. #include "ds1302.h"
  3. #include "iic.h"
  4. sfr P4 = 0xC0;
  5. sbit R1=P3^0;
  6. sbit R2=P3^1;
  7. sbit R3=P3^2;
  8. sbit R4=P3^3;
  9. sbit C1=P4^4;
  10. sbit C2=P4^2;
  11. sbit C3=P3^5;
  12. sbit C4=P3^4;
  13. code unsigned char Seg_Table[] =
  14. {
  15. 0xc0, //0
  16. 0xf9, //1
  17. 0xa4, //2
  18. 0xb0, //3
  19. 0x99, //4
  20. 0x92, //5
  21. 0x82, //6
  22. 0xf8, //7
  23. 0x80, //8
  24. 0x90, //9
  25. 0x88, //A
  26. 0x83, //b
  27. 0xc6, //C
  28. 0xa1, //d
  29. 0x86, //E
  30. 0x8e //F
  31. };
  32. unsigned char Write[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
  33. unsigned char Read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
  34. unsigned char Timer[7]={0x59,0x09,0x23,0x10,0x04,0x03,0x24};
  35. unsigned char led4=0;
  36. void DelaySMG(unsigned int t)
  37. {
  38. while(t--);
  39. }
  40. void Delay1ms() //@12.000MHz
  41. {
  42. unsigned char i, j;
  43. i = 12;
  44. j = 169;
  45. do
  46. {
  47. while (--j);
  48. } while (--i);
  49. }
  50. void Delay(unsigned char t)
  51. {
  52. unsigned char i;
  53. for(i=0;i<t;i++)
  54. {
  55. Delay1ms();
  56. }
  57. }
  58. void SelectHc573(unsigned char channel,unsigned char value)
  59. {
  60. P0=value;
  61. switch(channel)
  62. {
  63. case 4:
  64. P2 = ( P2 & 0x1f ) | 0x80;
  65. break;
  66. case 5:
  67. P2 = ( P2 & 0x1f ) | 0xa0;
  68. break;
  69. case 6:
  70. P2 = ( P2 & 0x1f ) | 0xc0;
  71. break;
  72. case 7:
  73. P2 = ( P2 & 0x1f ) | 0xe0;
  74. break;
  75. case 0:
  76. P2 = ( P2 & 0x1f ) | 0x00;
  77. break;
  78. }
  79. P2 = ( P2 & 0x1f ) | 0x00;
  80. }
  81. void ShowSMG_Bit(unsigned char pos,unsigned char value)
  82. {
  83. SelectHc573(6,0x01<<pos);
  84. SelectHc573(7,value);
  85. DelaySMG(500);
  86. SelectHc573(6,0x01<<pos);
  87. SelectHc573(7,0xff);
  88. }
  89. void ShowSMG_All(unsigned char value)
  90. {
  91. SelectHc573(6,0xff);
  92. SelectHc573(7,value);
  93. }
  94. //1)通过读取DS1302 RTC芯片,获取时间数据;
  95. //初始化
  96. void Init_DS1302()
  97. {
  98. unsigned char i;
  99. Write_Ds1302_Byte(0x8e,0x00);
  100. for(i=0;i<7;i++)
  101. {
  102. Write_Ds1302_Byte(Write[i],Timer[i]);
  103. }
  104. Write_Ds1302_Byte(0x8e,0x80);
  105. }
  106. //不断读取时钟数据
  107. void Read_DS1302()
  108. {
  109. unsigned char i;
  110. for(i=0;i<7;i++)
  111. {
  112. Timer[i]=Read_Ds1302_Byte(Read[i]);
  113. }
  114. }
  115. //时间界面
  116. void ShowSMG_Timer()
  117. {
  118. ShowSMG_Bit(0,Seg_Table[Timer[2]/16]);
  119. ShowSMG_Bit(1,Seg_Table[Timer[2]%16]);
  120. ShowSMG_Bit(2,0xbf);
  121. ShowSMG_Bit(3,Seg_Table[Timer[1]/16]);
  122. ShowSMG_Bit(4,Seg_Table[Timer[1]%16]);
  123. ShowSMG_Bit(5,0xbf);
  124. ShowSMG_Bit(6,Seg_Table[Timer[0]/16]);
  125. ShowSMG_Bit(7,Seg_Table[Timer[0]%16]);
  126. }
  127. //2)通过EEPROM实现数据记录功能;
  128. /*输入数据起始时间(时):E2PROM内部地址0
  129. 输入数据起始时间(分):E2PROM内部地址1
  130. 输入数据(高字节):E2PROM内部地址2
  131. 输入数据(低字节):E2PROM内部地址3*/
  132. unsigned char h=0; //记录输入4位数据的起始时和分数据
  133. unsigned char min=0;
  134. void Write_Eeprom(unsigned char add,unsigned char value)
  135. {
  136. I2CStart();
  137. I2CSendByte(0xa0);
  138. I2CWaitAck();
  139. I2CSendByte(add);
  140. I2CWaitAck();
  141. I2CSendByte(value);
  142. I2CSendAck(1);
  143. I2CStop();
  144. }
  145. unsigned char Read_Eeprom(unsigned char add)
  146. {
  147. unsigned char dat;
  148. I2CStart();
  149. I2CSendByte(0xa0);
  150. I2CWaitAck();
  151. I2CSendByte(add);
  152. I2CWaitAck();
  153. I2CStart();
  154. I2CSendByte(0xa1);
  155. I2CWaitAck();
  156. dat=I2CReceiveByte();
  157. I2CSendAck(1);
  158. I2CStop();
  159. return dat;
  160. }
  161. //3)通过LED指示灯完成试题要求的状态指示功能;
  162. //4)通过数码管、按键完成试题要求的数据显示和界面切换功能。
  163. unsigned char stat=1; //记录页面号
  164. unsigned int Egg=0; //记录加的值
  165. unsigned char Egg_OK()
  166. {
  167. if((Egg/1000)!=0) //代表着四位数字记录完成
  168. {
  169. //完成之后可以向存储器中存入数据
  170. unsigned int Eggt=Egg;
  171. unsigned char LSB,MSB;
  172. LSB=Eggt;
  173. MSB=Eggt>>8;
  174. Write_Eeprom(0x02,MSB);
  175. Delay(5);
  176. Write_Eeprom(0x03,LSB);
  177. Delay(5);
  178. return 0;
  179. }
  180. else
  181. {
  182. return 1;
  183. }
  184. }
  185. //输入界面
  186. void Show_Scan()
  187. {
  188. ShowSMG_Bit(0,Seg_Table[12]);
  189. ShowSMG_Bit(1,0xff);
  190. ShowSMG_Bit(2,0xff);
  191. ShowSMG_Bit(3,0xff);
  192. if(Egg>999)
  193. {
  194. ShowSMG_Bit(4,Seg_Table[Egg/1000]);
  195. }
  196. else
  197. {
  198. ShowSMG_Bit(4,0xff);
  199. }
  200. if(Egg>99)
  201. {
  202. ShowSMG_Bit(5,Seg_Table[Egg/100%10]);
  203. }
  204. else
  205. {
  206. ShowSMG_Bit(5,0xff);
  207. }
  208. if(Egg>9)
  209. {
  210. ShowSMG_Bit(6,Seg_Table[Egg/10%10]);
  211. }
  212. else
  213. {
  214. ShowSMG_Bit(6,0xff);
  215. }
  216. if(Egg>0)
  217. {
  218. ShowSMG_Bit(7,Seg_Table[Egg%10]);
  219. }
  220. else
  221. {
  222. ShowSMG_Bit(7,0xff);
  223. }
  224. }
  225. //记录界面
  226. void Show_Note()
  227. {
  228. ShowSMG_Bit(0,Seg_Table[14]);
  229. ShowSMG_Bit(1,0xff);
  230. ShowSMG_Bit(2,0xff);
  231. ShowSMG_Bit(3,Seg_Table[h/16]);
  232. ShowSMG_Bit(4,Seg_Table[h%16]);
  233. ShowSMG_Bit(5,0xbf);
  234. ShowSMG_Bit(6,Seg_Table[min/16]);
  235. ShowSMG_Bit(7,Seg_Table[min%16]);
  236. }
  237. //总页面
  238. unsigned char led=0x00;
  239. unsigned char Led_OK()
  240. {
  241. unsigned int Egg_f=0;
  242. unsigned char LSB,MSB;
  243. MSB=Read_Eeprom(0x02);
  244. Delay(5);
  245. LSB=Read_Eeprom(0x03);
  246. Egg_f=MSB;
  247. Egg_f=(Egg_f<<8)|LSB;
  248. if(Egg_f<Egg)
  249. {
  250. return 1;
  251. }
  252. else
  253. {
  254. return 0;
  255. }
  256. }
  257. void ShowSMG()
  258. {
  259. if(stat==1)
  260. {
  261. ShowSMG_Timer();
  262. if(led4==0)
  263. {
  264. led=~0x01;
  265. SelectHc573(4,led);
  266. }
  267. else
  268. {
  269. led=~0x09;
  270. SelectHc573(4,led);
  271. }
  272. }
  273. else if(stat==2)
  274. {
  275. Show_Scan();
  276. if(led4==0)
  277. {
  278. led=~0x02;
  279. SelectHc573(4,led);
  280. }
  281. else
  282. {
  283. led=~0x0a;
  284. SelectHc573(4,led);
  285. }
  286. }
  287. else if(stat==3)
  288. {
  289. Show_Note();
  290. if(led4==0)
  291. {
  292. led=~0x04;
  293. SelectHc573(4,led);
  294. }
  295. else
  296. {
  297. led=~0x0c;
  298. SelectHc573(4,led);
  299. }
  300. }
  301. }
  302. //按键
  303. void Scan_key()
  304. {
  305. C1=C2=C3=C4=1;
  306. R1=R3=R4=1;
  307. R2=0;
  308. if(C1==0) //S6
  309. {
  310. Delay(2);
  311. if(C1==0)
  312. {
  313. if(stat==2&&Egg_OK())
  314. {
  315. Egg=Egg*10+0;
  316. }
  317. }
  318. while(C1==0)
  319. {
  320. ShowSMG();
  321. }
  322. }
  323. if(C2==0) //S10
  324. {
  325. Delay(2);
  326. if(C2==0)
  327. {
  328. if(stat==2&&Egg_OK())
  329. {
  330. Egg=Egg*10+1;
  331. }
  332. }
  333. while(C2==0)
  334. {
  335. ShowSMG();
  336. }
  337. }
  338. if(C3==0) //S14
  339. {
  340. Delay(2);
  341. if(C3==0)
  342. {
  343. if(stat==2&&Egg_OK())
  344. {
  345. Egg=Egg*10+2;
  346. }
  347. }
  348. while(C3==0)
  349. {
  350. ShowSMG();
  351. }
  352. }
  353. if(C4==0) //S18
  354. {
  355. Delay(2);
  356. if(C4==0)
  357. {
  358. if(stat==2&&Egg_OK())
  359. {
  360. Egg=Egg*10+3;
  361. }
  362. }
  363. while(C4==0)
  364. {
  365. ShowSMG();
  366. }
  367. }
  368. C1=C2=C3=C4=1;
  369. R2=R1=R4=1;
  370. R3=0;
  371. if(C1==0) //S5
  372. {
  373. Delay(2);
  374. if(C1==0)
  375. {
  376. Egg=0;
  377. }
  378. while(C1==0)
  379. {
  380. ShowSMG();
  381. }
  382. }
  383. if(C2==0) //S9
  384. {
  385. Delay(2);
  386. if(C2==0)
  387. {
  388. if(stat==2&&Egg_OK())
  389. {
  390. Egg=Egg*10+4;
  391. }
  392. }
  393. while(C2==0)
  394. {
  395. ShowSMG();
  396. }
  397. }
  398. if(C3==0) //S13
  399. {
  400. Delay(2);
  401. if(C3==0)
  402. {
  403. if(stat==2&&Egg_OK())
  404. {
  405. Egg=Egg*10+5;
  406. }
  407. }
  408. while(C3==0)
  409. {
  410. ShowSMG();
  411. }
  412. }
  413. if(C4==0) //S17
  414. {
  415. Delay(2);
  416. if(C4==0)
  417. {
  418. if(stat==2&&Egg_OK())
  419. {
  420. Egg=Egg*10+6;
  421. }
  422. }
  423. while(C4==0)
  424. {
  425. ShowSMG();
  426. }
  427. }
  428. C1=C2=C3=C4=1;
  429. R2=R3=R1=1;
  430. R4=0;
  431. if(C1==0) //S4
  432. {
  433. Delay(2);
  434. if(C1==0)
  435. {
  436. stat++;
  437. if(stat==2) //开始记录数据
  438. {
  439. h=Timer[2];
  440. min=Timer[1];
  441. Write_Eeprom(0xa0,h);
  442. Write_Eeprom(0xa2,min);
  443. Egg=0;//同时Egg开始记录获取的数据
  444. }
  445. if(stat==3)
  446. {
  447. led4=Led_OK();
  448. }
  449. if(stat==4)
  450. {
  451. stat=1;
  452. }
  453. }
  454. while(C1==0)
  455. {
  456. ShowSMG();
  457. }
  458. }
  459. if(C2==0) //S8
  460. {
  461. Delay(2);
  462. if(C2==0)
  463. {
  464. if(stat==2&&Egg_OK())
  465. {
  466. Egg=Egg*10+7;
  467. }
  468. }
  469. while(C2==0)
  470. {
  471. ShowSMG();
  472. }
  473. }
  474. if(C3==0) //S12
  475. {
  476. Delay(2);
  477. if(C3==0)
  478. {
  479. if(stat==2&&Egg_OK())
  480. {
  481. Egg=Egg*10+8;
  482. }
  483. }
  484. while(C3==0)
  485. {
  486. ShowSMG();
  487. }
  488. }
  489. if(C4==0) //S16
  490. {
  491. Delay(2);
  492. if(C4==0)
  493. {
  494. if(stat==2&&Egg_OK())
  495. {
  496. Egg=Egg*10+9;
  497. }
  498. }
  499. while(C4==0)
  500. {
  501. ShowSMG();
  502. }
  503. }
  504. }
  505. void Init_Sys()
  506. {
  507. SelectHc573(0,0x00);
  508. SelectHc573(5,0x00);
  509. SelectHc573(4,0xff);
  510. ShowSMG_All(0xff);
  511. Init_DS1302(); //时钟初始化
  512. }
  513. void main()
  514. {
  515. Init_Sys();
  516. while(1)
  517. {
  518. Read_DS1302(); //不断读取时钟
  519. Scan_key(); //扫描按键
  520. ShowSMG();
  521. }
  522. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/737281
推荐阅读
相关标签
  

闽ICP备14008679号