RobotC掲示板 8076

RobotCを使っている皆さん、使っていてわからないところがある皆さん、是非ここで情報交換してください!! 因みに、管理者TKのロボブログです。 http://blog.livedoor.jp/tk_robot_drs345/ 又、中傷等のコメントは論外、削除させていただきます。 よろしくお願いいたします。


I2C

1:管理者・TK :

2011/02/17 (Thu) 21:16:37

どうも。。TKです。。
この掲示板も早速過疎化が進んでたりしますが、よろしくお願いします。。
多分あまりいないとは思うのですが、I2C系のプログラムが分かる方いらっしゃいますか?
RobotCForumを見ても、よくわからないのですが。。
よろしくお願いします。。
2:MOMONGA :

2011/06/21 (Tue) 14:36:06

私も少しは、わかりますが1つだけ分からないのは、I2Cアドレスを変える方法です。
どうすればいいですか。
3:管理者・TK :

2011/06/23 (Thu) 21:43:16

はじめまして、TKと申します。

I2Cアドレスを変更するプログラムなら、ありますよ。

//////////////////////////////////////////////////////////////////////////////
//
// change the adress of the I2C sensor oldAddresswith the NewAddress
//
/////////////////////////////////////////////////////////////////////////////

void Change_add(byte oldAddress,byte newAddress)
{
byte i2cMsg[8];
const byte kMsgSize = 0;
const byte kDeviceAddress = 1;
const byte kCommandAddress = 2;
const byte kCommand = 3;

nI2CBytesReady[S2] = 0;
SensorType[S2] = sensorI2CMindsensorsDist;

// Build the I2C message
i2cMsg[kMsgSize] = 3;
i2cMsg[kDeviceAddress] = oldAddress ;
i2cMsg[kCommandAddress] =0x41;

i2cMsg[kCommand]=0xA0;
while (nI2CStatus[S2] == STAT_COMM_PENDING); // Wait till I2C bus is ready
sendI2CMsg(S2, i2cMsg[0], 0); // Send the message
wait10Msec(10);

i2cMsg[kCommand]=0xAA;
while (nI2CStatus[S2] == STAT_COMM_PENDING); // Wait till I2C bus is ready
sendI2CMsg(S2, i2cMsg[0], 0); // Send the message
wait10Msec(10);

i2cMsg[kCommand]=0xA5;
while (nI2CStatus[S2] == STAT_COMM_PENDING); // Wait till I2C bus is ready
sendI2CMsg(S2, i2cMsg[0], 0); // Send the message
wait10Msec(10);

i2cMsg[kCommand]=newAddress;
while (nI2CStatus[S2] == STAT_COMM_PENDING); // Wait till I2C bus is ready
sendI2CMsg(S2, i2cMsg[0], 0); // Send the message
wait10Msec(10);

}

//////////////////////////////////////////////////////////////////////////////
//
// demo main function to show change adress function
//
/////////////////////////////////////////////////////////////////////////////

task main()
{
nxtDisplayTextLine(1, "mindsensors.com");
nxtDisplayTextLine(2, "address change");

Change_add(0xa8,0x04);

StopAllTasks();
}

ここの、Change_addに、(今のI2Cアドレス,変更後のI2Cアドレス)
というようにすれば、アドレスが変わります。
4:管理者・TK :

2011/06/23 (Thu) 21:45:40

因みに、ポートスプリッター経由でポート一つに複数のセンサーをつけた時に、そのセンサー名とI2Cアドレスを表示させるプログラムもあります。

#define i2cScanVersion 0x00
#define i2cScanDeviceName 0x10
#define i2cScanPort S2 // Connect sensor to this port!!

byte replyMsg[10];

//////////////////////////////////////////////////////////////////////////////
//
// Read the data from I2C sensor
//
/////////////////////////////////////////////////////////////////////////////

byte I2Cscan(byte ScanID )
{

byte i2cscanMsg[4];
const byte MsgSize = 0;
const byte i2cScanAddress = 1;
const byte ReadAddress = 2;

// Build the I2C message
i2cscanMsg[MsgSize] = 2;
i2cscanMsg[i2cScanAddress] = ScanID ;
i2cscanMsg[ReadAddress] =i2cScanDeviceName ;

replyMsg[0]=0x00;

while (nI2CStatus[i2cScanPort] == STAT_COMM_PENDING); // Wait for I2C bus to be ready
sendI2CMsg(i2cScanPort, i2cscanMsg[0], 8); // Send the message
if(nI2CStatus[i2cScanPort] == ERR_COMM_BUS_ERR)return 0;
while (nI2CStatus[i2cScanPort] == STAT_COMM_PENDING); // Wait for I2C bus to be ready
readI2CReply(i2cScanPort, replyMsg[0], 8); // read all the six data bytes
if(replyMsg[0]==0x00) return 0;

return 1;
}

//////////////////////////////////////////////////////////////////////////////
//
// scans the port 1 and Display the results on NXT display
//
/////////////////////////////////////////////////////////////////////////////

task main()
{
int i2cScanID;
ubyte count;
string s2;
string s1;
int r, i;

nI2CBytesReady[i2cScanPort] = 0;
SensorType[i2cScanPort] = sensorI2CCustom9V;
nxtDisplayTextLine(0,"mindsensors.com");
nxtDisplayTextLine(1,"Name: i2cID(hex)");
nxtDisplayTextLine(2,"nothing found?");
i2cScanID = 0x02;
count=1;
while(i2cScanID<0xfe)
{
if(I2Cscan(i2cScanID)==1)
{
count++;
i = 0;
s1 = "";
while ( replyMsg[i] != 0x00 ) {
r = replyMsg[i];
StringFormat (s2, "%s%c", s1, r);
s1 = s2;
i++;
}
StringFormat (s2, "%s: %x", s1, i2cScanID);
nxtDisplayTextLine(count,"%s", s2);
}
i2cScanID += 0x02;
}
wait10Msec(1000);
StopAllTasks();
}

  • 名前: E-mail(省略可):
  • 画像:

Copyright © 1999- FC2, inc All Rights Reserved.