当前位置:   article > 正文

第十三届蓝桥杯单片机完整程序_蓝桥杯单片机的手册

蓝桥杯单片机的手册

声明:1、我发布程序旨在希望大家蓝桥杯单片机比赛可以取得好成绩

           2、在看我程序之前希望大家先去B站观看完小蜜蜂老师的蓝桥杯单片机视频讲解

小蜜蜂视频链接:【小蜜蜂笔记】蓝桥杯大赛-单片机设计与开发基础技能与进阶强化教程_哔哩哔哩_bilibili

本程序资源包采用第十三届蓝桥杯资源包 

博客包含第八届第十三届蓝桥杯单片机真题所有程序

main.c 

  1. #include <reg52.h>
  2. #include <ds1302.h>
  3. #include <onewire.h>
  4. sfr P4 = 0xC0;
  5. sbit L1 = P0^0;
  6. sbit L2 = P0^1;
  7. sbit L3 = P0^2;
  8. sbit R1 = P3^0;
  9. sbit R2 = P3^1;
  10. sbit R3 = P3^2;
  11. sbit R4 = P3^3;
  12. sbit C1 = P3^4;
  13. sbit C2 = P3^5;
  14. sbit C3 = P4^2;
  15. sbit C4 = P4^4;
  16. sbit Relay = P0^4;
  17. sbit Buzzer = P0^6;
  18. //变量区
  19. unsigned int count;//继电器5s
  20. unsigned char count1;//0.1s
  21. unsigned int count2;//LED5s
  22. unsigned char state_5 = 0;//0.1s标志位
  23. unsigned char state_4 = 0;//LED5s标志位
  24. unsigned char state_3 = 0;//继电器标志位
  25. unsigned char state_2 = 1;//时间控制
  26. unsigned char mode = 1;//模式控制
  27. unsigned char state_1 = 1;//界面控制
  28. unsigned char number = 23;//参数
  29. unsigned int temp;//温度
  30. unsigned char Write_DS1302_Addr[3] = {0x80,0x82,0x84};
  31. unsigned char Read_DS1302_Addr[3] = {0x81,0x83,0x85};
  32. unsigned char Timer[3] = {0x50,0x59,0x22};//22:59:50
  33. unsigned char SMG_Val[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
  34. //锁存器的选择
  35. void SelectHC573(unsigned char channel)
  36. {
  37. switch(channel)
  38. {
  39. case 4 : P2 = (P2 & 0x1f) | 0x80; break;
  40. case 5 : P2 = (P2 & 0x1f) | 0xa0; break;
  41. case 6 : P2 = (P2 & 0x1f) | 0xc0; break;
  42. case 7 : P2 = (P2 & 0x1f) | 0xe0; break;
  43. }
  44. }
  45. //数码管的选择(无点)
  46. void Select_SMG(unsigned char pos,val)
  47. {
  48. SelectHC573(7);
  49. P0 = 0xff;
  50. SelectHC573(6);
  51. P0 = 0x01 << pos;
  52. SelectHC573(7);
  53. P0 = val;
  54. }
  55. //数码管的选择(有点)
  56. void Select_DotSMG(unsigned char pos,val)
  57. {
  58. SelectHC573(7);
  59. P0 = 0xff;
  60. SelectHC573(6);
  61. P0 = 0x01 << pos;
  62. SelectHC573(7);
  63. P0 = val & 0x7f;
  64. }
  65. //关闭所有
  66. void CloseAll()
  67. {
  68. SelectHC573(6);
  69. P0 = 0xff;
  70. SelectHC573(7);
  71. P0 = 0xff;
  72. }
  73. //DS1302初始化
  74. void Init_DS1302()
  75. {
  76. unsigned char i;
  77. Write_Ds1302_Byte(0x8e,0x00);
  78. for(i = 0;i < 3;i ++)
  79. {
  80. Write_Ds1302_Byte(Write_DS1302_Addr[i],Timer[i]);
  81. }
  82. Write_Ds1302_Byte(0x8e,0x80);
  83. }
  84. //读取DS1302时间
  85. void Read_DS1302()
  86. {
  87. unsigned char i;
  88. for(i = 0;i < 3;i ++)
  89. {
  90. Timer[i] = Read_Ds1302_Byte(Read_DS1302_Addr[i]);
  91. }
  92. }
  93. //定时器0初始化
  94. void Init_Timer0()
  95. {
  96. TMOD = 0x01;
  97. TH0 = (65536 - 50000) / 256;
  98. TL0 = (65536 - 50000) % 256;
  99. EA = 1;
  100. ET0 = 1;
  101. // TR0 = 1;
  102. }
  103. //定时器0服务函数
  104. void Service_T0() interrupt 1
  105. {
  106. TH0 = (65536 - 50000) / 256;
  107. TL0 = (65536 - 50000) % 256;
  108. count ++;
  109. count2 ++;
  110. if(count == 100)
  111. {
  112. state_3 = 0;
  113. count = 0;
  114. }
  115. if(count2 == 100)
  116. {
  117. state_4 = 0;
  118. count2 = 0;
  119. }
  120. }
  121. //定时器1初始化
  122. void Init_Timer1()
  123. {
  124. TMOD = 0x01;
  125. TH1 = (65536 - 50000) / 256;
  126. TL1 = (65536 - 50000) % 256;
  127. EA = 1;
  128. ET1 = 1;
  129. // TR1 = 1;
  130. }
  131. //定时器1服务函数
  132. void Service_T1() interrupt 3
  133. {
  134. TH1 = (65536 - 50000) / 256;
  135. TL1 = (65536 - 50000) % 256;
  136. count1 ++;
  137. if(count1 == 2)
  138. {
  139. state_5 = !state_5;
  140. count1 = 0;
  141. }
  142. }
  143. //延迟函数
  144. void Delay(unsigned int t)
  145. {
  146. while(t --);
  147. }
  148. //DS1302界面
  149. void Display_DS1302()
  150. {
  151. Select_SMG(0,0xc1);
  152. Delay(100);
  153. Select_SMG(1,SMG_Val[2]);
  154. Delay(100);
  155. if(state_2 == 1)
  156. {
  157. Select_SMG(3,SMG_Val[Timer[2] / 16]);
  158. Delay(100);
  159. Select_SMG(4,SMG_Val[Timer[2] % 16]);
  160. Delay(100);
  161. Select_SMG(5,0xbf);
  162. Delay(100);
  163. Select_SMG(6,SMG_Val[Timer[1] / 16]);
  164. Delay(100);
  165. Select_SMG(7,SMG_Val[Timer[1] % 16]);
  166. Delay(100);
  167. }
  168. else if(state_2 == 2)
  169. {
  170. Select_SMG(3,SMG_Val[Timer[1] / 16]);
  171. Delay(100);
  172. Select_SMG(4,SMG_Val[Timer[1] % 16]);
  173. Delay(100);
  174. Select_SMG(5,0xbf);
  175. Delay(100);
  176. Select_SMG(6,SMG_Val[Timer[0] / 16]);
  177. Delay(100);
  178. Select_SMG(7,SMG_Val[Timer[0] % 16]);
  179. Delay(100);
  180. }
  181. CloseAll();
  182. }
  183. //DS18B20界面//123
  184. void Display_DS18B20()
  185. {
  186. temp = DS18B20_Temp();
  187. Select_SMG(7,SMG_Val[temp % 10]);
  188. Delay(500);
  189. Select_DotSMG(6,SMG_Val[temp % 100 / 10]);
  190. Delay(500);
  191. Select_SMG(5,SMG_Val[temp / 100]);
  192. Delay(500);
  193. Select_SMG(1,SMG_Val[1]);
  194. Delay(500);
  195. Select_SMG(0,0xc1);
  196. Delay(500);
  197. CloseAll();
  198. }
  199. //参数界面
  200. void Display_CS()
  201. {
  202. Select_SMG(7,SMG_Val[number % 10]);
  203. Delay(100);
  204. Select_SMG(6,SMG_Val[number / 10]);
  205. Delay(100);
  206. Select_SMG(1,SMG_Val[3]);
  207. Delay(100);
  208. Select_SMG(0,0xc1);
  209. Delay(100);
  210. CloseAll();
  211. }
  212. //所有界面
  213. void Display()
  214. {
  215. switch(state_1)
  216. {
  217. case 1 : Display_DS18B20(); break;
  218. case 2 : Display_DS1302(); break;
  219. case 3 : Display_CS(); break;
  220. }
  221. }
  222. void Control_mode();
  223. void LED_Control();
  224. //按键控制
  225. void KEY_KBO()
  226. {
  227. R4 = 0;R1 = R2 = R3 = 1; //S12
  228. C1 = C2 = C3 = C4 = 1;
  229. if(C2 == 0)
  230. {
  231. Delay(200);
  232. if(C2 == 0)
  233. {
  234. if(state_1 == 1)
  235. {
  236. state_1 = 2;
  237. }
  238. else if(state_1 == 2)
  239. {
  240. state_1 = 3;
  241. }
  242. else if(state_1 == 3)
  243. {
  244. state_1 = 1;
  245. }
  246. }
  247. while(C2 == 0)
  248. {
  249. Display();
  250. LED_Control();
  251. }
  252. }
  253. R3 = 0;R1 = R2 = R4 = 1; //S13
  254. C1 = C2 = C3 = C4 = 1;
  255. if(C2 == 0)
  256. {
  257. Delay(200);
  258. if(C2 == 0)
  259. {
  260. if(mode == 1)
  261. {
  262. mode = 2;
  263. }
  264. else if(mode ==2)
  265. {
  266. mode = 1;
  267. }
  268. }
  269. while(C2 == 0)
  270. {
  271. Display();
  272. LED_Control();
  273. }
  274. }
  275. R4 = 0;R1 = R2 = R3 = 1; //S16
  276. C1 = C2 = C3 = C4 = 1;
  277. if(C1 == 0)
  278. {
  279. Delay(200);
  280. if(C1 == 0)
  281. {
  282. if(state_1 == 3)
  283. {
  284. number ++;
  285. if(number > 99)
  286. {
  287. number = 10;
  288. }
  289. }
  290. }
  291. while(C1 == 0)
  292. {
  293. Display();
  294. LED_Control();
  295. }
  296. }
  297. R3 = 0;R1 = R2 = R4 = 1; //S17
  298. C1 = C2 = C3 = C4 = 1;
  299. if(C1 == 0)
  300. {
  301. Delay(200);
  302. if(C1 == 0)
  303. {
  304. if(state_1 == 2)
  305. {
  306. while(C1 == 0)
  307. {
  308. state_2 = 2;
  309. Display();
  310. // Control_mode();
  311. LED_Control();
  312. Read_DS1302();
  313. }
  314. state_2 = 1;
  315. }
  316. else if(state_1 == 3)
  317. {
  318. number --;
  319. if(number < 10)
  320. {
  321. number = 99;
  322. }
  323. }
  324. while(C1 == 0)
  325. {
  326. Display();
  327. LED_Control();
  328. }
  329. }
  330. }
  331. }
  332. //控制模块
  333. void Control_mode()
  334. {
  335. if(mode == 1) //温度
  336. {
  337. if(temp > number * 10)
  338. {
  339. SelectHC573(5);
  340. Relay = 1;
  341. Buzzer = 0;
  342. state_3 = 1;
  343. }
  344. else
  345. {
  346. SelectHC573(5);
  347. Relay = 0;
  348. Buzzer = 0;
  349. state_3 = 0;
  350. }
  351. }
  352. if(mode == 2) //时间
  353. {
  354. if(Timer[1] == 0x00 && Timer[0] == 0x00)
  355. {
  356. SelectHC573(5);
  357. Relay = 1;
  358. Buzzer = 0;
  359. state_3 = 1;
  360. TR0 = 1;
  361. }
  362. else if(state_3 == 0)
  363. {
  364. SelectHC573(5);
  365. Relay = 0;
  366. Buzzer = 0;
  367. TR0 = 0;
  368. }
  369. }
  370. }
  371. //LED
  372. void LED_Control()
  373. {
  374. SelectHC573(4);
  375. if(Timer[1] == 0x00 && Timer[0] == 0x00)
  376. {
  377. state_4 = 1;
  378. TR0 = 1;
  379. }
  380. else if(state_4 == 0)
  381. {
  382. TR0 = 0;
  383. }
  384. if(state_4 == 1)
  385. {
  386. L1 = 0;
  387. }
  388. else if(state_4 == 0)
  389. {
  390. L1 = 1;
  391. }
  392. if(mode == 1)
  393. {
  394. L2 = 0;
  395. }
  396. else if(mode == 2)
  397. {
  398. L2 = 1;
  399. }
  400. if(state_3 == 1)
  401. {
  402. TR1 = 1;
  403. if(state_5 == 1)
  404. {
  405. L3 = 0;
  406. }
  407. else if(state_5 == 0)
  408. {
  409. L3 = 1;
  410. }
  411. }
  412. else if(state_3 == 0)
  413. {
  414. TR1 = 0;
  415. }
  416. }
  417. //系统初始化
  418. void InitSystem()
  419. {
  420. SelectHC573(5);
  421. P0 = 0x00;
  422. SelectHC573(4);
  423. P0 = 0xff;
  424. }
  425. //主函数
  426. void main()
  427. {
  428. Init_Timer1();
  429. Init_Timer0();
  430. Init_DS1302();
  431. InitSystem();
  432. while(1)
  433. {
  434. LED_Control();
  435. Control_mode();
  436. Read_DS1302();
  437. Display();
  438. KEY_KBO();
  439. }
  440. }

DS1302.c

  1. #include "ds1302.h"
  2. //写字节
  3. void Write_Ds1302(unsigned char temp)
  4. {
  5. unsigned char i;
  6. for (i=0;i<8;i++)
  7. {
  8. SCK = 0;
  9. SDA = temp&0x01;
  10. temp>>=1;
  11. SCK=1;
  12. }
  13. }
  14. //向DS1302寄存器写入数据
  15. void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
  16. {
  17. RST=0; _nop_();
  18. SCK=0; _nop_();
  19. RST=1; _nop_();
  20. Write_Ds1302(address);
  21. Write_Ds1302(dat);
  22. RST=0;
  23. }
  24. //从DS1302寄存器读出数据
  25. unsigned char Read_Ds1302_Byte ( unsigned char address )
  26. {
  27. unsigned char i,temp=0x00;
  28. RST=0; _nop_();
  29. SCK=0; _nop_();
  30. RST=1; _nop_();
  31. Write_Ds1302(address);
  32. for (i=0;i<8;i++)
  33. {
  34. SCK=0;
  35. temp>>=1;
  36. if(SDA)
  37. temp|=0x80;
  38. SCK=1;
  39. }
  40. RST=0; _nop_();
  41. SCK=0; _nop_();
  42. SCK=1; _nop_();
  43. SDA=0; _nop_();
  44. SDA=1; _nop_();
  45. return (temp);
  46. }

OneWire.c

  1. #include "onewire.h"
  2. //单总线内部延时函数
  3. void Delay_OneWire(unsigned int t)
  4. {
  5. while(t--);
  6. }
  7. //单总线写操作
  8. void Write_DS18B20(unsigned char dat)
  9. {
  10. unsigned char i;
  11. for(i=0;i<8;i++)
  12. {
  13. DQ = 0;
  14. DQ = dat&0x01;
  15. Delay_OneWire(50);
  16. DQ = 1;
  17. dat >>= 1;
  18. }
  19. Delay_OneWire(50);
  20. }
  21. //单总线读操作
  22. unsigned char Read_DS18B20(void)
  23. {
  24. unsigned char i;
  25. unsigned char dat;
  26. for(i=0;i<8;i++)
  27. {
  28. DQ = 0;
  29. dat >>= 1;
  30. DQ = 1;
  31. if(DQ)
  32. {
  33. dat |= 0x80;
  34. }
  35. Delay_OneWire(50);
  36. }
  37. return dat;
  38. }
  39. //DS18B20初始化
  40. bit init_ds18b20(void)
  41. {
  42. bit initflag = 0;
  43. DQ = 1;
  44. Delay_OneWire(120);
  45. DQ = 0;
  46. Delay_OneWire(800);
  47. DQ = 1;
  48. Delay_OneWire(100);
  49. initflag = DQ;
  50. Delay_OneWire(50);
  51. return initflag;
  52. }
  53. unsigned int DS18B20_Temp()
  54. {
  55. unsigned char LSB,MSB;
  56. unsigned int temp;
  57. init_ds18b20();
  58. Write_DS18B20(0xcc);
  59. Write_DS18B20(0x44);
  60. Delay_OneWire(50);
  61. init_ds18b20();
  62. Write_DS18B20(0xcc);
  63. Write_DS18B20(0xbe);
  64. LSB = Read_DS18B20();
  65. MSB = Read_DS18B20();
  66. temp = MSB << 8;
  67. temp = temp | LSB;
  68. temp = temp * 0.625;
  69. return temp;
  70. }

DS1302.h

  1. #ifndef __DS1302_H
  2. #define __DS1302_H
  3. #include <reg52.h>
  4. #include <intrins.h>
  5. sbit SCK = P1^7;
  6. sbit SDA = P2^3;
  7. sbit RST = P1^3;
  8. void Write_Ds1302(unsigned char temp);
  9. void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
  10. unsigned char Read_Ds1302_Byte( unsigned char address );
  11. #endif

OneWire.h

  1. #ifndef __ONEWIRE_H
  2. #define __ONEWIRE_H
  3. #include "reg52.h"
  4. sbit DQ = P1^4;
  5. unsigned int DS18B20_Temp();
  6. #endif

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

闽ICP备14008679号