1楼
cnrtu 发表于:2007-10-11 17:12:04
#include "SM89516A.h"
sbit SCL = P1^0;
sbit SDA = P1^1;
void Delay( void )
{
unsigned char i = 10;
while(i--);
}
void IIC_Start( void )
{
SDA = 1;
SCL = 1;
Delay( );
SDA = 0;
Delay( );
SCL = 0;
}
void IIC_Stop( void )
{
SDA = 0;
SCL = 1;
Delay( );
SDA = 1;
Delay( );
SCL = 0;
}
void IIC_ACK( void )
{y型钢
SDA = 0;
SCL = 1;
Delay( );
// SDA = 1;
SCL = 0;
}
void IIC_NACK( void )
{
SDA = 1;
SCL = 1;
Delay( );
// SDA = 0;
SCL = 0;
}
bit IIC_WriteByte(unsigned char Byte)
{
unsigned char i;
bit ack;
for( i=0; i<8; i++ )
{
if( ( Byte << i ) & 0x80 )
{
IIC_NACK( );
}
else
{
IIC_ACK( );
}
}
SDA = 1;
SCL = 1;
Delay( );
Delay( );
SCL =0;
return( ack );
}
unsigned char IIC_ReadByte( void )
{
register char i, Byte = 0;
for( i=0; i<8; i++)
{
SDA = 1;
SCL = 1;
Delay( );
Byte = Byte<<1;
{
Byte |= 0x01;
}
else
{
Byte &= 0xfe;
}
SCL = 0; 旋压皮带轮
Delay( );
}
return Byte;
}
void IIC_WriteData( unsigned char nChip, unsigned int WAddress, unsigned char *WData, unsigned int Count )
{
unsigned int Len;
IIC_Start( );
if( IIC_WriteByte(0xA0+(nChip<<1)) )
{
return;
}
if( IIC_WriteByte( (unsigned char )WAddress / 256 ) )
{
return;
}
if( IIC_WriteByte( (unsigned char )WAddress % 256 ) )
{
return;
}
for( Len=0; Len<Count; Len++ )
{
if( IIC_WriteByte(WData[Len]) )
{
return;
防屏蔽 }
}
P0_3 = !P0_3;
IIC_Stop( );
}
void IIC_ReadData( unsigned char nChip, unsigned int RAddress, unsigned char *RData, unsigned int Count )
{
unsigned int Len;
IIC_Start( );
if( IIC_WriteByte( 0xA0+(nChip<<1) ) )
{
return;
}