这个是代码 不知道哪边有问题.求指点.void write_add(uchar slave,uchar reg,uchar date)
{
Start();
write_byte(slave&0xFE);
ACK();
write_byte(reg);
ACK();
write_byte(date);
ACK();
Stop();
}
uchar read_add(uchar slave,uchar reg)
{
uchar date;
Start();
write_byte(slave&0xFE);
ACK();
write_byte(reg);
ACK();
Start();
write_byte(slave | 0x01);
EACK();
date=read_byte();
Stop();
return date;
}
void Start(void)
{
SDA_H();
delay(5);
SCL_H();
delay(5);
SCL_L();
}
void Stop(void)
{
SDA_L();
delay(5);
SCL_H();
delay(5);
SDA_H();
}
void init(void)
{
SDA_H();
delay(5);
SCL_H();
delay(5);
}
void SDA_H(void)
{
unsigned long int temp;
temp = inpd(GPIO_Add + GP_LVL2 );
outpd(GPIO_Add + GP_LVL2, temp | 0x40);
}
void SDA_L(void)
{
unsigned long int temp;
temp = inpd(GPIO_Add + GP_LVL2 );
outpd(GPIO_Add + GP_LVL2, temp &(~0x40));
}
void SCL_H(void)
{
unsigned long int temp;
temp = inpd(GPIO_Add + GP_LVL2 );
outpd(GPIO_Add + GP_LVL2, temp | 0x80);
}
void SCL_L(void)
{
unsigned long int temp;
temp = inpd(GPIO_Add + GP_LVL2 );
outpd(GPIO_Add + GP_LVL2, temp &(~0x80));
}
uchar SDA_S(void)
{
unsigned long int temp;
temp = inpd(GPIO_Add + GP_LVL2 );
if( (temp & 0x40) == 0x40 )
{
return 1;
}
else
return 0;
}
uchar SCL_S(void)
{
unsigned long int temp;
temp = inpd(GPIO_Add + GP_LVL2 );
if( (temp & 0x80) == 0x80 )
{
return 1;
}
else
return 0;
}
void ACK(void)
{
SDA_H();
delay(5);
SCL_H();
delay(5);
SDA_L();
}
void EACK(void)
{
SDA_L();
delay(5);
SCL_H();
delay(5);
SCL_L();
delay(5);
}
void write_byte(uchar date)
{
uchar i;
for(i=0;i<8;i++)
{
SCL_H();
delay(5);
if(date &0x80)
SDA_H();
else
SDA_L();
delay(5);
date<<=1;
SCL_L();
}
delay(5);
}
uchar read_byte(void)
{
uchar i,k=0;
for(i=0;i<8;i++)
{
SCL_L();
delay(5);
SCL_H();
delay(5);
k<<=1;
if(SDA_S() == 1)
k+=1;
delay(5);
SCL_L();
delay(5);
}
return k;
}
|