我们都知道IIC设备在启动信号以后就可以向IIC设备里写一个字节,然后等待IIC设备把SDA拉低,因此可以根据这一点就可以知道IIC的地址是多少,淘宝上买的好多IIC的东西,告诉你的地址并不准确,比如我就遇到过买的PCF8574, 资料里显示地址是0x27,可是怎么也不对,把A0,A1,A2都接地地址换成0X20还是不对,因此我就想到让单片机来完成找地址的工作。单片机是STC的1T单片机,晶振11M,关键代码是:
for(i=0;i<0xff;i++)
{
IICstart();
if(Write1Byte(i)==1)
{
_itoa(i,string,16);//把整数转变成字符串的函数
UartPrintf(string);//串口发送字符串
}
}
itoa函数在下面,我从ZIGBEE的协议栈里拷贝的,很好用
/*********************************************************************
* @fn _itoa
*
* @brief convert a 16bit number to ASCII
*
* @param num -
* buf -
* radix -
*
* @return void
*
*********************************************************************/
void _itoa(uint16 num, uint8 *buf, uint8 radix)
{
char c,i;
uint8 *p, rst[5];
p = rst;
for ( i=0; i<5; i++,p++ )
{
c = num % radix; // Isolate a digit
*p = c + (( c < 10 ) ? '0' : '7'); // Convert to Ascii
num /= radix;
if ( !num )
break;
}
for ( c=0 ; c<=i; c++ )
*buf++ = *p--; // Reverse character order
*buf = '\0';
}
|