#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code lcd[]="tension is:";
uchar code lcd1[]=" V";
uchar dis[5];
sbit CS=P2^0;
sbit CLK=P2^1;
sbit DataOut=P2^2;
sbit lcd_rs=P3^2;
sbit lcd_rw=P3^1;
sbit lcd_en=P3^0;
uchar num,num1;
float aver;
void delayms(uint z) //延时ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)//lcd写指令控制命令
{
delayms(10);
lcd_rs=0;
lcd_rw=0;
P1=com;
lcd_en=1;
delayms(5);
lcd_en=0;
delayms(5);
}
void write_data(uchar date)//lcd写数据命令
{
delayms(10);
lcd_rs=1;
lcd_rw=0;
P1=date;
delayms(10);
lcd_en=1;
delayms(10);
lcd_en=0;
}
void init()
{
delayms(15);
write_com(0x38);//设置lcd显示模式
delayms(5);
write_com(0x38);
delayms(5);
write_com(0x38);
write_com(0x0c);//设置光标的显示/闪烁
write_com(0x06);//设置显示后指针加1,并且光标移一位
write_com(0x01);//清屏设置
write_com(0x80);//设置字符从地址为10的位置开始显示
}
void init_play() //初始化显示
{
init();
for(num=0;num<11;num++)
{
write_data(lcd[num]);
delayms(20);
}
write_com(0x80+0x40);
for(num=0;num<7;num++)
{
write_data(lcd1[num]);
delayms(20);
}
}
uchar TLC549_ADC()
{
uchar i,temp;
CS=1;
CLK=0;
CS=0;
_nop_();
_nop_();
for(i=0;i<8;i++)
{
temp<<=1;
temp|=DataOut; //相或
CLK=1;
_nop_();
CLK=0;
}
CS=1;
for(i=17;i!=0;i--)_nop_();
return temp;
}
uchar average()
{
uint a[30];
uint i,j,t;
uchar T1;
float sum=0;
for(i=0;i<30;i++)
{
T1=TLC549_ADC();
num1=((float)T1/256)*5000; //电压转换扩大1000倍
a[i]=num1;
}
for(j=0;j<30;j++) //用起泡法对30个数进行排序
for(i=0;i<30-j;i++)
if(a[i]>a[i+1])
{
t=a[i];
a[i]=a[i+1];
a[i+1]=t;
}
for(i=5;i<25;i++) //取中间20个数的平均值
sum=sum+a[i];
aver=sum/20;
return aver;
}
void display()
{
uchar aver1;
aver1=average();
dis[0]=aver1/1000+0x30;
dis[1]=aver1/100%10+0x30;
dis[2]=aver1/10%10+0x30;
dis[3]=aver1%10+0x30;
write_com(0xc1); //显示
write_data(dis[0]);
write_com(0xc2);
write_data(dis[1]);
write_com(0xc3);
write_data(0x2e);
write_com(0xc4);
write_data(dis[2]);
write_com(0xc5);
write_data(dis[3]);
}
void main()
{
init_play();
while(1)
{
// TLC549_ADC();
display();
}
}
用lcd显示一个TLC549A/D转换数字电压表,但是lcd显示时,只有显示1.16而且也调不了,请高手帮忙解答 |