首先这届国赛题也比较一般也常规 led 数码管 温度 光敏 矩阵键盘 都是我们熟悉的模块 然后就是拼接起来不出bug 下面是我个人代码 ,仅供参考 -------
首先main函数
1. #include "dmx.h" 2. #include "onewire.h" 3. #include "iic.h" 4. void main() 5. { 6. 7. Init_System(); 8. while(1) 9. { 10. if(count>=300) 11. { 12. temp= Read_temp(); 13. count=0; 14. 15. 16. } 17. 18. if(count==50) 19. { 20. v=adc_read(0x01)*100.0/51; 21. 22. } 23. 24. 25. 26. scan_kbd(); 27. key_real(); 28. Read_time(); 29. SMG_show(); 30. led_work(); 31. 32. 33. } 34. 35. 36. }
下面是配置文件也比较关键
1. #include <STC15F2K60S2.H> 2. #include "dmx.h" 3. #include "onewire.h" 4. #include "ds1302.h" 5. unsigned char mod1=0; 6. bit mod2=0; 7. unsigned int temp_flag=0; 8. unsigned int count=0; 9. unsigned int temp=0; 10. unsigned char mod3=0; 11. unsigned int count_L3=0; 12. unsigned int count_L31=0; 13. unsigned int para_temp=25; 14. unsigned char para_time=17; 15. unsigned char para_led=4; 16. unsigned char para_mod1=0; 17. unsigned int v=200; 18. bit flag_L3=0; 19. unsigned char stat_led=0xff; 20. unsigned int key_num=20; //°´¼üÈ¡Ïû 21. unsigned char Read_ds1302[3]={0}; 22. unsigned char segbuff[8]={10,10,10,10,10,11,0,1}; 23. unsigned char segtab[]={0xc0, //0 24. 0xf9, //1 25. 0xa4, //2 26. 0xb0, //3 27. 0x99, //4 28. 0x92, //5 29. 0x82, //6 30. 0xf8, //7 31. 0x80, //8 32. 0x90, //9 33. 0xff, //ϨÃð 34. 0xbf, //- 35. 0x88, //A 36. 0x83, //b 37. 0xc6, //C 38. 0xa1, //d 39. 0x86, //E 40. 0x8e, //F 41. 0x8c, //18 42. 0xc0&0x7f, 43. 0xf9&0x7f, //1 44. 0xa4&0x7f, //2 45. 0xb0&0x7f, //3 46. 0x99&0x7f, //4 47. 0x92&0x7f, //5 48. 0x82&0x7f, //6 49. 0xf8&0x7f, //7 50. 0x80&0x7f, //8 51. 0x90&0x7f //9 52. }; 53. 54. void time0() 55. { 56. TMOD=0x10; 57. EA=1; 58. TH1=(0-1000)/256; 59. TL1=(0-1000)%256; 60. TR1=1; 61. ET1=1; 62. } 63. void Timer1() interrupt 3 64. { 65. 66. TH1=(0-1000)/256; 67. TL1=(0-1000)%256; 68. count++; 69. if(flag_L3) 70. { 71. if(++count_L3==3000) 72. { 73. stat_led&=0xfb; 74. count_L3=0; 75. } 76. } 77. else 78. { 79. 80. if(++count_L31==3000) 81. { 82. stat_led|=0x04; 83. count_L31=0; 84. } 85. 86. 87. } 88. 89. segs(); 90. 91. 92. 93. 94. 95. 96. } 97. 98. 99. void led_work() 100. { 101. unsigned int t= Read_ds1302[0]/16*10 + Read_ds1302[0]%16; 102. 103. if( t>=8 && t<= para_time) 104. { 105. stat_led |=0x01; 106. 107. 108. } 109. else stat_led&=0xfe; 110. 111. if(temp/10<para_temp) 112. { 113. stat_led &=0xfd; 114. 115. } 116. else stat_led|=0x02; 117. 118. if(v/100<1) 119. { 120. flag_L3=1; 121. if(para_led==4)stat_led=(stat_led|0xf8)&0xf7; 122. else if(para_led==5)stat_led=(stat_led|0xf8)&0xef; 123. else if(para_led==6)stat_led=(stat_led|0xf8)&0xdf; 124. else if(para_led==7)stat_led=(stat_led|0xf8)&0xbf; 125. else if(para_led==8)stat_led=(stat_led|0xf8)&0x7f; 126. 127. } 128. 129. else 130. { 131. flag_L3=0; 132. stat_led|=0xf8; 133. 134. } 135. P2=P2&0x1f|0x00; 136. P0=0xff; 137. P2=P2&0x1f|0x80; 138. P0=stat_led; 139. 140. 141. } 142. void scan_kbd() 143. { 144. static char kbd_state = 0; 145. unsigned char kbd_press; 146. char row; 147. switch(kbd_state){ 148. 149. case state_0: 150. kbd_io=0x0f;P42=0;P44=0; 151. kbd_press=kbd_io; 152. if(kbd_press != kbd_maskrow) 153. kbd_state=state_1; 154. break; 155. 156. case state_1: 157. kbd_press=kbd_io; 158. if(kbd_press!=kbd_maskrow) 159. { 160. if((kbd_io&0x08)==0) row=0; 161. if((kbd_io&0x04)==0) row=1; 162. if((kbd_io&0x02)==0) row=2; 163. if((kbd_io&0x01)==0) row=3; 164. 165. 166. kbd_io=0xf0;P42=1;P44=1; 167. if(P44==0)key_num=row; 168. if(P42==0)key_num=row+4; 169. if((kbd_io&0x20)==0)key_num=row+8; 170. if((kbd_io&0x10)==0)key_num=row+12; 171. kbd_state=state_2; 172. 173. } 174. else { 175. kbd_state=state_0; 176. } 177. break; 178. 179. case state_2: 180. kbd_io=0x0f; P42=0; P44=0; 181. kbd_press=kbd_io; 182. if(kbd_press==0x0f)kbd_state=state_0; 183. break; 184. 185. default : 186. break; 187. } 188. 189. } 190. 191. void key_real() 192. { 193. 194. switch(key_num) 195. { 196. case 0: 197. mod2=~mod2; 198. break; 199. case 1: 200. if(!mod2) 201. { 202. if(++mod1==3)mod1=0; 203. } 204. 205. else 206. { 207. if(++para_mod1==3)para_mod1=0; 208. } 209. break; 210. case 4: 211. if(para_mod1==0) 212. { 213. if(para_time>=23)para_time=0; 214. else para_time+=1; 215. 216. } 217. else if(para_mod1==1) 218. { 219. if(para_temp>=99)para_temp=0; 220. para_temp+=1; 221. } 222. else if(para_mod1==2) 223. { 224. if(para_led>=8)para_led=4; 225. else para_led+=1; 226. } 227. 228. 229. break; 230. 231. case 5: 232. if(para_mod1==0) 233. { 234. if(para_time<=0)para_time=23; 235. else para_time-=1; 236. 237. } 238. else if(para_mod1==1) 239. { 240. if(para_temp<=0)para_temp=99; 241. else para_temp-=1; 242. } 243. else if(para_mod1==2) 244. { 245. if(para_led<=4)para_led=8; 246. else para_led-=1; 247. } 248. 249. 250. break; 251. 252. default :break; 253. 254. 255. } 256. 257. key_num=20; 258. 259. 260. 261. } 262. void Slect_fun(unsigned char n) 263. { 264. 265. P2=P2&0x1f; 266. 267. switch(n) 268. { 269. case 0:P2|=0x00; break; 270. case 4:P2|=0x80; break; 271. case 5:P2|=0xa0; break; 272. case 6:P2|=0xc0; break; 273. case 7:P2|=0xe0; break; 274. default : break; 275. 276. 277. 278. } 279. 280. } 281. void Init_System() 282. { 283. unsigned char i; 284. Slect_fun(4);P0=0xff; 285. Slect_fun(5);P0=0x00; 286. for(i=0;i<3;i++) 287. { 288. Read_temp(); 289. 290. } 291. time0(); 292. Init_ds1302(); 293. 294. } 295. void segs() 296. { 297. static unsigned char wei=0; 298. Slect_fun(7); 299. P0=0xff; 300. 301. Slect_fun(6); 302. P0=1<<wei; 303. 304. Slect_fun(7); 305. P0=segtab[segbuff[wei]]; 306. wei++; 307. if(wei==8)wei=0; 308. 309. Slect_fun(0); 310. 311. } 312. void Init_ds1302() 313. { 314. 315. 316. Write_Ds1302_Byte(0x8e,0x00); 317. Write_Ds1302_Byte(0x84,0x16); 318. Write_Ds1302_Byte(0x82,0x59); 319. Write_Ds1302_Byte(0x80,0x50); 320. Write_Ds1302_Byte(0x8e,0x80); 321. 322. 323. } 324. void Read_time() 325. { 326. 327. Read_ds1302[0]=Read_Ds1302_Byte(0x85); 328. Read_ds1302[1]=Read_Ds1302_Byte(0x83); 329. Read_ds1302[2]=Read_Ds1302_Byte(0x81); 330. 331. 332. } 333. void SMG_show() 334. { 335. if(mod2==0) 336. { 337. if(mod1==0) 338. { 339. segbuff[0]=Read_ds1302[0]>>4; 340. segbuff[1]=Read_ds1302[0]%16; 341. segbuff[2]=11; 342. segbuff[3]=Read_ds1302[1]>>4; 343. segbuff[4]=Read_ds1302[1]%16; 344. segbuff[5]=11; 345. segbuff[6]=Read_ds1302[2]>>4; 346. segbuff[7]=Read_ds1302[2]%16; 347. } 348. 349. else if(mod1==1) 350. { 351. segbuff[0]=14; 352. segbuff[1]=10; 353. segbuff[2]=10; 354. segbuff[3]=10; 355. segbuff[4]=10; 356. segbuff[5]=temp/100; 357. segbuff[6]=temp/10%10+19; 358. segbuff[7]=temp%10; 359. } 360. else if(mod1==2) 361. { 362. segbuff[0]=16; 363. segbuff[1]=10; 364. segbuff[2]=v/100+19; 365. segbuff[3]=v/10%10; 366. segbuff[4]=v%10; 367. segbuff[5]=11; 368. segbuff[6]=11; 369. if(v/100) 370. segbuff[7]=0; 371. else segbuff[7]=1; 372. 373. } 374. } 375. 376. else 377. { 378. 379. if(para_mod1==0) 380. { 381. segbuff[0]=18; 382. segbuff[1]=1; 383. segbuff[2]=11; 384. segbuff[3]=11; 385. segbuff[4]=11; 386. segbuff[5]=11; 387. segbuff[6]=para_time/10; 388. segbuff[7]=para_time%10; 389. } 390. 391. else if(para_mod1==1) 392. { 393. segbuff[0]=18; 394. segbuff[1]=2; 395. segbuff[2]=10; 396. segbuff[3]=10; 397. segbuff[4]=10; 398. segbuff[5]=10; 399. segbuff[6]=para_temp/10; 400. segbuff[7]=para_temp%10; 401. } 402. else if(para_mod1==2) 403. { 404. segbuff[0]=18; 405. segbuff[1]=3; 406. segbuff[2]=10; 407. segbuff[3]=10; 408. segbuff[4]=10; 409. segbuff[5]=10; 410. segbuff[6]=10; 411. segbuff[7]=para_led; 412. 413. } 414. 415. 416. 417. } 418. 419. 420. }
然后.h文件
1. #ifndef _DMX_H 2. #define _DMX_H 3. 4. #define state_0 0 5. #define state_1 1 6. #define state_2 2 7. #define kbd_io P3 8. #define kbd_maskrow 0x0f 9. extern unsigned int temp; 10. extern unsigned int v; 11. void time0(); 12. void led_work(); 13. void scan_kbd(); 14. void key_real(); 15. void Slect_fun(unsigned char n) ; 16. void segs(); 17. void Init_ds1302(); 18. void Read_time(); 19. void SMG_show(); 20. void Init_System(); 21. extern unsigned count; 22. #endif
然后iic ,和onewire 文件就不提供了 ,都是基础的代码,小编也没有修改。