Hi,
I do think that the example is not working at all!!!
try to compile and let me know if that is OK.
by default the sensor is expected on the port S3 but you can change it inside the htCompassDrv.h file.
my code for you :
1) fisrt main program must be saved as htCompass.c
//*!!CLICK to edit 'wizard' created sensor & motor configuration. !!*//
#include "htCompassDrv.h"
task main()
{
int x = 50000;
eraseDisplay();
bHT_COMPASS = true;
StartTask(HT_COMPASS);
while(x-- != 0)
{
nxtDisplayString(1,"Heading=%04d",HT_COMPASS_Heading_Data);
wait1Msec(5);
}
bHT_COMPASS = false;
StopAllTasks();
}
****************************************************
2) second program must be saved as htCompassDrv.h.
****************************************************
//* ********************************************************** *
//* *
//* ********************************************************** *
typedef enum
{
subTypeNone = 0,
subTypeHiTechnicCompass = 1,
subTypeHiTechnicRcxIR = 2,
subTypeMindsensorsCompass = 20,
subTypeMindsensorsRcxIR = 21,
subTypeMindsensorsPSX = 22,
subTypeMindsensorsMotorMux = 22,
} TSensorSubTypes;
//* ********************************************************** *
//* *
//* ********************************************************** *
bool bHT_COMPASS = false;
const tSensors HT_COMPASS_I2C_Port = S3;
int HT_COMPASS_Heading_Data;
//* ********************************************************** *
//* *
//* ********************************************************** *
typedef struct{
byte nMsgSize;
byte I2C_Bus_Addr;
byte I2C_RegIndex;
} TI2C_Output;
typedef struct{
byte nMsgSize;
byte I2C_Bus_Addr;
byte I2C_RegIndex;
byte I2C_Data;
} TI2C_Output_Data;
int I2C_Error = 0;
//* ********************************************************** *
//* *
//* ********************************************************** *
inline bool I2C_Convers_Data(const tSensors I2C_Port,const byte I2C_Bus_Addr ,const byte I2C_RegIndex, ubyte *I2C_Reply, int I2C_Reply_Length,const byte I2C_Data ,int nDelay)
{
TI2C_Output_Data I2C_Output;
int nI2C_BytesReady = 0;
while (true)
{
nI2C_BytesReady = 0;
I2C_Output.nMsgSize = 3;
I2C_Output.I2C_Bus_Addr = I2C_Bus_Addr;
I2C_Output.I2C_RegIndex = I2C_RegIndex;
I2C_Output.I2C_Data = I2C_Data;
memset(I2C_Reply,0x00,I2C_Reply_Length);
sendI2CMsg(I2C_Port,I2C_Output.nMsgSize,0);
while (nI2CStatus[I2C_Port] == STAT_COMM_PENDING) {wait1Msec(0);}
while (nI2CBytesReady[I2C_Port] > 0)
{
readI2CReply(I2C_Port, I2C_Reply,1);
}
nI2CBytesReady[I2C_Port] = 0;
nI2CRetries = 5;
sendI2CMsg(I2C_Port,I2C_Output.nMsgSize,I2C_Reply_Length);
if (nI2CStatus[I2C_Port] == ERR_COMM_BUS_ERR)
{
I2C_Error++;
return(false);
}
while (nI2CStatus[I2C_Port] == STAT_COMM_PENDING) {wait1Msec(0);}
if (nI2CStatus[I2C_Port] != NO_ERR)
{
I2C_Error++;
return(false);
}
while(nI2C_BytesReady < I2C_Reply_Length)
{
nI2C_BytesReady = nI2CBytesReady[I2C_Port];
wait1Msec(0);
}
readI2CReply(I2C_Port,I2C_Reply,I2C_Reply_Length);
if (nI2CStatus[I2C_Port] != NO_ERR)
{
I2C_Error++;
return(false);
}
break;
}
wait1Msec(nDelay);
return(true);
}
//* ********************************************************** *
//* *
//* ********************************************************** *
inline bool I2C_Convers(const tSensors I2C_Port,const byte I2C_Bus_Addr ,const byte I2C_RegIndex, ubyte *I2C_Reply, int I2C_Reply_Length, int nDelay)
{
TI2C_Output I2C_Output;
int nI2C_BytesReady = 0;
while (true)
{
nI2C_BytesReady = 0;
I2C_Output.nMsgSize = 2;
I2C_Output.I2C_Bus_Addr = I2C_Bus_Addr;
I2C_Output.I2C_RegIndex = I2C_RegIndex;
memset(I2C_Reply,0x00,I2C_Reply_Length);
sendI2CMsg(I2C_Port,I2C_Output.nMsgSize,0);
while (nI2CStatus[I2C_Port] == STAT_COMM_PENDING) {wait1Msec(0);}
while (nI2CBytesReady[I2C_Port] > 0)
{
readI2CReply(I2C_Port, I2C_Reply,1);
}
nI2CBytesReady[I2C_Port] = 0;
nI2CRetries = 5;
sendI2CMsg(I2C_Port,I2C_Output.nMsgSize,I2C_Reply_Length);
if (nI2CStatus[I2C_Port] == ERR_COMM_BUS_ERR)
{
I2C_Error++;
return(false);
}
while (nI2CStatus[I2C_Port] == STAT_COMM_PENDING) {wait1Msec(0);}
if (nI2CStatus[I2C_Port] != NO_ERR)
{
I2C_Error++;
return(false);
}
while(nI2C_BytesReady < I2C_Reply_Length)
{
nI2C_BytesReady = nI2CBytesReady[I2C_Port];
wait1Msec(0);
}
readI2CReply(I2C_Port,I2C_Reply,I2C_Reply_Length);
if (nI2CStatus[I2C_Port] != NO_ERR)
{
I2C_Error++;
return(false);
}
break;
}
wait1Msec(nDelay);
return(true);
}
//* ********************************************************** *
//* *
//* ********************************************************** *
task HT_COMPASS()
{
byte I2C_Bus_Addr = 0x02;
int Heading_Data_Tmp = 0;
ubyte I2C_Reply_Msg_LSB_MSB[6];
ubyte I2C_Reply_Software_Version[8];
ubyte I2C_Reply_Vendor_ID[8];
ubyte I2C_Reply_Sensor_Module_Type[8];
nSchedulePriority = kDefaultTaskPriority;
SensorType[HT_COMPASS_I2C_Port] = sensorI2CCustomFast;
SensorSubType[HT_COMPASS_I2C_Port] = subTypeHiTechnicCompass;
SensorMode[HT_COMPASS_I2C_Port] = modeRaw;
I2C_Convers(HT_COMPASS_I2C_Port,I2C_Bus_Addr ,(const byte)0x00,I2C_Reply_Software_Version,8,50);
I2C_Convers(HT_COMPASS_I2C_Port,I2C_Bus_Addr ,(const byte)0x08,I2C_Reply_Vendor_ID,8,50);
I2C_Convers(HT_COMPASS_I2C_Port,I2C_Bus_Addr ,(const byte)0x10,I2C_Reply_Sensor_Module_Type,8,50);
I2C_Convers_Data(HT_COMPASS_I2C_Port,I2C_Bus_Addr ,(const byte)0x41,I2C_Reply_Msg_LSB_MSB,1,0x00,50);
while (bHT_COMPASS) {
if (!I2C_Convers(HT_COMPASS_I2C_Port,I2C_Bus_Addr ,(const byte)0x44,I2C_Reply_Msg_LSB_MSB,2,0)) continue;
Heading_Data_Tmp = ((uword)((0xFF & I2C_Reply_Msg_LSB_MSB[1]) <<

+ (uword) (0xFF & I2C_Reply_Msg_LSB_MSB[0]));
HT_COMPASS_Heading_Data = Heading_Data_Tmp;
SensorValue[HT_COMPASS_I2C_Port] = Heading_Data_Tmp;
wait1Msec(4);
}
return;
}