1、温度传感器的单片机处理显示程序 作者: 日期:13 个人收集整理 勿做商业用途/Function:Test Temperature。30+105/Used Equation /RT/R25=e(B(1/(T+273)-(1/298)))/Change List:V0。9 V0.91(07,02)/Change display A error/V0.91 - V0。92(07,03)/Add Temp Tab jiaozheng 70100C/*/#include”ADUC812.h”include”math.h#includeintrins.h#include”string.h”include
2、stdio.h#includedada.hbdata unsigned char LCD_data8; / Display Buffer */intssdata3;#defineTemp_100235defineTemp_401352sbits_data = P20; /* LCD Serial Data Port */sbit clk_lcd = P22; /* LCD Clock Port */sbit lcd_cs = P21; /* LCD Chip Select Port /sbitled=P27;/* LCD Command Name Define */#define cmd_of
3、f 0x00 /* LCD Control Power Down /define cmd_on 0x01 /* LCD Control Power Up */define cmd_LCD_off 0x02 / Close LCD */#define cmd_LCD_on 0x03 /* Open LCD */define cmd_LCD_B3C4 0x29 / LCD Mode 1/3BAIS,1/4DUTY /#define cmd_LCD_RC 0x18 /* LCD Control Internal Oscillator 256K */data unsigned char Comm_bu
4、ff16; / Serial Port Receiver Buffer */data unsigned char Comm_len; /* Serial Port Receiver Data Length */bit Comm_ready; /* Serial Port Receiver Flag */ LCD Code 0,1,2。.A,B,F,0。,1。,.。A.,B。,F. */unsigned char code LCD_code = 0xeb,0x60,0xc7,0xe5,0x6c,0xad,0xaf,0xe0,0xef,0xed,0xee,0x2F,0x8b,0x67,0x8F,0
5、x8e, 0xfb,0x70,0xd7,0xf5,0x7c,0xbd,0xbf,0xf0,0xff,0xfd,0xfe,0x3f,0x9b,0x77,0x9f,0x9e, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ; /* delay */void Delay(int p)/* Delay p Clock Cycle */int j;for(j=0;j=p;j+)_nop_();/* Delay N ms */void Delay_ms(int n)inti,j;for(i=0
6、;i=n;i+)for(j=0;j=52;j+)/ Delay 1 ms */_nop_();/* LCD Data Display */void LCD_Display() unsigned char data i,j,k,tmp,tmp1,tmp2; /* Write Writr Mode Command Word 101 */ lcd_cs = 0;/* Chip Select Enable */ Delay(100);clk_lcd = 0; /* Clock Down Edge */ s_data = 1;/* Data 1 */ clk_lcd = 1; /* Clock Up E
7、dge */ clk_lcd = 0; /* Clock Down Edge */ s_data = 0;/ Data 0 */ clk_lcd = 1; / Clock Up Edge */ clk_lcd = 0; / Clock Down Edge */ s_data = 1;/* Data 1 / clk_lcd = 1; /* Clock Up Edge / s_data = 0; for(i=0; i6; i+) / Write First Address 00000(A5A0) */ clk_lcd = 0; /* Clock Down Edge / s_data = 0;/*
8、Data 0 / clk_lcd = 1; /Clock Up Edge */ for(i=0; i8; i+) / 8 Bits Display Data */ k=8; tmp = LCD_datai; tmp1 = tmp&0xf0; /* High 4 Bits */ tmp2 = tmp&0x0f; /* Low 4 Bits */ /* Data Bits Write Out From M to L / for(j=0; jk; j+) clk_lcd = 0; / Clock Down Edge */if(tmp & 0x80) /* Data Out / s_data = 1;
9、 else s_data = 0; clk_lcd = 1; / Clock up Edge */ tmp = tmp 1; /* Data 1 Bit */ lcd_cs = 1; /* LCD Chip Select Disable 8/* LCD Command Write */void LCD_CMD(unsigned char cmd) unsigned char data i; /* Write Command Control Word 100 */ lcd_cs = 0;/ Chip Enable */ Delay(100); clk_lcd = 0; /* Clock Up E
10、dge */ s_data = 1;/ Data 1 */ clk_lcd = 1; /* Clock Down Edge / clk_lcd = 0; /* Clock Up Edge */ s_data = 0;/* Data 0 / clk_lcd = 1; /* Clock Down Edge / clk_lcd = 0; / Clock Up Edge */ s_data = 0;/* Data 0 / clk_lcd = 1; / Clock Down Edge / / Write Command Word Bits From M to L,Add One Bit(hardware
11、 need) */ for(i=0; i9; i+) clk_lcd = 0; /* Clock Down Edge */ if(cmd & 0x80) s_data = 1; else s_data = 0;/* Data Write */ clk_lcd = 1; /* Clock Up Edge */ cmd = cmd 1; /* Command Data =5)con5=con5+1;if(con59)/ V0。9 V0.91 chang dispaly A error /con5=0;con4=con4+1;if(con49)con4=0;con3=con3+1;if(con39)
12、con3=0;con2=con2+1;if(con29)con2=0;con1=con1+1;/* V0。91 end */LCD_data7=/LCD_code;/* LCD Data */LCD_data6=LCD_codecon1;LCD_data5=LCD_codecon2;LCD_data4=LCD_codecon3+0x10;LCD_data3=LCD_codecon4;LCD_data2=LCD_codecon5;LCD_data1=0xcc;/LCD_codecom2;LCD_data0=0x8b;/LCD_codecom1;LCD_Display();/* Clear LCD
13、 */void Clear_LCD()LCD_data0 = 0x00; LCD_data1 = 0x00; LCD_data2 = 0x00; LCD_data3 = 0x00; LCD_data4 = 0x00; LCD_data5 = 0x00; LCD_data6 = 0x00; LCD_data7 = 0x00; LCD_data8 = 0x00; LCD_Display();/* LCD Inital */ void Inital_LCD(void) LCD_CMD(cmd_off); /* Close LCD System Oscillator / LCD_CMD(cmd_LCD
14、_off); / Close LCD Bisa Control */ LCD_CMD(cmd_LCD_RC); / Start internal RC Oscillator */ LCD_CMD(cmd_on); /* Open LCD System Oscillator / LCD_CMD(cmd_LCD_on); /* Open LCD Bisa Control / LCD_CMD(cmd_LCD_B3C4); /* Set Mode 1/3BAIS,1/4DUTY / /* Serial Port set */void Inital_Serial(void)SCON=0x52;/* Se
15、rial Mode 1 */TMOD=0x21;/* T1 Mode 2 */TH1=0xFD;TL1=0xFD;/* Baud 9600 */TR1=1;/* Start T1 */TI=1;/ Open Int T1 /EA=1;/* Open All Int */* LCD Check */LCD_Check()inti;for(i=0;i5;i+)Clear_LCD();led=1;Delay_ms(500);LCD_data7=0xFF;/ LCD Data = FF */LCD_data6=0xFF;LCD_data5=0xFF;LCD_data4=0xFF;LCD_data3=0
16、xFF;LCD_data2=0xFF;LCD_data1=0xFF;LCD_data0=0xFF;LCD_Display();led=0;Delay_ms(500);/* AD Inital */void AD_Set(void) ADCCON1=0x7C;/* Start ADC,Clock=CK/8,8Clock */ ADCCON2=0x21;/* serial ADC, Port 4 */ Delay(100);/* Main Program */void main(void)intData;inti,j,Channel;float Sum;float Temp;/* Inital S
17、et */Inital_Serial();Inital_LCD();LCD_Check();AD_Set();/* Start */while(1)Channel=ADCDATAH4;if(Channel=0x01)Sum=0;for(i=0;i100;i+)Data=ADCDATAL(ADCDATAH0x0F)8);/printf(”%dn,Data);Sum=Sum+Data;Sum=Sum/100。0;/Sum=1800。0;/ V0。91 - V0。92 ADD check Tab */if(Sum=Temp_100)for(i=0;i75;i+)if(Sum=ADC_Datai)j=i;Temp=Temp_Tabj+((Temp_Tabj+1-Temp_Tabj)*(ADC_Dataj-Sum)/(ADC_DatajADC_Dataj+1);/* V0。92 change end */elseTemp=1。0/((log(Sum/2250.0)/3470.0+(1.0/298.0)273.0;Delay(50);/Temp=101.372;printf(”temperature=。1fn,Temp);Delay_ms(900);Display(Temp);Delay(20);