问题如下:
问题出现在I2C读MAX127转换数据的时候只能读一次,哪位帮我看看问题出现在哪里?
谢谢!我是用proteus仿真的。
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
const uchar tab[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f
};
const uchar tab1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef};//带小数的真值表
#define SDA RC4
#define SCL RC3
#define ADD 0X50 //器件地址
#define COMM 0x80 //器件的控制字节
void Init(void)
{
TRISB=0X00;
PORTB=0XFF;
TRISA=0X00;
TRISA=0X00;
ADCON1=0X06;
TRISC|=0X18;
SSPSTAT=0X80;
SSPCON=0X28;
SSPCON2=0X00;
SSPADD=0X09;
SSPIF=0;
}
void delayms(uchar ms)
{
uchar x;
for(;ms>0;ms--)
for(x=110;x>0;x--);
}
void Start(void)
{
SEN=1;
while(SEN);
}
void Stop(void)
{
PEN=1;
while(PEN);
}
void Rstart(void)
{
RSEN=1;
while(RSEN);
}
uchar IIC_Send(uchar dat)
{
SSPBUF=dat;
while(RW);
return(!ACKSTAT);
}
void Write_Max127(void)
{
Start();
IIC_Send(ADD);
IIC_Send(COMM);
Stop();
}
uchar IIC_Read(ack)
{
uchar data;
RCEN=1;
while(RCEN);
data=SSPBUF;
if(ack)
{
ACKDT=0;
}
else
{
ACKDT=1;
}
ACKEN=1;
while(ACKEN);
return data;
}
uint Read_Max127(void)
{
uint adslut;
uchar dath,datl;
IIC_Send(0x51); //开始读
dath=IIC_Read(0);
datl=IIC_Read(0);
adslut=dath;
adslut<<=8;
adslut|=datl;
adslut>>=4;
Stop();
return adslut;
}
void disp(uchar q,uchar b,uchar s,uchar g)
{
PORTA=0XFE;
PORTB=tab1[q];
delayms(5);
PORTA=0XFd;
PORTB=tab;
delayms(5);
PORTA=0XFb;
PORTB=tab;
delayms(5);
PORTA=0XF7;
PORTB=tab[g];
delayms(5);
}
void main(void)
{
uchar a,b,c,d;
uint Get_adslut;
Init();
Write_Max127();
while(1)
{
Get_adslut=Read_Max127();
a=Get_adslut/1000;
b=Get_adslut%1000/100;
c=Get_adslut%100/10;
d=Get_adslut%10;
disp(a,b,c,d);
}
} |