|
本帖最後由 antlu 於 2013-9-16 11:06 AM 編輯
簡易的溫度計,使用筆電電池拆下來的 薄膜溫度感應器(NTC) 25度10K歐姆 B=4400,EXCEL公式 =10K*EXP(4400*((1/(溫度+273.13))-(1/(25+273.13)))),運用查表的方式把NTC的溫度電阻對應的溫度送到顯示LCM顯示出來,供應電壓5V的變化會影響到溫度的顯示,最好電壓為穩壓值. 小數點使用線上插值方式,以上的公式與經驗都是網路上得到的,在此感謝無名提供者!!
//#include<STC15F204EA.h>
//#include<intrins.h>
/* 2013 0916 modify delay time LCM 雖然程式使用4BIT 但是仍然要接8BIT才會顯示 浮點運算會導致 結果大影響,使用查表方式並且運用 線性方式取得小數點的溫度值
*/
#include <reg52.h>
#include<intrins.h>
sfr ADC_CONTR = 0xBC; //0000,0000 A/Dת»»¿ØÖƼĴæÆ÷
sfr ADC_RES = 0xBD; //0000,0000 A/Dת»»½á¹û¸ß8λ
sfr ADC_RESL = 0xBE; //0000,0000 A/Dת»»½á¹ûµÍ2λ
sfr P1M0 = 0x92; //0000,0000 Ë¿Ú1ģʽ¼Ä´æÆ÷0
sfr P1M1 = 0x91; //0000,0000 Ë¿Ú1ģʽ¼Ä´æÆ÷1
sfr P1ASF = 0x9D; //0000,0000 Ë¿Ú1Ä£Ä⹦ÄÜÅäÖüĴæÆ÷
sbit RS = P3^7; //©w…óºÝ¤f
sbit RW = P3^6;
sbit EN = P3^5;
char displayword[16];
unsigned int code tempresist[]={27421,26200,25042,23942,22899,21908,20966,20071,19221,18411,17642,16909,16212,15548,14916,14313,13739,13192,12669,12171,11696,11242,10809,10395,10000,9622,9260,8915,8585,8269,7966,7677,7400,7134,6880 ,6636,6403,6179,5964,5758,5560,5371,5189,5114,4846,4685,4530,5381,4238,4100,3968,3841,3718,3600,3487,3378,3273,3171,3074,2980,2889,2802,2718,2637,2558,2483,2410,2340,2272,2206,2143,2082,2023,1966,1911,1858,1806,1756,1708,1662,1617,1573,1531,1490,1451,1412,1375,1339,1305,1271,1238,1207,1176,1146,1118,1090,1063,1036,1011,986 };
#define RS_CLR RS=0
#define RS_SET RS=1
#define RW_CLR RW=0
#define RW_SET RW=1
#define EN_CLR EN=0
#define EN_SET EN=1
#define DataPort P2 //STC15F204EA P1 A/D P2 LCM P3^7--RS P3^6--W/R P3^5--E
unsigned int L,M,N,Q;//Y©w…ó¦¨uchar«´N¥u¯àF¥Ü2.5V¥H¤Uªº‡ÛÈ
unsigned int R,ADC_DATA;
unsigned int TP0,TP1,TP2,TP3,P0n,P1n,P2n,P3n;
//unsigned int SV,AV;//´«ºâ«áªº³]©w•Å«×sv=100/V ´«ºâ«áªº¼ö°¸•Å«×av=100/V
unsigned char Rtemp0,Rtemp1,Rtemp2,Rtemp3;//P1n ¤p¼ÆÂI«á1¦ì
unsigned char Compare_tempres (unsigned int);
/*------------------------------------------------
uS©µƒº¨ç‡Û¡A§t¦³‰r¤Jƒò‡Û unsigned char t¡AÆÓªð¦^È
unsigned char O©w…óÆӲņA¦r²Å@q¡A¨äȪºS„ÏO
0~255 „±¨½¨Ï¥Î´¹®12M¡AºëÚÌ©µƒºˆ[¨Ï¥Î…¹ˆC,¤jP©µƒº
ƒc«×¦p¤U T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{ //¤jP©µƒº1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
§P¦£¨ç‡Û
------------------------------------------------*/
bit LCD_Check_Busy(void)
{
DataPort= 0xFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return (bit)(DataPort & 0x80);
}
/*------------------------------------------------
‡À¤J©R¥O¨ç‡Û
------------------------------------------------*/
/* void LCD_Write_Com(unsigned char com)
{
while(LCD_Check_Busy()); //¦£ƒhµ¥«Ý
RS_CLR;
RW_CLR;
EN_SET;
DataPort= com;
_nop_();
EN_CLR;
DelayMs(1);
}
*/
void LCD_Write_Com(char com) //©R¥O¼g¤J 4bit
{
while(LCD_Check_Busy()); //¦£ƒhµ¥«Ý 1=while loop still;
RS_CLR; //«ü¥O¥\¯à
RW_CLR; //¼g¤J
EN_SET; //¤U°½t ¥ÑHIÅܦ¨LO
DataPort= com; // «ü¥O³]©w
_nop_();
EN_CLR;//
com<<=4;
EN_SET; //¤U°½t ¥ÑHIÅܦ¨LO
_nop_();
DataPort= com; // «ü¥O³]©w
EN_CLR;//
DelayMs(1);
}
/*------------------------------------------------
‡À¤J‡ÛÕu¨ç‡Û
------------------------------------------------*/
/* void LCD_Write_Data(unsigned char Data)
{
while(LCD_Check_Busy()); //¦£ƒhµ¥«Ý
RS_SET;
RW_CLR;
EN_SET;
DataPort= Data;
_nop_();
EN_CLR;
DelayMs(1);
}
*/
void LCD_Write_Data(char Data) //¸ê®Æ¼g¤J4bit
{
while(LCD_Check_Busy()); //¦£ƒhµ¥«Ý
RS_SET; // ¸ê®Æ³B²z
RW_CLR;// ¼gªº°Ê§@
EN_SET; // ±Ò°Ê¤U½tIJµo
DataPort= Data; //¿é¤J»Ýn¼g¤Jªº¸ê®Æ
_nop_();//©µ«á¤@Ӯɡ
EN_CLR;//
Data<<=4;
EN_SET; // ±Ò°Ê¤U½tIJµo
DataPort= Data; //¿é¤J»Ýn¼g¤Jªº¸ê®Æ
_nop_();//©µ«á¤@Ӯɡ
EN_CLR;//
DelayMs(1);//µ¹ 1T MCU ¨Ï¥Î
}
/*------------------------------------------------
²M«Ì¨ç‡Û
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
‡À¤J¦r²Å¦ê¨ç‡Û
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
‡À¤J¦r²Å¨ç‡Û
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
/*------------------------------------------------
ªì©l¤Æ¨ç‡Û
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x38); /*F¥Ü¼Ò¦¡„¦¸m*/
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
DelayMs(5);
LCD_Write_Com(0x38);
LCD_Write_Com(0x08); /*F¥Ü‹×„¸*/
LCD_Write_Com(0x01); /*F¥Ü²M«Ì*/
LCD_Write_Com(0x06); /*F¥Ü¥ú‡á²¾ƒð„¦¸m*/
DelayMs(5);
LCD_Write_Com(0x0C); /*F¥Ü…{¤Î¥ú‡á„¦¸m*/
}
/*******************************************************************
¨ç‡Û¦W¡GADCªì©l¤Æ¤Î8¦ìA/D‹H„ã¨ç‡Û
ªð¦^È¡G8¦ìªºADC‡ÛÕu
…C ªG¡GŒÝ¥X«ü©wADC±µ¤fªºA/D‹H„ãÈ¡A¦}ªð¦^‡ÛÈ
„ ª`¡GÓì¥Î¤_STC12C2052AD¨t¦C„ˤùÉó¡]¥²…§¨Ï¥ÎSTC12C2052AD.h‰³¤å¥ó¡^
*******************************************************************/
unsigned int Read (unsigned char CHA){
unsigned char AD_FIN=0; //¦s‰ÉA/D‹H„ã‡á§Ó;Y¦b¨ç‡Û¥~©w…ó¦¹@qƒh¤£¯à±o¨ì„²Œ¢@¤Æªº¼Ò‰ÞqªºF¥Ü
/******¥H¤UƒoADCªì©l¤Æµ{§Ç****************************/
CHA &= 0x07; //‰uˆáADCªº8ƒª±µ¤f¤¤ªº¤@ƒª¡]0000 0111 ²M0°ª5¦ì¡^
ADC_CONTR = 0x60; //ADC‹H„㪺³t«×¡]0XX0 0000 ¨ä¤¤XX±±¨î³t«×¡Aˆ[®ÚÕu‡ÛÕu¤â•ä„¦¸m¡^
_nop_();
ADC_CONTR |= CHA; //‰uˆáA/D…å«e³q¹D
_nop_();
ADC_CONTR |= 0x80; //„QƒðA/D†z•½
DelayMs(1); //¨Ï‰r¤J†z‰Í†c¨ì‹®©w¡]1ms§Y¥i?
/******¥H¤UƒoADCƒù¦æµ{§Ç****************************/
ADC_CONTR |= 0x08; //„QƒðA/D‹H„ã¡]0000 1000 ¥OADCS = 1¡^
_nop_();
_nop_();
_nop_();
_nop_();
while (AD_FIN ==0){ //µ¥«ÝA/D‹H„ã…C§ô
AD_FIN = (ADC_CONTR & 0x10); //0001 0000„ò†IA/D‹H„ã…C§ô§_
}
ADC_CONTR &= 0xE7; //1111 0111 ²MADC_FLAG¦ì, ‹×„¸A/D‹H„ã,
ADC_DATA=ADC_RES*4+ADC_RESL;
return (ADC_DATA); //ªð¦^A/D‹H„ã…CªG¡]8¦ì¡^
}
/******************************************************************
read an channel then get thermo-resister
******************************************************************/
unsigned int transfer(unsigned char an){
float PP,Kp;
unsigned int Rt;
M=Read(an);// tc read out volt
N=Read(4);//P1.5¤f2.5V°òã†z‰Í•½ªö‡Þ¡]‹H„ã¡^
PP=(M*2.5/N)*1000;//read an temp convert to mV
Kp=10000/(5000/PP-1); //float caculate
Rt=Kp;
return Rt;//return thermo-resister value
}
void display(void)
{
//
displayword[0]='0';
displayword[1]=':';
displayword[2]=Rtemp0/100+'0';
displayword[3]=(Rtemp0%100)/10+'0';
displayword[4]= (Rtemp0%10)+'0';
displayword[5]='.';
displayword[6]=P0n+'0';
displayword[7]='c';
displayword[8]= '1';
displayword[9]= ':';
displayword[10]=Rtemp1/100+'0';
displayword[11]=(Rtemp1%100)/10+'0';
displayword[12]=Rtemp1%10+'0';
displayword[13]='.';
displayword[14]=P1n+'0';
displayword[15]='c';
LCD_Write_String(0,0, displayword);//Åã¥Ü¦b²Ä¤@¦æ
displayword[0]='2';
displayword[1]=':';
displayword[2]=Rtemp2/100+'0';
displayword[3]=(Rtemp2%100)/10+'0';
displayword[4]= (Rtemp2%10)+'0';
displayword[5]='.';
displayword[6]=P2n+'0';
displayword[7]='c';
displayword[8]= '3';
displayword[9]= ':';
displayword[10]=Rtemp3/100+'0';
displayword[11]=(Rtemp3%100)/10+'0';
displayword[12]=Rtemp3%10+'0';
displayword[13]='.';
displayword[14]=P3n+'0';
displayword[15]='c';
LCD_Write_String(0,1, displayword);//Åã¥Ü¦b²Ä¤G¦æ
}
unsigned char Compare_tempres(unsigned int TR)
{
unsigned char cmp_cnt;
cmp_cnt =0;
while (TR<tempresist[cmp_cnt])
{
cmp_cnt++;
if (cmp_cnt>100)
break;
}
return cmp_cnt;
}
void main(void)
{
P1M0 = 0xff; //P1.0/P1.1¡G1111 1111¡]°ªªý¡^//ª`•N¡G§ó§ïADC³q¹Dƒº…§¦Pƒº’â†Á‰ØªºIO±µ¤fקïƒo°ªªý‰r¤J¡C
P1M1 = 0x00; //P1.0/P1.1¡G0000 0000
P1ASF=0xff; //all analog in put mode
LCD_Init();
LCD_Clear();//²M«Ì
while (1)
{
DelayMs(250);
DelayMs(250);
TP0= transfer(0);//read chanel 0 resister value(chanel 0 •Å«×¹qªý)
Rtemp0=Compare_tempres(TP0);// table search to temperature dªí¨ú±o•Å«×
P0n=((tempresist[Rtemp0-1]-TP0)*10)/(tempresist[Rtemp0-1]-tempresist[Rtemp0]);//T¤p¼ÆÂI½u©Ê
TP1=transfer(1);//read chanel 1 resister value(chanel 1 •Å«×¹qªý)
Rtemp1= Compare_tempres(TP1);//dªí¨ú±o•Å«×
P2n=((tempresist[Rtemp1-1]-TP1)*10)/(tempresist[Rtemp1-1]-tempresist[Rtemp1]);//TP¤p¼ÆÂI½u©Ê
TP2= transfer(2);//read chanel 0 resister value(chanel 0 •Å«×¹qªý)
Rtemp2=Compare_tempres(TP2);// table search to temperature dªí¨ú±o•Å«×
P2n=((tempresist[Rtemp2-1]-TP2)*10)/(tempresist[Rtemp2-1]-tempresist[Rtemp2]);//T¤p¼ÆÂI½u©Ê
TP3=transfer(3);//read chanel 1 resister value(chanel 1 •Å«×¹qªý)
Rtemp3= Compare_tempres(TP3);//dªí¨ú±o•Å«×
P3n=((tempresist[Rtemp3-1]-TP3)*10)/(tempresist[Rtemp3-1]-tempresist[Rtemp3]);//TP¤p¼ÆÂI½u©Ê
DelayMs(250);
display();
DelayMs(250);
DelayMs(250);
}
}
|
-
評分
-
10
查看全部評分
-
|