当前位置:   article > 正文

蓝桥杯单片机第十四届国赛 代码及演示_蓝桥杯单片机十四届国赛代码

蓝桥杯单片机十四届国赛代码

        去年14届的国赛题,个人感觉难度适中。主要的难点在于,S8\S9按键长按清楚数据,以及同时按下时,记录超声波的测量值。 还有一个值得注意的是,在校准值中,引入了负数(这点需要特别注意)。当在-5再减到-10时,数码管的处理是一个小难点。LED灯也不同于往年的考法,结合了二进制的知识点。

        此次试题,本人用了几个巧妙地方法完美实现以上问题。以下为代码实现部分,所有都为本人一人编写,仅供大家学习交流。

        

矩阵键盘部分

  1. #include <STC15F2K60S2.H>
  2. unsigned char Key_read()
  3. {
  4. unsigned char temp=0;
  5. P44=0;P42=1;P35=1;P34=1;
  6. if(P30==0)temp=7;
  7. if(P31==0)temp=6;
  8. if(P32==0)temp=5;
  9. if(P33==0)temp=4;
  10. P44=1;P42=0;P35=1;P34=1;
  11. if(P30==0)temp=11;
  12. if(P31==0)temp=10;
  13. if(P32==0)temp=9;
  14. if(P33==0)temp=8;
  15. P44=1;P42=1;P35=0;P34=1;
  16. if(P30==0)temp=15;
  17. if(P31==0)temp=14;
  18. if(P32==0)temp=13;
  19. if(P33==0)temp=12;
  20. P44=1;P42=1;P35=1;P34=0;
  21. if(P30==0)temp=19;
  22. if(P31==0)temp=18;
  23. if(P32==0)temp=17;
  24. if(P33==0)temp=16;
  25. return temp;
  26. }

数码管部分

  1. #include <STC15F2K60S2.H>
  2. code unsigned char Shu[]=
  3. {
  4. 0xc0,
  5. 0xf9,
  6. 0xa4,
  7. 0xb0,
  8. 0x99,
  9. 0x92,
  10. 0x82,
  11. 0xf8,
  12. 0x80,
  13. 0x90,
  14. 0xff,
  15. 0xbf, //-
  16. 0x8c, //P
  17. 0x8e //F
  18. };
  19. void InitHC138(unsigned char u,num)
  20. {
  21. P0=num;
  22. switch(u)
  23. {
  24. case 4:P2=P2&0x1f|0x80;break;
  25. case 5:P2=P2&0x1f|0xa0;break;
  26. case 6:P2=P2&0x1f|0xc0;break;
  27. case 7:P2=P2&0x1f|0xe0;break;
  28. }
  29. P2=P2&0x1f|0x00;
  30. }
  31. void SMG(unsigned char location,num,point)
  32. {
  33. InitHC138(7,0xff);
  34. InitHC138(6,0x01<<location);
  35. P0=Shu[num];
  36. if(point)
  37. P0&=0x7f;
  38. P2=P2&0x1f|0xe0;
  39. P2=P2&0x1f|0x00;
  40. }

LED、继电器模块

  1. #include <STC15F2K60S2.H>
  2. static unsigned char temp1=0x00;
  3. static unsigned char old1=0xff;
  4. void Led_read(unsigned char addr,able)
  5. {
  6. static unsigned char temp=0x00;
  7. static unsigned char old=0xff;
  8. if(able)
  9. temp|=0x01<<addr;
  10. else
  11. temp&=~(0x01<<addr);
  12. if(temp!=old)
  13. {
  14. P0=~temp;
  15. P2=P2&0x1f|0x80;
  16. P2=P2&0x1f|0x00;
  17. old=temp;
  18. }
  19. }
  20. void Relay(unsigned char able)
  21. {
  22. if(able)
  23. temp1|=0x10;
  24. else
  25. temp1&=~0x10;
  26. if(temp1!=old1)
  27. {
  28. P0=temp1;
  29. P2=P2&0x1f|0xa0;
  30. P2=P2&0x1f|0x00;
  31. old1=temp1;
  32. }
  33. }

温度读取模块(省去onewire给的代码)

  1. float Temperture()
  2. {
  3. unsigned char low,high;
  4. unsigned int temp;
  5. float t;
  6. init_ds18b20();
  7. Write_DS18B20(0xcc);
  8. Write_DS18B20(0x44);
  9. Delay_OneWire(200);
  10. init_ds18b20();
  11. Write_DS18B20(0xcc);
  12. Write_DS18B20(0xbe);
  13. low=Read_DS18B20();
  14. high=Read_DS18B20();
  15. temp=(high<<8)|low;
  16. t=temp*0.0625;
  17. return t;
  18. }

超声波模块

  1. #include <STC15F2K60S2.H>
  2. #include "intrins.h"
  3. sbit TX=P1^0;
  4. sbit RX=P1^1;
  5. void Delay12us(void) //@12.000MHz
  6. {
  7. unsigned char data i;
  8. _nop_();
  9. _nop_();
  10. i = 40;
  11. while (--i);
  12. }
  13. void Wave_Init()
  14. {
  15. unsigned char i;
  16. for(i=0;i<8;i++)
  17. {
  18. TX=1;
  19. Delay12us();
  20. TX=0;
  21. Delay12us();
  22. }
  23. }
  24. unsigned char Wave_get()
  25. {
  26. unsigned int time;
  27. TMOD&=0x0f;
  28. TH1=TL1=0;
  29. Wave_Init();
  30. TR1=1;
  31. while((RX==1) && (TF1==0));
  32. TR1=0;
  33. if(TF1==0)
  34. {
  35. time=(TH1<<8)|TL1;
  36. return time*0.017;
  37. }
  38. else
  39. {
  40. TF1=0;
  41. return 0;
  42. }
  43. }

DA输出模块(省去iic给的代码)

  1. void DA(unsigned char dat)
  2. {
  3. I2CStart();
  4. I2CSendByte(0x90);
  5. I2CWaitAck();
  6. I2CSendByte(0x43);
  7. I2CWaitAck();
  8. I2CSendByte(dat);
  9. I2CWaitAck();
  10. I2CStop();
  11. }

主函数

  1. #include <STC15F2K60S2.H>
  2. #include "intrins.h"
  3. #include "Key.h"
  4. #include "Seg.h"
  5. #include "Led.h"
  6. #include "onewire.h"
  7. #include "Wave.h"
  8. #include "iic.h"
  9. unsigned char Key_time;
  10. unsigned int Seg_time;
  11. unsigned char Key_value,Key_dowm,Key_up,Key_old;
  12. unsigned char Seg_buf[]={10,10,10,10,10,10,10,10,10,10};
  13. unsigned char Seg_point[]={0,0,0,0,0,0,0,0};
  14. unsigned char Seg_pos;
  15. unsigned char Led_buf[]={0,0,0,0,0,0,0,0};
  16. float T;
  17. int L;
  18. unsigned char Mode;
  19. unsigned char Show_L=40; //¾àÀë²ÎÊý
  20. unsigned char Show_T=30; //ζȲÎÊý
  21. unsigned char Mode_can; //²ÎÊýģʽ1/2
  22. unsigned char Mode_can3; //²ÎÊýģʽ3
  23. unsigned char Change_can; //У׼ֵ
  24. unsigned char Fu; //¸ºÊýУ׼ֵ
  25. int Speed=340; //´«ÊäËÙÂÊ
  26. unsigned char V=10; //µçѹ
  27. float V_use;
  28. unsigned char HEX[8]={128,64,32,16,8,4,2,1};
  29. unsigned char Ad_num;
  30. unsigned char Time_100ms;
  31. unsigned char State; //µÆ±ä»»ÁÁÃð
  32. void Key_get()
  33. {
  34. if(Key_time)return;
  35. Key_time=1;
  36. Key_value=Key_read();
  37. Key_dowm=Key_value&(Key_value^Key_old);
  38. Key_up=~Key_value&(Key_value^Key_old);
  39. Key_old=Key_value;
  40. switch(Key_dowm)
  41. {
  42. case 4:
  43. if(++Mode==3)Mode=0;
  44. Mode_can=0;
  45. Mode_can3=0;
  46. break;
  47. case 5:
  48. if(Mode==0 || Mode==1)Mode_can^=1;
  49. if(Mode==2)
  50. {
  51. if(++Mode_can3==3)Mode_can3=0;
  52. }
  53. break;
  54. case 8: //¼Ó
  55. if(Mode==1)
  56. {
  57. if(Mode_can==0)
  58. {
  59. Show_L+=10;
  60. if(Show_L==100)Show_L=90;
  61. }
  62. else
  63. {
  64. if(++Show_T==81)Show_T=80;
  65. }
  66. }
  67. else if(Mode==2)
  68. {
  69. if(Mode_can3==0)
  70. {
  71. if(Fu==0 && Change_can>=0)
  72. {
  73. Change_can+=5;
  74. if(Change_can==95)Change_can=90;
  75. }
  76. if(Fu>0)
  77. Fu-=5;
  78. }
  79. else if(Mode_can3==1)
  80. {
  81. Speed+=10;
  82. if(Speed==9995)Change_can=9990;
  83. }
  84. else
  85. {
  86. if(++V==21)V=20;
  87. }
  88. }
  89. break;
  90. case 9:
  91. if(Mode==1) //²ÎÊý¼õ
  92. {
  93. if(Mode_can==0)
  94. {
  95. Show_L-=10;
  96. if(Show_L==0)Show_L=10;
  97. }
  98. else
  99. {
  100. if(--Show_T==255)Show_T=0;
  101. }
  102. }
  103. else if(Mode==2) //¹¤³§Ä£Ê½
  104. {
  105. if(Mode_can3==0)
  106. {
  107. if(Change_can==0 && Fu>=0)
  108. {
  109. Fu+=5;
  110. if(Fu==95)Fu=90;
  111. }
  112. if(Change_can>0)
  113. Change_can-=5;
  114. }
  115. else if(Mode_can3==1)
  116. {
  117. Speed-=10;
  118. if(Speed==0)Change_can=10;
  119. }
  120. else
  121. {
  122. if(--V==0)V=1;
  123. }
  124. }
  125. break;
  126. }
  127. }
  128. void Seg_get()
  129. {
  130. if(Seg_time)return;
  131. Seg_time=1;
  132. T=Temperture();
  133. L=(Speed/340)*Wave_get()+(Change_can>Fu?Change_can:-Fu);
  134. switch(Mode)
  135. {
  136. case 0:
  137. Seg_buf[0]=T/10;
  138. Seg_buf[1]=(unsigned char)T%10;
  139. Seg_buf[2]=(unsigned int)(T*10)%10;
  140. Seg_buf[3]=11;
  141. Seg_point[1]=1;
  142. Seg_point[6]=0;
  143. if(Mode_can==0)
  144. {
  145. Seg_buf[5]=L>99?L/100%10:10;
  146. Seg_buf[6]=L/10%10;
  147. Seg_buf[7]=L%10;
  148. Seg_point[5]=0;
  149. }
  150. else
  151. {
  152. Seg_buf[5]=L>99?L/100%10:0;
  153. Seg_buf[6]=L/10%10;
  154. Seg_buf[7]=L%10;
  155. Seg_point[5]=1;
  156. }
  157. break;
  158. case 1:
  159. Seg_buf[0]=12;
  160. Seg_buf[2]=10;
  161. Seg_buf[3]=10;
  162. Seg_buf[5]=10;
  163. Seg_point[1]=0;
  164. Seg_point[5]=0;
  165. if(Mode_can==0)
  166. {
  167. Seg_buf[1]=1;
  168. Seg_buf[6]=Show_L/10;
  169. Seg_buf[7]=Show_L%10;
  170. }
  171. else
  172. {
  173. Seg_buf[1]=2;
  174. Seg_buf[6]=Show_T/10;
  175. Seg_buf[7]=Show_T%10;
  176. }
  177. break;
  178. case 2:
  179. Seg_buf[0]=13;
  180. if(Mode_can3==0)
  181. {
  182. Seg_buf[1]=1;
  183. Seg_point[6]=0;
  184. if(Change_can>0 || Change_can==Fu)
  185. {
  186. Seg_buf[5]=10;
  187. Seg_buf[6]=Change_can<10?10:Change_can/10;
  188. Seg_buf[7]=Change_can%10;
  189. }
  190. if(Fu>0)
  191. {
  192. Seg_buf[5]=Fu>=10?11:10;
  193. Seg_buf[6]=Fu<10?11:Fu/10;
  194. Seg_buf[7]=Fu%10;
  195. }
  196. }
  197. else if(Mode_can3==1)
  198. {
  199. Seg_buf[1]=2;
  200. Seg_buf[4]=Speed>999?Speed/1000%10:10;
  201. Seg_buf[5]=Speed>99?Speed/100%10:10;
  202. Seg_buf[6]=Speed/10%10;
  203. Seg_buf[7]=Speed%10;
  204. }
  205. else
  206. {
  207. Seg_buf[1]=3;
  208. Seg_buf[4]=10;
  209. Seg_buf[5]=10;
  210. Seg_point[6]=1;
  211. if(V>=10)
  212. {
  213. Seg_buf[6]=V/10;
  214. Seg_buf[7]=V%10;
  215. }
  216. else
  217. {
  218. Seg_buf[6]=0;
  219. Seg_buf[7]=V%10;
  220. }
  221. }
  222. break;
  223. }
  224. }
  225. void Led_get()
  226. {
  227. unsigned char i;
  228. int temp=L;
  229. if(Mode==1)
  230. {
  231. for(i=0;i<7;i++) //ϨÃðÆäÓà7ÕµµÆ£¬ÁôL8
  232. Led_buf[i]=0;
  233. Led_buf[7]=1;
  234. }
  235. else if(Mode==2)
  236. {
  237. for(i=1;i<8;i++)
  238. Led_buf[i]=0;
  239. Led_buf[0]=State;
  240. }
  241. else
  242. {
  243. for(i=0;i<8;i++)
  244. {
  245. if(temp>HEX[i])
  246. {
  247. Led_buf[7-i]=1;
  248. temp-=HEX[i];
  249. }
  250. else if(temp==HEX[i]) //¸ÕºÃµÈÓÚ1¡¢2¡¢4¡¢8¡¢16...
  251. {
  252. Led_buf[7-i]=1;
  253. temp=0;
  254. }
  255. else
  256. Led_buf[7-i]=0;
  257. }
  258. }
  259. V_use=V/10;
  260. if(L>=90)Ad_num=5;
  261. if(L<=10)Ad_num=V_use;
  262. if(L>10 && L<90)Ad_num=((5-V_use)/80)*L+(9*V_use-5)/8;
  263. DA(Ad_num*51);
  264. if((Show_L-5<=L && Show_L+5>=L) && T<=Show_T)
  265. Relay(1);
  266. else
  267. Relay(0);
  268. }
  269. void Timer0_Init()
  270. {
  271. AUXR &= 0x7F;
  272. TMOD &= 0xF0;
  273. TL0 = 0x18;
  274. TH0 = 0xFC;
  275. TF0 = 0;
  276. TR0 = 1;
  277. EA=1;
  278. ET0=1;
  279. }
  280. void Delay750ms(void)
  281. {
  282. unsigned char data i, j, k;
  283. _nop_();
  284. _nop_();
  285. i = 35;
  286. j = 51;
  287. k = 182;
  288. do
  289. {
  290. do
  291. {
  292. while (--k);
  293. } while (--j);
  294. } while (--i);
  295. }
  296. void main()
  297. {
  298. InitHC138(4,0xff);
  299. InitHC138(5,0x00);
  300. Timer0_Init();
  301. Temperture();
  302. Delay750ms();
  303. while(1)
  304. {
  305. Key_get();
  306. Seg_get();
  307. Led_get();
  308. }
  309. }
  310. void Init_T0() interrupt 1
  311. {
  312. if(++Key_time==10)Key_time=0;
  313. if(++Seg_time==200)Seg_time=0;
  314. if(++Seg_pos==8)Seg_pos=0;
  315. SMG(Seg_pos,Seg_buf[Seg_pos],Seg_point[Seg_pos]);
  316. Led_read(Seg_pos,Led_buf[Seg_pos]);
  317. if(++Time_100ms==100)
  318. {
  319. Time_100ms=0;
  320. State^=1;
  321. }
  322. }

十四届国赛

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/642866
推荐阅读
相关标签
  

闽ICP备14008679号