NXC is a C-like programming language that can access all the features of this sensor.
For more information go to http://bricxcc.sourceforge.net/nbc/.
Example NXC Code
//=====================================================================
// HiTechnic - Angle Sensor Sample Program
//
#define ANGLE S1
//=====================================================================
// ReadSensorHTAngle(port, Angle, AccAngle, RPM)
// Reads the HiTechnic Angle Sensor and returns the current:
// Angle degrees (0-359)
// Accumulated Angle degrees (-2147483648 to 2147483647)
// RPM rotations per minute (-1000 to 1000)
void ReadSensorHTAngle(int port, int &Angle, long &AccAngle, int &RPM)
{
int count;
byte cmndbuf[] = {0x02, 0x42}; // I2C device, register address
byte respbuf[]; // Response Buffer
bool fSuccess;
count=8; // 8 bytes to read
fSuccess = I2CBytes(port, cmndbuf, count, respbuf);
if (fSuccess) {
Angle = respbuf[0]*2 + respbuf[1];
AccAngle = respbuf[2]*0x1000000 + respbuf[3]*0x10000+
respbuf[4]*0x100 + respbuf[5];
RPM = respbuf[6]*0x100 + respbuf[7];
} else {
// No data from sensor
Angle = 0; AccAngle = 0; RPM = 0;
}
}
// ReserSensorHTAnalog(port, resetmode)
// resetmode:
// HTANGLE_MODE_CALIBRATE Calibrate the zero position of angle.
// Zero position is saved in EEPROM on sensor.
// HTANGLE_MODE_RESET Reset the rotation count of accumulated.
// angle to zero. Not saved in EEPORM.
#define HTANGLE_MODE_CALIBRATE 0x43
#define HTANGLE_MODE_RESET 0x52
void ResetSensorHTAngle(int port, int resetmode)
{
int count;
byte cmndbuf[] = {0x02, 0x41, 0}; // I2C device, register address
byte respbuf[]; // buffer for inbound I2C response
cmndbuf[2] = resetmode; // Set reset code
count=0; // 0 bytes to read
I2CBytes(port, cmndbuf, count, respbuf);
if (resetmode == HTANGLE_MODE_CALIBRATE)
Wait(50); // Time to allow burning EEPROM
}
//=====================================================================
task main()
{
int angle;
long acc_angle;
int rpm;
SetSensorLowspeed(ANGLE);
Wait(100);
TextOut(0, LCD_LINE1, "HiTechnic");
TextOut(0, LCD_LINE2, " Angle Sensor");
TextOut(0, LCD_LINE8, "");
while(true) {
Wait(100);
ReadSensorHTAngle(ANGLE, angle, acc_angle, rpm);
TextOut(0, LCD_LINE4, "Angle : ");
NumOut(7*6, LCD_LINE4, angle);
TextOut(0, LCD_LINE5, "AccAng: ");
NumOut(7*6, LCD_LINE5, acc_angle);
TextOut(0, LCD_LINE6, "RPM : ");
NumOut(7*6, LCD_LINE6, rpm);
if (ButtonPressed(BTNLEFT,0)) {
while(ButtonPressed(BTNLEFT,0));
ResetSensorHTAngle(ANGLE, HTANGLE_MODE_RESET);
}
if (ButtonPressed(BTNRIGHT,0)) {
while(ButtonPressed(BTNRIGHT,0));
ResetSensorHTAngle(ANGLE, HTANGLE_MODE_CALIBRATE);
}
}
}