首先驱动onewire.固定模块我这边就直接贴代码了
1. #include <STC15F2K60S2.H> 2. sbit DQ=P1^4; 3. 4. void Delay_OneWire(unsigned int t) 5. { 6. unsigned char i; 7. while(t--){ 8. for(i=0;i<12;i++); 9. } 10. } 11. 12. // 13. void Write_DS18B20(unsigned char dat) 14. { 15. unsigned char i; 16. for(i=0;i<8;i++) 17. { 18. DQ = 0; 19. DQ = dat&0x01; 20. Delay_OneWire(5); 21. DQ = 1; 22. dat >>= 1; 23. } 24. Delay_OneWire(5); 25. } 26. 27. // 28. unsigned char Read_DS18B20(void) 29. { 30. unsigned char i; 31. unsigned char dat; 32. 33. for(i=0;i<8;i++) 34. { 35. DQ = 0; 36. dat >>= 1; 37. DQ = 1; 38. if(DQ) 39. { 40. dat |= 0x80; 41. } 42. Delay_OneWire(5); 43. } 44. return dat; 45. } 46. 47. // 48. bit init_ds18b20(void) 49. { 50. bit initflag = 0; 51. 52. DQ = 1; 53. Delay_OneWire(12); 54. DQ = 0; 55. Delay_OneWire(80); 56. DQ = 1; 57. Delay_OneWire(10); 58. initflag = DQ; 59. Delay_OneWire(5); 60. 61. return initflag; 62. } 63. 64. int Read_temp() 65. { 66. unsigned int temp; 67. unsigned char low,high; 68. float i; 69. unsigned int result; 70. init_ds18b20(); 71. Write_DS18B20(0xcc); 72. Write_DS18B20(0x44); 73. 74. init_ds18b20(); 75. Write_DS18B20(0xcc); 76. Write_DS18B20(0xbe); 77. 78. low=Read_DS18B20(); 79. high=Read_DS18B20(); 80. high=high&0x1f; 81. result=(high<<8)|low; 82. i=result *0.0625; 83. temp=i; 84. return temp; 85. 86. }
实时时钟代码块
1. /* # DS1302代码片段说明 2. 1. 本文件夹中提供的驱动代码供参赛选手完成程序设计参考。 3. 2. 参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题 4. 中对单片机时钟频率的要求,进行代码调试和修改。 5. */ 6. 7. // 8. #include <STC15F2K60S2.H> 9. #include "intrins.h" 10. sbit RST=P1^3; 11. sbit SDA=P2^3; 12. sbit SCK=P1^7; 13. void Write_Ds1302(unsigned char temp) 14. { 15. unsigned char i; 16. for (i=0;i<8;i++) 17. { 18. SCK = 0; 19. SDA = temp&0x01; 20. temp>>=1; 21. SCK=1; 22. } 23. } 24. 25. // 26. void Write_Ds1302_Byte( unsigned char address,unsigned char dat ) 27. { 28. RST=0; _nop_(); 29. SCK=0; _nop_(); 30. RST=1; _nop_(); 31. Write_Ds1302(address); 32. Write_Ds1302(dat); 33. RST=0; 34. } 35. 36. // 37. unsigned char Read_Ds1302_Byte ( unsigned char address ) 38. { 39. unsigned char i,temp=0x00; 40. RST=0; _nop_(); 41. SCK=0; _nop_(); 42. RST=1; _nop_(); 43. Write_Ds1302(address); 44. for (i=0;i<8;i++) 45. { 46. SCK=0; 47. temp>>=1; 48. if(SDA) 49. temp|=0x80; 50. SCK=1; 51. } 52. RST=0; _nop_(); 53. SCK=0; _nop_(); 54. SCK=1; _nop_(); 55. SDA=0; _nop_(); 56. SDA=1; _nop_(); 57. return (temp); 58. }
然后这个代码是实现各个功能模块的
1. #include <STC15F2K60S2.H> 2. #include "dmx.h" 3. #include "onewire.h" 4. #include "ds1302.h" 5. bit flag_t=1; 6. unsigned char mod1=2; 7. unsigned char mod2=0; 8. unsigned int count_f=0; 9. unsigned int start=10; 10. unsigned int start_temp=0; 11. unsigned int temp_flag=0; 12. unsigned int count=0; 13. bit flag=0; 14. bit flag_led=0; 15. unsigned char count_led=0; 16. unsigned int temp=0; 17. unsigned char mod3=0; 18. unsigned int key_num=16; 19. unsigned char Read_ds1302[3]={0}; 20. unsigned char a[10]={0}; 21. unsigned char segbuff[8]={10,10,10,10,10,11,0,1}; 22. unsigned char segtab[]={0xc0, //0 23. 0xf9, //1 24. 0xa4, //2 25. 0xb0, //3 26. 0x99, //4 27. 0x92, //5 28. 0x82, //6 29. 0xf8, //7 30. 0x80, //8 31. 0x90, //9 32. 0xff, //ϨÃð 33. 0xbf, //- 34. 0x88, //A 35. 0x83, //b 36. 0xc6, //C 37. 0xa1, //d 38. 0x86, //E 39. 0x8e //F 40. }; 41. 42. void time0() 43. { 44. TMOD=0x01; 45. TH0=(0-1000)/256; 46. TL0=(0-1000)%256; 47. TR0=1; 48. ET0=1; 49. EA=1; 50. 51. } 52. 53. void Timer0() interrupt 1 54. { 55. TH0=(0-1000)/256; 56. TL0=(0-1000)%256; 57. count++; 58. segs(); 59. if(count==(start*100)) 60. { 61. temp_flag=1; 62. count=0; 63. 64. } 65. if(mod2==1){ 66. count_f++; 67. if(count_f==1000) 68. { 69. flag^=1; 70. count_f=0; 71. 72. } 73. 74. } 75. 76. if(flag_led) 77. { 78. count_led++; 79. P0=0xff; 80. 81. if(count_led<100) 82. { Slect_fun(4); 83. P0=0xff; 84. 85. 86. } 87. else 88. { Slect_fun(4); 89. P0=0xfe; 90. if(count_led==200)count_led=0; 91. } 92. 93. Slect_fun(0); 94. P0|=0x01; 95. 96. 97. } 98. 99. 100. } 101. 102. void scan_kbd() 103. { 104. static unsigned char state=0; 105. unsigned char key_press; 106. 107. switch (state) 108. { 109. case state_0: 110. kbd_io=0x0f; 111. key_press=kbd_io; 112. if(key_press!=kbd_maskrow) 113. state=state_1; 114. break; 115. 116. 117. case state_1: 118. //kbd_io=0x0f; 119. key_press=kbd_io; 120. if(key_press!=kbd_maskrow) 121. { 122. if((kbd_io&0x08)==0)key_num=0; 123. if((kbd_io&0x04)==0)key_num=1; 124. if((kbd_io&0x02)==0)key_num=2; 125. if((kbd_io&0x01)==0)key_num=3; 126. state=state_2; 127. 128. } 129. else state=state_0; 130. break; 131. 132. case state_2: 133. kbd_io=0x0f; 134. key_press=kbd_io; 135. if(key_press==0x0f)state=state_0; 136. 137. break; 138. 139. 140. default :break; 141. 142. } 143. 144. } 145. void key_real() 146. { 147. 148. switch(key_num) 149. { 150. 151. case 0: 152. mod1++; 153. if(mod1==4)mod1=0; 154. 155. if(mod1==0)start_temp=10; 156. else if(mod1==1)start_temp=50; 157. else if(mod1==2)start_temp=300; 158. else if(mod1==3)start_temp=600; 159. 160. break; 161. 162. case 1: 163. mod2=1; 164. start=start_temp; 165. break; 166. 167. case 2: 168. mod3++; 169. mod2=2; 170. flag_led=0; 171. P0=0xff; 172. Slect_fun(4); 173. P0=0xff; 174. break; 175. 176. case 3: 177. mod2=0; 178. mod3=0; 179. //mod1++; 180. if(mod1==4)mod1=0; 181. break; 182. 183. default :break; 184. 185. 186. 187. 188. } 189. 190. key_num=16; 191. 192. 193. 194. } 195. void Slect_fun(unsigned char n) 196. { 197. 198. P2=P2&0x1f; 199. 200. switch(n) 201. { 202. case 0:P2|=0x00; break; 203. case 4:P2|=0x80; break; 204. case 5:P2|=0xa0; break; 205. case 6:P2|=0xc0; break; 206. case 7:P2|=0xe0; break; 207. default : break; 208. 209. 210. 211. } 212. 213. } 214. void Init_System() 215. { 216. unsigned char i; 217. Slect_fun(4);P0=0xff; 218. Slect_fun(5);P0=0xaf; 219. for(i=0;i<3;i++) 220. { 221. Read_temp(); 222. 223. } 224. time0(); 225. 226. } 227. void segs() 228. { 229. static unsigned char wei=0; 230. Slect_fun(7); 231. P0=0xff; 232. 233. Slect_fun(6); 234. P0=1<<wei; 235. 236. Slect_fun(7); 237. P0=segtab[segbuff[wei]]; 238. wei++; 239. if(wei==8)wei=0; 240. 241. Slect_fun(0); 242. 243. } 244. void Init_ds1302() 245. { 246. 247. 248. Write_Ds1302_Byte(0x8e,0x00); 249. Write_Ds1302_Byte(0x84,0x23); 250. Write_Ds1302_Byte(0x82,0x59); 251. Write_Ds1302_Byte(0x80,0x50); 252. Write_Ds1302_Byte(0x8e,0x80); 253. 254. 255. } 256. void Read_time() 257. { 258. 259. Read_ds1302[0]=Read_Ds1302_Byte(0x85); 260. Read_ds1302[1]=Read_Ds1302_Byte(0x83); 261. Read_ds1302[2]=Read_Ds1302_Byte(0x81); 262. 263. 264. } 265. void SMG_show() 266. { 267. if(mod2==0) 268. { 269. segbuff[0]=10; 270. segbuff[1]=10; 271. segbuff[2]=10; 272. segbuff[3]=10; 273. segbuff[4]=10; 274. segbuff[5]=11; 275. switch (mod1) 276. { 277. case 0: 278. segbuff[6]=0; 279. segbuff[7]=1; 280. break; 281. 282. case 1: 283. segbuff[6]=0; 284. segbuff[7]=5; 285. break; 286. 287. case 2: 288. segbuff[6]=3; 289. segbuff[7]=0; 290. break; 291. 292. case 3: 293. segbuff[6]=6; 294. segbuff[7]=0; 295. break; 296. default : break; 297. 298. } 299. } 300. 301. 302. else if(mod2==1) 303. { 304. if(flag_t) 305. { 306. Init_ds1302();flag_t=0; 307. } 308. 309. segbuff[0]=Read_ds1302[0]>>4; 310. segbuff[1]=Read_ds1302[0]%16; 311. if(flag==1)segbuff[2]=10; 312. else segbuff[2]=11; 313. 314. segbuff[3]=Read_ds1302[1]>>4; 315. segbuff[4]=Read_ds1302[1]%16; 316. 317. if(flag==1)segbuff[5]=10; 318. else segbuff[5]=11; 319. 320. segbuff[6]=Read_ds1302[2]>>4; 321. segbuff[7]=Read_ds1302[2]%16; 322. 323. } 324. else if(mod2==2) 325. { 326. 327. segbuff[0]=11; 328. segbuff[1]=0; 329. segbuff[2]=mod3; 330. 331. 332. segbuff[3]=10; 333. segbuff[4]=10; 334. 335. 336. segbuff[5]=11; 337. 338. segbuff[6]=a[mod3]/10; 339. segbuff[7]=a[mod3]%10; 340. 341. 342. 343. } 344. 345. 346. }
主函数如下
1. #include "dmx.h" 2. #include "onewire.h" 3. unsigned char caiji=0; 4. void main() 5. { 6. 7. Init_System(); 8. while(1) 9. { 10. if(temp_flag&&(mod2==1)) 11. { 12. a[caiji]= Read_temp(); 13. caiji++; 14. temp_flag=0; 15. if(caiji==10){ 16. caiji=0; 17. mod2=2; 18. flag_led=1; 19. } 20. 21. 22. } 23. 24. scan_kbd(); 25. key_real(); 26. Read_time(); 27. SMG_show(); 28. 29. 30. } 31. 32. 33. }