本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
我的AD模块是PCF8591,我想在LCD1602上显示,但是怎么都调试不对,虽然出现了现象,但是仔细观察就会发现,1602显示数据是错误的。我实在是不知道哪出问题了。
************************
下面是我的程序
************************
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCDWR=P2^5; //WR=RW
sbit LCDRD=P2^6; //RD=RS
sbit LCDEN=P2^7;
sbit scl=P2^1;
sbit sda=P2^0;
bit ack;
uchar code display1[]={"Current AD"};
uchar code display2[]={"0123456789"};
void delay()
{
uchar a,b;
for(a=1;a>0;a--)
for(b=1;b>0;b--);
}
void delayms(uint a) //延时程序
{
uint b,c;
for(b=a;b>0;b--)
for(c=10;c>0;c--);
}
void write_zhiling(uchar zhiling) //1602写指令
{
LCDRD=0;
LCDWR=0;
P0=zhiling;
LCDEN=1;
delayms(25);
LCDEN=0;
}
void write_shuju1(uchar a) //1602写数据
{
LCDRD=1;
LCDWR=0;
P0=display1[a];
LCDEN=1;
delayms(25);
LCDEN=0;
}
void write_shuju2(uchar a)
{
LCDRD=1;
LCDWR=0;
P0=a;
LCDEN=1;
delayms(25);
LCDEN=0;
}
void write_charlist(uchar cX,uchar cY)
{
uchar a=0;
if(cY==1)
{
cX=cX|0x80;
}
else
{
cX=cX|0x80|0x40;
}
while(display1[a]!='\0')
{
write_zhiling(cX+a);
write_shuju1(a);
a++;
}
}
void write_char(uchar cX,uchar cY,uchar b)
{
if(cY==1)
{
cX=cX|0x80;
}
else
{
cX=cX|0x80|0x40;
}
write_zhiling(cX);
write_shuju2(b);
}
void start(void)
{
delayms(15);
write_zhiling(0x38);
delayms(5);
write_zhiling(0x38);
delayms(5);
write_zhiling(0x38);
write_zhiling(0x38);
write_zhiling(0x08);
write_zhiling(0x01);
write_zhiling(0x06);
write_zhiling(0x0c);
}
void Start_I2c() //I2c启动程序
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
scl=0;
delay();
}
void Stop_I2c() //I2c停止程序
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
scl=0;
delay();
}
void Respons_I2c() //I2c回应程序
{
uchar i;
scl=0;
delay();
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
while((sda==1)&&(i<255))
i++;
scl=0;
delay();
sda=1;
delay();
}
void N_Respons_I2c() //I2c回应程序
{
scl=0;
// delay();
sda=0;
// delay();
scl=1;
// delay();
sda=1;
delay();
scl=0;
delay();
sda=0;
delay();
}
void init() //I2c初始化程序
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar a) //I2c写入程序
{
uchar i,temp;
temp=a;
for(i=0;i<8;i++)
{
if(temp&0x80)
sda=1;
else
sda=0;
temp=temp<<1;
scl=1;
delay();
scl=0;
delay();
}
sda=1;
delay();
if(sda==1)
ack=0;
else
ack=1;
scl=0;
delay();
}
unsigned char Read_byte() //I2c读程序
{
uchar i,j,k;
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=0;
delay();
j=sda;
k=(k<<1)|j;
scl=0;
delay();
}
return k;
}
unsigned char Read_PCF8591()
{
uchar num;
Start_I2c();
write_byte(0x90);
Respons_I2c();
write_byte(0x40);
Respons_I2c();
Start_I2c();
write_byte(0x91);
Respons_I2c();
num=Read_byte();
N_Respons_I2c();
Stop_I2c();
return num;
}
void main()
{
uchar num;
uchar a,b,c;
uchar q,m,n;
start();
init();
while(1)
{
num=Read_PCF8591();
a=num/51;
b=(num%51)*10/51;
c=((num%51)*10%51)*10/51;
write_charlist(2,1);
write_char(13,2,0x30+c);
write_char(12,2,0x30+b);
write_char(10,2,0x30+a);
write_char(11,2,'.');
q=num/100;
m=(num%100)*10/100;
n=num/10;
write_char(6,2,0x30+n);
write_char(5,2,0x30+m);
write_char(4,2,0x30+q);
delayms(50);
}
}
*******************************
显示结果能够出现数据,但是数据转换关系之间是错误的,并且还有几个是乱码。我感觉我写的已经没有错了,不知道是什么原因了。
希望大家可以帮帮我。 |
|