电路原理图如下:
单片机源程序如下:
- #include<reg51.h>
- #include<stdio.h>
- #include<math.h>
- #define uint unsigned int
- #define uchar unsigned char
- unsigned int temp;
- unsigned char t[6] ={"0000`C"} ;
- float U1;
- unsigned int code K_TABLE[242]={0,198,397,597,798,1000,1203,1407,1614,1817,2022,2229,2436,2643,2850,3058,3266,
- 3473,3681,3888,4095,4302,4508,4714,4919,5124,5327,5531,5733,5936,6137,6338,6539,6739,6939,7139,7338,7538,7737,7937,8137,8336,8537,8737,8938,
- 9139,9341,9543,9745,9948,10151,10355,10560,10764,10969,11175,11381,11587,11793,12000,12207,12415,12623,12831,13039,13247,13456,13665,13874,
- 14083,14292,14502,14712,14922,15132,15342,15552,15763,15974,16184,16395,16607,16818,17029,17241,17453,17664,17876,18088,18301,18513,18725,18938,
- 19150,19363,19576,19788,20001,20214,20427,20640,20853,21066,21280,21493,21706,21919,22132,22346,22559,22772,22985,23198,23411,23624,23837,24050,
- 24263,24476,24689,24902,25114,25327,25539,25751,25964,26176,26387,26599,26811,27022,27234,27445,27656,27867,28078,28288,28498,28709,28919,29128,
- 29338,29547,29756,29965,30174,30383,30591,30799,31007,31214,31422,31629,31836,32042,32249,32455,32661,32866,33072,33277,33482,33686,33891,34095,
- 34299,34502,34705,34909,35111,35314,35516,35718,35920,36121,36323,36524,36724,36925,37125,37325,37524,37923,38122,38320,38519,38717,38915,39112,
- 39310,39546,39703,39900,40096,40292,40488,40634,40897,41074,41296,41463,41657,41851,42045,42239,42432,42625,42817,43010,43202,43394,43585,43777,
- 43968,44159,44349,44539,44729,44919,45108,45297,45486,45675,45863,46051,46238,46425,46612,46799,46985,47171,47356,47542,47726,47911,48095,48279,48462,48645,48828
- };//0~1200°C范围的 K分度表,每隔5
- sbit RS=P2^0; //数据(L)/命令(H)选择
- sbit LCDRW=P2^1; //写,低电平有效
- sbit EN=P2^2; //使能,低电平有效
- sbit START=P2^3;
- sbit ALE=P2^4;
- sbit EOC=P2^5;
- sbit OE=P2^6;
- unsigned int search (float input);
- void conv_start(void);
- void conv_check(void);
- int read_data(void);
- void delay_ms(int);
- void write_data(char);
- void write_com(char);
- void LCD_home(void);
- void lcd_init();
- void print_string(char*);
- //----------------------------------------------------------------------------
- void main()
- {
- int res=0;
- float vol=0.0;
- float vin;
- unsigned int tempin;
- char pr[8];
- lcd_init();
- while(1)
- {
- res=read_data();
- vol=5.0*res/255;
- vin=vol/10*1000;
- tempin=search(vin);
- sprintf(pr,"%d",tempin);
- LCD_home();
- print_string("Temperature:");
- print_string(pr);
- write_data(0xDF);
- print_string("C ");
- }
- }
- //----------------------------------------------------------------------------
- unsigned int search (float input) /*+查表子函数*/
- {
- unsigned int da=0,max,min,mid,j;
- da=input*1000; /*u1扩大1000倍 */
- //da=da+1203;//30 度
- max=242;
- min=0;
- temp=0;
- while(1)
- {
- mid=(max+min)/2; /*中心元素位置*/
- if(K_TABLE[mid]==da)
- {
- temp=mid*5;
- break;
- } /*中心元素等于查表的元素,计算相应温度*/
- if(K_TABLE[mid]>da)
- max=mid-1;
- else
- min=mid+1;
- if((max-min)==1 ) /*线性插值计算温度值*/
- {
- if(da >= K_TABLE[min])
- {
- j=(K_TABLE[max]- K_TABLE[min])/5; /*表中相邻两值对应温度相差5°C*/
- j=(da- K_TABLE[min])/j;
- temp=5*min+j;
- break;
- }
- if(da <= K_TABLE[min])
- {
- j=(K_TABLE[max]- K_TABLE[min])/5; /*表中相邻两值对应温度相差5°C*/
- j=(K_TABLE[min]-da )/j;
- temp=5*min-j;
- break;
- }
- }
- if(max==min)
- {
- if(da >= K_TABLE[min])
- {
- j=(K_TABLE[min+1]- K_TABLE[min])/5;
- j=(da- K_TABLE[min])/j;
- }
- else if(da < K_TABLE[min])
- {
- j=(K_TABLE[min]- K_TABLE[min-1])/5;
- j=(da- K_TABLE[min-1])/j;
- min = min -1;
- }
- temp=5*min+j;
- break;
- }
- }
- return temp;
- }
- //----------------------------------------------------------------------------
- void conv_start()
- {
- ALE=1;
- START=1;
- delay_ms(1); //当晶振较高时加延时
- ALE=0;
- START=0;
- // delay_ms(5); //当晶振较高时加延时
- }
- //----------------------------------------------------------------------------
- int read_data()
- {
- int i=0;
- OE=1;
- conv_start();
- while(!EOC);
- i=P1;
- OE=0;
- return i;
- }
- //----------------------------------------------------------------------------
- void delay_ms(int t)
- {
- int j;
- for( ;t!=0; t--)
- for(j=0;j<255;j++);
- }
- //----------------------------------------------------------------------------
- void write_data(char dat)
- {
- RS=1; //数据
- LCDRW=0; //写
- P0=dat; //把数据送到P口
- delay_ms(5); //当晶振较高时加延时
- EN=1;
- delay_ms(5); //当晶振较高时加延时
- EN=0; //关使能
- }
- //----------------------------------------------------------------------------
- void write_com(char com)
- {
- RS=0; //命令
- LCDRW=0; //写
- P0=com;
- delay_ms(5); //当晶振较高时加延时
- EN=1;
- delay_ms(5); //当晶振较高时加延时
- EN=0;
- }
- //----------------------------------------------------------------------------
- void LCD_home(void)
- {
- write_com(0x02);
- }
- //----------------------------------------------------------------------------
- void lcd_init()
- {
- LCDRW=0;
- RS=0;
- write_com(0x36); //set text home address
- write_com(0x0c); //display on,cursor off,blink off
- write_com(0x38);
- write_com(0x06); //input set.
- write_com(0x01); //screen clear
- write_com(0x80); //DDRAM AD set to 00h,
- }
- //----------------------------------------------------------------------------
- void print_string(char* str)
- {
- int i;
- for(i=0;str[ i]!=0;i++)
- {
- write_data(str[ i]);
- }
- }
复制代码
|