51单片机热敏电阻测温查表程序1.#include<reg51.h>2.#include"LCD.h"3.#define uchar unsigned char4.#define uint unsigned int5.#define N 106.uchar bai,ge,shi,n;7.uint temp,tp;temph,templ;8.uint code ad_date[]={9. 1060,1060,1060,1100,1120,1140,1166,1180,1200,1220,10. 1240,1320,1340,1360,1380,1400,1420,1440,1460,1480,11. 1500,1520,1540,1560,1580,1600,1620,1640,1660,1680,12. 1700,1720,1740,1760,1780,1800,1820,1840,1840,1860,13. 1880,1900,1920,1940,1960,1980,2000,2020,2040,2060,14. 2080,2100,2120,2140,2160,2180,2200,2220,2240,2260,15. 2280,2300,2320,2340,2360,2380,2400,2420,2440,2460,16. 2480,2500,2520,2540,2560,2580,2600,2620,2640,2660,17. 2680,2700,2720,2740,2760,2780,2800,2820,2840,2860,18. 2880,2900,2920,2920,2940,2960,2980,3000,3020,3040,19. 3060,3080,3100,3120,3140,3160,3180,3200,3220,3240,20. 3260,3280,3300,3320,3340,3360,3380,3400,3420,3440,21. 3460,3480,3500,3520,3540,3560,3580,3600,3620,3640,22. 3660,3680};23.uint code temperiture[]={24. 594,593,586,580,579,560,564,559,552,545,25. 541,518,513,508,503,497,491,488,483,480,26. 473,468,463,458,455,451,447,440,434,433,27. 430,425,420,416,413,409,404,401,401,396,28. 390,386,382,378,374,372,368,364,361,357,29. 354,351,347,343,340,336,333,328,326,322,30. 319,316,313,309,307,303,300,296,294,289,31. 286,283,280,276,274,271,267,264,260,257,32. 253,249,246,243,240,237,233,231,228,224,33. 222,219,214,216,211,209,206,202,198,194,34. 187,184,181,178,175,171,168,164,161,158,35. 154,152,148,146,142,139,136,133,130,126,36. 124,121,116,114,112,108,102,98,96,94,37. 89,86};38.uchar code table[]=" T:00.0`C";39.uchar num=0;40.uchar keynum,max=40,min=0;41.uchar code Temp_max[]=" Max:40`C",Temp_min[]=" Min:00`C";42.43.sbit ad_wr=P3^6;44.sbit ad_rd=P3^7;45.sbit ad_cs=P3^5;46.sbit ad_int=P3^2;47.sbit key1=P0^5;48.sbit key2=P0^6;49.sbit key3=P0^7;50.//sbit Beep=P0^0;51.52./*********函数表************/53.void init(); //初始化54.void ad_delay(uint t); //延时函数55.void change(uchar tempture); //分离百位、十位、个位56.uchar ad_getshuju(); //ad转换57.char filter(); //滤波58.uint convert(); //电压转换59.void tempertur(); //度温度60.void keyscan(); //键盘扫描61.62.//主函数63.void main()64.{65. init();66.while(1)67. {68. keyscan();69. }70.}71.72.void init()73.{74. LCD_init();75.for(num=0;num<10;num++)76. {77. LCD_Write_add_data(num,0,table[num]);78. }79.}80.81.void ad_delay(uint t)82.{83. uchar j;84. uint i;85.for(i=0;i<t;i++)86.for(j=0;j<110;j++);87.}88.89.uchar ad_getshuju()90.{91. uchar date;92. ad_cs=0;ad_wr=0;ad_rd=1;93. ad_cs=1;ad_wr=1;ad_rd=1;94.while(ad_int==1);95. ad_cs=0;ad_wr=1;ad_rd=0;96. date=P2;97. ad_cs=1;ad_wr=1;ad_rd=1;98.return date;99.}100.101.c har filter() //滤波函数中位值滤波法102.{103. uint value_buf[N],temp_f;104. uchar count,i,j;105.for ( count=0;count<N;count++)106. {107. value_buf[count] = ad_getshuju();108. delay(2);109. }110.for (j=0;j<N-1;j++)111. {112.for (i=0;i<N-j;i++)113. {114.if ( value_buf[i]>value_buf[i+1] )115. {116. temp_f = value_buf[i];117. value_buf[i] = value_buf[i+1];118. value_buf[i+1] = temp_f;119. }120. }121. }122.return value_buf[(N-1)/2];123.}124.125.u int convert()126.{127. uint temph,templ; //用于存储读出数据的高字节和低字节128. uint tmp="0"; //存储最后处理完的结果注意数据类型129. tmp=filter()+tmp; //求滤波后ad值130. temph=tmp&0xf0; //屏蔽低四位131. temph=temph>>4; //右移四位取出高四位132. templ=tmp&0x0f; //屏蔽高四位取出低四位133. tmp=templ*20+temph*320; //最后的结果是一个四位数,便于显示电压值134.return tmp; //返回最后处理结果135.}136.137.v oid change(uchar tempture)138.{139. bai=tempture/100;140. shi=tempture%100/10;141. ge=tempture%10;142.}143.144.v oid tempertur()145.{146.for(n=0;temp>=ad_date[n];n++);147. tp=temperiture[n-1];148. change(tp);149. LCD_Write_add_data(4,0,0x30+bai);150. LCD_Write_add_data(5,0,0x30+shi);151. LCD_Write_add_data(7,0,0x30+ge);152.}153.154.155.v oid keyscan()156.{157.if(key1==0)158. {159. delay(5);160.if(key1==0)161. {162.while(!key1);163. keynum++;164.if(keynum==1)165. {166. change(max);167. LCD_Write_Com(0x01);168. delay(10);169. LCD_Write_add_str(0,0,10,Temp_max);170. LCD_Write_add_data(6,0,0x30+shi);171. LCD_Write_add_data(7,0,0x30+ge);172. }173.if(keynum==2)174. {175. change(min);176. LCD_Write_Com(0x01);177. min=min>0?min:0;178. delay(10);179. LCD_Write_add_str(0,0,10,Temp_min); 180. LCD_Write_add_data(6,0,0x30+shi); 181. LCD_Write_add_data(7,0,0x30+ge); 182. }183.if(keynum==3)184. {185. keynum=0;186. LCD_Write_Com(0x01);187. delay(10);188. LCD_Write_add_str(0,0,10,table); 189. }190. }191. }192.if(keynum==0)193. {194. temp=convert();195. tempertur();196. delay(100);197. }198.else199. {200.if(keynum==1)201. {202.if(key2==0)203. {204. delay(5);205.if(key2==0)206. {207.while(!key2);208. max++;209. change(max);210. delay(1);211. }212. }213.if(key3==0)214. {215. delay(5);216.if(key3==0)217. {218.while(!key3);219. max--;220. change(max);221. delay(1);222. }223. }224.//LCD_Write_add_str(0,0,9,Temp_max); 225. LCD_Write_add_data(6,0,0x30+shi); 226. LCD_Write_add_data(7,0,0x30+ge); 227. }228.if(keynum==2)229. {230.if(key2==0)231. {232. delay(5);233.if(key2==0)234. {235.while(!key2);236. min++;237. change(min);238. min=min>0?min:0;239. delay(1);240. }241. }242.if(key3==0)243. {244. delay(5);245.if(key3==0)246. {247.while(!key3);248. min--;249. change(min);250. delay(1);251. }252. }253.//LCD_Write_add_str(0,0,9,Temp_min); 254. LCD_Write_add_data(6,0,0x30+shi); 255. LCD_Write_add_data(7,0,0x30+ge); 256. }257. }258.}。