温度传感器的单片机处理显示程序
//
// //
// 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 70~100C //
// //
//******************************************************//
#include "ADUC812.h"
#include "math.h"
#include "intrins.h"
#include "string.h"
#include "stdio.h"
#include "dada.h"
bdata unsigned char LCD_data[8]; //** Display Buffer **//
int ssdata[3];
#define Temp_100 235
#define Temp_40 1352
sbit s_data = P2^0; //** LCD Serial Data Port **//
sbit clk_lcd = P2^2; //** LCD Clock Port **//
sbit lcd_cs = P2^1; //** LCD Chip Select Port **//
sbit led =P2^7;
//********** LCD Command Name Define ***********//
#define cmd_off 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_buff[16]; //** 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,0x8e,
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)
{
int i,j;
for(i=0;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 Edge **//
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; i<6; i++) //** Write First Address '00000'(A5-A0) **//
{
clk_lcd = 0; //** Clock Down Edge **//
s_data = 0; //** Data '0' **//
clk_lcd = 1; //**Clock Up Edge **//
}
for(i=0; i<8; i++) //** 8 Bits Display Data **//
{
k=8;
tmp = LCD_data[i];
tmp1 = tmp&0xf0; //** High 4 Bits **//
tmp2 = tmp&0x0f; //** Low 4 Bits **//
//** Data Bits Write Out From M to L **//
for(j=0; j
clk_lcd = 0; //** Clock Down Edge **//
if(tmp & 0x80) //** Data Out **//
s_data = 1;
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 Edge **//
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 need) **//
for(i=0; i<9; 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 <<1 Bit **//
}
lcd_cs = 1; //** Chip Select disable **//
}
//******** Display *****************//
void Display(float TT)
{
int con1=0,con2=0,con3=0,con4=0,con5=0,con6=0,dd=0,ss=0;
long tt6;
dd=TT*100;
if(dd<0)
{
dd=abs(dd);
LCD_data[7]=0x04; //** +&- **//
}
else
{
LCD_data[7]=0x00;
}
ss=dd;
con5=ss%10;
ss=dd/10;
con4=ss%10;
ss=dd/100;
con3=ss%10;
ss=dd/1000;
con2=ss%10;
ss=dd/10000;
con1=ss%10;
tt6=TT*1000;
dd=tt6%10;
if(dd>=5)
{
con5=con5+1;
if(con5>9) //** V0.9 -> V0.91 chang dispaly A error **//
{
con5=0;
con4=con4+1;
if(con4>9)
{
con4=0;
con3=con3+1;
if(con3>9)
{
con3=0;
con2=con2+1;
if(con2>9)
{
con2=0;
con1=con1+1;
}
}
}
} //*** V0.91 end ******//
}
//LCD_data[7]=//LCD_code[]; //** LCD Data **//
LCD_data[6]=LCD_code[con1];
LCD_data[5]=LCD_code[con2];
LCD_data[4]=LCD_code[con3+0x10];
LCD_data[3]=LCD_code[con4];
LCD_data[2]=LCD_code[con5];
LCD_data[1]=0xcc;//LCD_code[com2];
LCD_data[0]=0x8b;//LCD_code[com1];
LCD_Display();
}
//*************** Clear LCD ********************//
void Clear_LCD()
{
LCD_data[0] = 0x00;
LCD_data[1] = 0x00;
LCD_data[2] = 0x00;
LCD_data[3] = 0x00;
LCD_data[4] = 0x00;
LCD_data[5] = 0x00;
LCD_data[6] = 0x00;
LCD_data[7] = 0x00;
LCD_data[8] = 0x00;
LCD_Display();
}
//***************** LCD Inital *****************//
void Inital_LCD(void)
{
LCD_CMD(cmd_off); //** Close LCD System Oscillator **//
LCD_CMD(cmd_LCD_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; //** Serial 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()
{
int i;
for(i=0;i<5;i++)
{
Clear_LCD();
led=1;
Delay_ms(500);
LCD_data[7]=0xFF; //** LCD Data = FF **//
LCD_data[6]=0xFF;
LCD_data[5]=0xFF;
LCD_data[4]=0xFF;
LCD_data[3]=0xFF;
LCD_data[2]=0xFF;
LCD_data[1]=0xFF;
LCD_data[0]=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)
{
int Data;
int i,j,Channel;
float Sum;
float Temp;
//****** Inital Set *****//
Inital_Serial();
Inital_LCD();
LCD_Check();
AD_Set();
//******* Start *************//
while(1)
{
Channel=ADCDATAH>>4;
if(Channel==0x01)
{
Sum=0;
for(i=0;i<100;i++)
{
Data=ADCDATAL|((ADCDATAH&0x0F)<<8);
// printf("%d\n",Data);
Sum=Sum+Data;
}
Sum=Sum/100.0;
// Sum=1800.0; //*** V0.91 -> V0.92 ADD check Tab **//
if(Sum<=Temp_40)
{
if(Sum>=Temp_100)
{
for(i=0;i<75;i++)
{
if(Sum<=ADC_Data[i])
{
j=i;
}
}
Temp=Temp_Tab[j]+((Temp_Tab[j+1]-Temp_Tab[j])*(ADC_Data[j]-Sum)/(ADC_Data[j]-ADC_Data[j+1]));
} //** V0.92 change end **//
}
else
{
Temp=1.0/((log(Sum/2250.0))/3470.0+(1.0/298.0))-273.0;
}
Delay(50);
}
//Temp=101.372;
printf("temperature=%.1f\n",Temp);
Delay_ms(900);
Display(Temp);
Delay(20);
}
}
非常好我支持^.^
(2) 100%
不好我反对
(0) 0%