原标题:pt100测温程序-LCD1602
#include"at89X52.h"
sbit LCD_RS =P2^0;
sbit LCD_RW =P2^1;
sbit LCD_E =P2^2;
sbit ADC_CS =P2^3;
sbit ADC_WR =P3^6;
sbit ADC_RD =P3^7;
#define LCD_DATA P0
unsigned char LcdBuf1[10]="";
unsigned char code Bmp001[][8]=
{
{0x06,0x09,0x09,0x06,0x00,0x00,0x00,0x00},
{0x06,0x09,0x10,0x10,0x10,0x09,0x06,0x00}
};
void dellay(unsigned int h)
{
while(h--); //0.01MS
}
void WriteDataLcd(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS=1;
LCD_RW=0;
LCD_E=0;
dellay(1000);
LCD_E=1;
}
void WriteCommandLcd(unsigned char wdata)
{
LCD_DATA=wdata;
LCD_RS=0;
LCD_RW=0;
LCD_E=0;
dellay(1000);
LCD_E=1;
}
void lcd_init(void)
{
LCD_DATA=0;
WriteCommandLcd(0x38);
dellay(1000);
WriteCommandLcd(0x38);
dellay(1000);
WriteCommandLcd(0x01);
WriteCommandLcd(0x0c);
}
void display_xy(unsigned char x,unsigned char y)
{
if(y==1)
x+=0x40;
x+=0x80;
WriteCommandLcd(x);
}
void display_string(unsigned char x,unsigned char y,unsigned char *s)
{
display_xy(x,y);
while(*s)
{
WriteDataLcd(*s);
s++;
}
}
void Write_CGRAM(unsigned char add,unsigned char *char_num)
{
unsigned i;
add=add<<3;
WriteCommandLcd(0x40|add+8);
for(i=0;i<8;i++)
{
WriteDataLcd(*char_num++);
}
}
void delayms()
{
int i;
for(i=110;i>0;i--);
}
void delay1s()
{
int i,j;
for(i=1000;i>0;i--);
for(j=110;j>0;j--);
}
void convert()
{
unsigned long value;
char i;
unsigned long res;
int temp,temp2;
int w;
P1=0xff;
ADC_RD=0;
for(i=0;i<10;i++);
value=P1;
res=(float)(2550000+110000*value/30)/(2550-11*value/30);
temp=(float)(res*10000-10000000)/3851-545;
temp2 = temp;
if(temp >= 0)
{
for(i=6;temp>0;i--)
{
LcdBuf1[i]=temp%10+48;
temp/=10;
}
}
if(temp < 0 )
{
w= -temp;
for(i=6;w>0;i--)
{
LcdBuf1[i]=w%10+48;
w/=10;
}
}
if(i>=0&&temp<0)
{
if(temp > -10)
{
LcdBuf1[i] = '0';
i--;
}
LcdBuf1[i] = '-';
i--;
/* if(i==5)
{
LcdBuf1[5] = '0';
i --;
}
LcdBuf1[4] = '-';
i--; */
}
for(;i>=0;i--)
LcdBuf1[i]=' ';
for(i=0;i<5;i++)
LcdBuf1[i]=LcdBuf1[i+1];
if(LcdBuf1[4]==' ')LcdBuf1[4]='0';
LcdBuf1[5]='.';
LcdBuf1[7]=1;
LcdBuf1[8]='C';
WriteCommandLcd(0x84);
display_string(3,0,LcdBuf1);
ADC_RD=1;
ADC_WR=0;
for(i=0;i<10;i++)
ADC_WR=1;
delayms();
}
main()
{
unsigned char i;
lcd_init();
for(i=0;i<2;i++)
Write_CGRAM(i,Bmp001[i]);
ADC_CS=0;
ADC_WR=0;
for(i=0;i<10;i++);
ADC_WR=1;
while(1)
{
convert();
delay1s();
}
}
责任编辑: