当前位置:   article > 正文

蓝桥杯单片机第14届省赛本科组客观题(赛后完美实现版)_第十四届蓝桥杯单片机省赛

第十四届蓝桥杯单片机省赛

目录

前言

赛程难度:

代码框架:

细节归纳:

主观题题目:

代码部分:

代码资源:


补番提示:主页有最新版的完美实现版本,欢迎查看交流。都附带工程文件。

前言

首次参加的蓝桥杯比赛,发挥一般以为寄了,没想到活了,于是有了今天的这篇文章。

贴个图证实一下

8fc18f882e054a68bd445338da2271bb.jpeg


然后给大家看一下工程的完成情况↓↓

蓝桥杯第14届省赛本科组

赛程难度:

相比于往届的比赛难度真的上升了,逻辑难度大概提升了两个档次,但是整个工程组成的繁琐度提高了三个档次的样子,这个看个人感受吧,取决于往届的比赛你做了多少,也看个人吧。

代码框架:

说下这份主观题,有哪些我觉得需要注意的点。

1.注意很多不同的.C文件和main.c文件之间的变量传递。

2.整个过程我一开始使用的是3个定时器,但是我发现这样定时器的中断优先级很难把握,整个工程也很臃肿,所以在NE555频率检测中用到的两个定时器,选定时器1作为整个过程的标志位检测及LED状态更新。

3.在主函数中进行按键扫描,数码管显示,光敏采集,

细节归纳:

这份题目我觉的难度还是很高的,湿度数据的采集、数码管显示的时候很多的变量、显示的精度、不同类型的变量之间的运算精度、LED的控制、中断函数的逻辑编写、非常推荐准备下一届的去自己写一写,肯定有很多的收获。

主观题题目:

1e552a4c3a464261a8079d8d02e7daca.png


fe00cee31bbf4178833d0ec397b22125.png


3040ed1fbcbb4a43b38d19ad908e4810.png


f3920fd90fb440d7b1e981e64755a590.png


d467d9ab77c442f980b8b899306fdf21.png

代码部分:

main.c

  1. #include <STC15F2K60S2.H>
  2. #include <ds1302.H>
  3. #include <seg.H>
  4. #include <base.H>
  5. #include <onewire.H>
  6. #include <ne555.H>
  7. #include <iic.H>
  8. bit i=0,j=0,L4=0;
  9. extern unsigned char Time[7];
  10. extern unsigned int count,count_s,dat;
  11. unsigned char S4_Flag=0,Set_Temp=30,S5_Flag=0,Led_Run=0;
  12. unsigned long Temp_Value,Sum_Temp,Last_Temp,Last_Temp_1;
  13. unsigned int Humidity_Value,Sum_Humidity,Last_Humidity,Last_Humidity_1;
  14. unsigned char Count_chufa=0,add_1=0,Led_L5=0;
  15. unsigned char Flag_2=0,Count_Last=0,Last_Com=0;
  16. unsigned char Light=0,Flag_Light=0,Flag_L6=0;
  17. unsigned char Time_1,Time_2,Dis_Temp_Humidity,Flag_Temp_Humidity;
  18. void Delay5ms() //@12.000MHz
  19. {
  20. unsigned char i, j;
  21. i = 59;
  22. j = 90;
  23. do
  24. {
  25. while (--j);
  26. } while (--i);
  27. }
  28. void Display_Time(void) //显示时间
  29. {
  30. Seg(1,Time[2]/10);
  31. Delay2ms();
  32. Seg(2,Time[2]%10);
  33. Delay2ms();
  34. Seg(3,21);
  35. Delay2ms();
  36. Seg(4,Time[1]/10);
  37. Delay2ms();
  38. Seg(5,Time[1]%10);
  39. Delay2ms();
  40. Seg(6,21);
  41. Delay2ms();
  42. Seg(7,Time[0]/10);
  43. Delay2ms();
  44. Seg(8,Time[0]%10);
  45. Delay2ms();
  46. }
  47. void Display_Back_Temp(void) //c 显示温度
  48. {
  49. if(Count_chufa==0)
  50. {
  51. Seg(1,22);
  52. Delay2ms();
  53. Seg(2,20);
  54. Delay2ms();
  55. Seg(3,20);
  56. Delay2ms();
  57. Seg(4,20);
  58. Delay2ms();
  59. Seg(5,20);
  60. Delay2ms();
  61. Seg(6,20);
  62. Delay2ms();
  63. Seg(7,20);
  64. Delay2ms();
  65. Seg(8,20);
  66. Delay2ms();
  67. }else
  68. {
  69. Seg(1,22);
  70. Delay2ms();
  71. Seg(2,20);
  72. Delay2ms();
  73. Seg(3,Set_Temp/10);
  74. Delay2ms();
  75. Seg(4,Set_Temp%10);
  76. Delay2ms();
  77. Seg(5,21);
  78. Delay2ms();
  79. Seg(6,(Sum_Temp/Count_chufa)/100000);
  80. Delay2ms();
  81. Seg(7,(Sum_Temp/Count_chufa)%100000/10000+10);
  82. Delay2ms();
  83. Seg(8,(Sum_Temp/Count_chufa)%10000/1000);
  84. Delay2ms();
  85. }
  86. }
  87. void Display_Set_Value(void)//p 设定温度
  88. {
  89. Seg(1,24);
  90. Delay2ms();
  91. Seg(2,20);
  92. Delay2ms();
  93. Seg(3,20);
  94. Delay2ms();
  95. Seg(4,20);
  96. Delay2ms();
  97. Seg(5,20);
  98. Delay2ms();
  99. Seg(6,20);
  100. Delay2ms();
  101. Seg(7,Set_Temp/10);
  102. Delay2ms();
  103. Seg(8,Set_Temp%10);
  104. Delay2ms();
  105. }
  106. void Display_Back_Humidity(void) //h 湿度
  107. {
  108. if(Count_chufa==0)
  109. {
  110. Seg(1,26);
  111. Delay2ms();
  112. Seg(2,20);
  113. Delay2ms();
  114. Seg(3,20);
  115. Delay2ms();
  116. Seg(4,20);
  117. Delay2ms();
  118. Seg(5,20);
  119. Delay2ms();
  120. Seg(6,20);
  121. Delay2ms();
  122. Seg(7,20);
  123. Delay2ms();
  124. Seg(8,20);
  125. Delay2ms();
  126. }else
  127. {
  128. if(Humidity_Value==0)
  129. {
  130. Seg(1,26);
  131. Delay2ms();
  132. Seg(2,20);
  133. Delay2ms();
  134. Seg(3,6);
  135. Delay2ms();
  136. Seg(4,8);
  137. Delay2ms();
  138. Seg(5,21);
  139. Delay2ms();
  140. Seg(6,23);
  141. Delay2ms();
  142. Seg(7,23); //a
  143. Delay2ms();
  144. Seg(8,23);
  145. Delay2ms();
  146. }
  147. else{
  148. Seg(1,26);
  149. Delay2ms();
  150. Seg(2,20);
  151. Delay2ms();
  152. Seg(3,6);
  153. Delay2ms();
  154. Seg(4,8);
  155. Delay2ms();
  156. Seg(5,21);
  157. Delay2ms();
  158. Seg(6,(Sum_Humidity/Count_chufa)/100);
  159. Delay2ms();
  160. Seg(7,(Sum_Humidity/Count_chufa)%100/10+10);
  161. Delay2ms();
  162. Seg(8,(Sum_Humidity/Count_chufa)%10);
  163. Delay2ms();
  164. }
  165. }
  166. }
  167. void Display_Back_Time(void) //f 记录采集时间
  168. {
  169. if(Count_chufa==0)
  170. {
  171. Seg(1,27);
  172. Delay2ms();
  173. Seg(2,Count_chufa/10);
  174. Delay2ms();
  175. Seg(3,Count_chufa%10);
  176. Delay2ms();
  177. Seg(4,20);
  178. Delay2ms();
  179. Seg(5,20);
  180. Delay2ms();
  181. Seg(6,20);
  182. Delay2ms();
  183. Seg(7,20);
  184. Delay2ms();
  185. Seg(8,20);
  186. Delay2ms();
  187. }
  188. else{
  189. Seg(1,27);
  190. Delay2ms();
  191. Seg(2,Count_chufa/10);
  192. Delay2ms();
  193. Seg(3,Count_chufa%10);
  194. Delay2ms();
  195. Seg(4,Time_1 / 10);
  196. Delay2ms();
  197. Seg(5,Time_1 % 10);
  198. Delay2ms();
  199. Seg(6,21);
  200. Delay2ms();
  201. Seg(7,Time_2 / 10);
  202. Delay2ms();
  203. Seg(8,Time_2 % 10);
  204. Delay2ms();
  205. }
  206. }
  207. void Temp_Humidity(void)//e 温湿度数据
  208. {
  209. if(Humidity_Value==0)
  210. {
  211. Seg(1,25);
  212. Delay2ms();
  213. Seg(2,20);
  214. Delay2ms();
  215. Seg(3,20);
  216. Delay2ms();
  217. Seg(4,Temp_Value/100000);
  218. Delay2ms();
  219. Seg(5,Temp_Value%100000/10000);
  220. Delay2ms();
  221. Seg(6,21);
  222. Delay2ms();
  223. Seg(7,23);
  224. Delay2ms();
  225. Seg(8,23);
  226. Delay2ms();
  227. }
  228. else{
  229. Seg(1,25);
  230. Delay2ms();
  231. Seg(2,20);
  232. Delay2ms();
  233. Seg(3,20);
  234. Delay2ms();
  235. Seg(4,Temp_Value/100000);
  236. Delay2ms();
  237. Seg(5,Temp_Value%100000/10000);
  238. Delay2ms();
  239. Seg(6,21);
  240. Delay2ms();
  241. Seg(7,Humidity_Value/100);
  242. Delay2ms();
  243. Seg(8,Humidity_Value%100/10);
  244. Delay2ms();
  245. }
  246. }
  247. void All_Display(void)//显示模式切换
  248. {
  249. if(Dis_Temp_Humidity==1)
  250. {
  251. Temp_Humidity();
  252. }
  253. else
  254. {
  255. if(S4_Flag == 0)
  256. {
  257. Display_Time();
  258. }
  259. if(S4_Flag == 1)
  260. {
  261. if(S5_Flag==0)
  262. {
  263. Display_Back_Temp();
  264. }
  265. if(S5_Flag==1)
  266. {
  267. Display_Back_Humidity();
  268. }
  269. if(S5_Flag==2)
  270. {
  271. Display_Back_Time();
  272. }
  273. }
  274. if(S4_Flag == 2)
  275. {
  276. Display_Set_Value();
  277. }
  278. }
  279. }
  280. void Scan_Key(void)//按键扫描
  281. {
  282. P32=0;P33=1;
  283. if(P44==0)
  284. {
  285. Delay5ms();
  286. while(P44==0)
  287. {
  288. All_Display();
  289. }
  290. if(S4_Flag==1)
  291. {
  292. S5_Flag++;
  293. S5_Flag %= 3;
  294. }
  295. } //S5定义为“回显”按键,在记录回显界面下,按下 S5 按键,切换
  296. //温度回显、湿度回显和时间回显三个子界面。S5 按键在时间界面无效。
  297. if(P42==0)
  298. {
  299. Delay5ms();
  300. while(P42==0)
  301. {
  302. All_Display();
  303. Flag_2=0;
  304. }
  305. Flag_2=1;
  306. if(S4_Flag==2)
  307. {
  308. Set_Temp--;
  309. if(Set_Temp==0)
  310. {
  311. Set_Temp=99;
  312. }
  313. }
  314. } //S9定义为“减”按键,参数界面下,按下温度参数值减 1;
  315. //时间回显子界面下,长按 S9 超过 2 秒后松开,清除所有已记录的数据,触
  316. //发次数重置为 0。
  317. P32=1;P33=0;
  318. if(P44==0)
  319. {
  320. Delay5ms();
  321. while(P44==0)
  322. {
  323. All_Display();
  324. }
  325. S4_Flag++;
  326. S4_Flag %= 3;
  327. S5_Flag=0;
  328. } //S4切换时间回显参数界面
  329. if(P42==0)
  330. {
  331. Delay5ms();
  332. while(P42==0)
  333. {
  334. All_Display();
  335. }
  336. if(S4_Flag==2)
  337. {
  338. Set_Temp++;
  339. if(Set_Temp==100)
  340. {
  341. Set_Temp=0;
  342. }
  343. }
  344. } //S8定义为“加”按键,参数界面下,按下温度参数值加 1
  345. }
  346. void Recode_Last(void)//L6_完成数据的记录
  347. {
  348. if(i==0)
  349. {
  350. Last_Temp=Temp_Value;
  351. Last_Humidity=Humidity_Value;
  352. i = 1;
  353. }
  354. else
  355. {
  356. Last_Temp_1=Temp_Value;
  357. Last_Humidity_1=Humidity_Value;
  358. i = 0;
  359. }
  360. }
  361. void Compare_Last(void)//L6_比对数据的结果
  362. {
  363. if(j==0)
  364. {
  365. if(Temp_Value>Last_Temp && Humidity_Value>Last_Humidity)
  366. {
  367. Flag_L6=1;
  368. }
  369. else
  370. {
  371. Flag_L6=0;
  372. }
  373. j = 1;
  374. }
  375. else
  376. {
  377. if(Humidity_Value>Last_Humidity_1 && Temp_Value>Last_Temp_1)
  378. {
  379. Flag_L6=1;
  380. }
  381. else
  382. {
  383. Flag_L6=0;
  384. }
  385. j = 0;
  386. }
  387. }
  388. void Led(void)//LED控制
  389. {
  390. P0=0xff;//先将P0口的灯都熄灭掉,再根据情况点亮灯,刷新速度够快的话,相当于都亮
  391. if(S4_Flag==0)
  392. {
  393. P0 &= 0xfe;//点亮L1
  394. Select_138(4);
  395. }
  396. if(S4_Flag==1)
  397. {
  398. P0=0xff;
  399. P0 &= 0xfd;//点亮L2
  400. Select_138(4);
  401. }
  402. if(S4_Flag==2)
  403. {
  404. P0 &= 0xfb;//点亮L3
  405. Select_138(4);
  406. }
  407. if(L4==1)
  408. {
  409. if(Led_Run==1)
  410. {
  411. P0 &= 0xf7;
  412. Select_138(4);
  413. }
  414. else
  415. {
  416. P0 |= 0x08;
  417. Select_138(4);
  418. }
  419. }
  420. if(Led_L5==1)
  421. {
  422. P0 &= 0xef;//点亮L5
  423. Select_138(4);
  424. }
  425. else
  426. {
  427. P0 |= 0x10;//关闭L5
  428. Select_138(4);
  429. }
  430. if(Flag_L6==1)
  431. {
  432. P0 &= 0xdf;//点亮L6
  433. Select_138(4);
  434. }
  435. else
  436. {
  437. P0 |= 0x20;//关闭L6
  438. Select_138(4);
  439. }
  440. }
  441. void Timer0_Service() interrupt 1
  442. {
  443. count++;
  444. }
  445. void Timer1_Service() interrupt 3
  446. {
  447. TH1 = (65535-50000)/256; //50MS
  448. TL1 = (65535-50000)%256;
  449. count_s++;
  450. if(count_s == 20)
  451. {
  452. dat = count;
  453. count = 0;
  454. count_s = 0;
  455. }//NE555
  456. Get_Time();//获取时间
  457. if(P42==0)//长按两秒清除数据 50MS*40=2S
  458. {
  459. add_1++;
  460. if(add_1>=40)
  461. {
  462. Count_chufa=0;
  463. Flag_2=1;
  464. add_1=0;
  465. }
  466. }
  467. if(Flag_Light==0 && Light<120)//先采集数据,然后置标志位,
  468. {
  469. Flag_Light=1;
  470. if((Temp_Value*0.0001) > Set_Temp){L4=1;}else{L4=0;}
  471. if(Humidity_Value==0){Led_L5=1;}else{Led_L5=0;}//L5控制位
  472. Time_1 = Time[2];//小时
  473. Time_2 = Time[1];//分钟
  474. Count_chufa++;
  475. Recode_Last();
  476. if(Count_chufa>=2){Compare_Last();}//控制L6
  477. Dis_Temp_Humidity=1;//显示界面切换
  478. }
  479. if(Dis_Temp_Humidity==1)
  480. {
  481. Flag_Temp_Humidity++;//开始计时,
  482. if(Flag_Temp_Humidity>=60)//60*20ms=3S
  483. {
  484. Sum_Humidity += Humidity_Value;//记录湿度的总和
  485. Sum_Temp += Temp_Value;//记录温度的总和
  486. Flag_Temp_Humidity=0;//计时清零
  487. Dis_Temp_Humidity=0;//取消计时
  488. S4_Flag=0;//回显界面的数字清零
  489. S5_Flag=0;//显示完全后回到主界面
  490. Flag_Light=0;//等待下一次变化
  491. }
  492. }
  493. Led_Run++;//LED闪烁控制
  494. Led_Run%=2;
  495. Led();//led控制
  496. }
  497. void main()
  498. {
  499. Init_Sys();//系统初始化
  500. Init_Time();//DS1302初始化
  501. Timer_Init();//定时器0、1初始化
  502. while(1)
  503. {
  504. Scan_Key();//按键扫描
  505. All_Display();//数码管显示
  506. Humidity_Value=Get_Shidu()*10;//湿度数据获取 ex:64.8
  507. Temp_Value = Get_Temp()*625;//温度数据获取 ex:123456
  508. Light = AD_Read(0x01);//光敏电阻数据获取
  509. }
  510. }

其他工程文件由于时间问题就不都上传了,有时间我做成云盘文件,需要的请自取,

代码资源:

完整的工程代码如下,需要的自取。

链接:https://pan.baidu.com/s/1VZUozQEtSE9UjWWgJjEIHg?pwd=hpg1 
提取码:hpg1

欢迎大家指出的我的代码问题,在评论区讨论。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号