Request for translation (H48C Accelerometer code)
Hello, I am working on a sensor project with the Javelin stamp. I have acquired a lot of sensors but a few of them don't have sample code written for the Javelin. I was wondering if someone could help me translate the basic code to java? These are the sensors I'm using:
-Sensirion Temp/Humidity sensor www.parallax.com/detail.asp?product_id=28018
-Hitachi H48C Tri-axis Accelerometer www.parallax.com/detail.asp?product_id=28026
Any help would be appreciated, thanks.
Post Edited (bulkhead) : 7/31/2006 9:53:24 PM GMT
-Sensirion Temp/Humidity sensor www.parallax.com/detail.asp?product_id=28018
-Hitachi H48C Tri-axis Accelerometer www.parallax.com/detail.asp?product_id=28026
Any help would be appreciated, thanks.
Post Edited (bulkhead) : 7/31/2006 9:53:24 PM GMT
Comments
http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/peripheral/sensor/humidity/sht11/
The attachement is a HC48 test program.
regards peter
the 4k7 pullup and pulldown resistors are built into the SHT11 module?
the capacitor and 220 ohm resistor are not built into the SHT11?
Please specify exactly what is built in and what is external
so I can adapt the schematic.
regards peter
I hooked up mine directly to the module (no resistors or capacitors in between) and it worked fine. I guess the schematic isn't needed with the Sensirion module. However, you could add the note about the optional 4.7k ohm pull-down resistor on the clock line. Hope this is clear.
The basic stamp ** operator (multiply integer with fraction)
is performed by the UnsignedIntMath.umulf() method.
You find the UnsignedIntMath class here:
http://groups.yahoo.com/group/JavelinCode/files/Javelin%20Stamp%20IDE/lib/stamp/math/
regards peter
in method gforce(int channel).
Take a look at the timing diagrams in the documentation.
The PBASIC code is as comment included. I translated it from that
code. Not tested because I don't have a H48C.
regards peter
I would like to see what you changed to get the readings.
If the unit is not moving, I assume x and y should be 0 when there is no tilt.
For z, isn't it the gravity that is measured?
regards peter
I have attached my modified version. The only thing I changed was switching the bitcount and mode on shiftIn.
regards peter
If there is no overflow, it returns false.
regards peter
EDIT: PS: it prints 255 for refValue and adcValue
Post Edited (kojiWa) : 4/18/2007 9:09:54 PM GMT
Here is the javelin version
· /**
·· * Multiply unsigned integer with unsigned fraction.
·· *
·· * @param value Unsigned int to multiply
·· * @param fraction Unsigned multiplier (represents Multiplier / 65536)
·· * @return value * fraction rounded off unsigned integer result
·· */
· public static int umulf(int value, int fraction) {
··· int i, result=0, rest=0;
··· for (i=1; i<17; i++) {
····· if (fraction < 0) { //b15 set
······· result += (value >>> i);··················· //accumulate integer result
······· rest += ( (value & ((1<<i)-1)) << (16-i) ); //accumulate rest R/65536
······· if (CPU.carry()) result++;················· //update result if rest overflows
····· }
····· fraction <<= 1; //next bit
··· }
··· if (rest < 0) result++; //roundoff
··· return result;
· }
You replaced the bold line with
······ result += (value >>= i);··················· //accumulate integer result
This alters value which must not be.
The javelin >>> means logical right shift, shifting in 0's at b15
The javelin >> means arithmetic shift right, copying b15 to b14 (aka sign extension)
Assuming you don't have the >>> equivalent use
result += ((value >> i) & 0x7FFF);
Here is an adapted umulf
int umulf(int value, int fraction)
· {
··· int result = 0;
··· int rest = 0;
····int·temp,temp2;
··· for (int i=1; i<17; i++)
··· {
····· if (fraction < 0) { //b15 set
······· result += ((value >> i) & 0x7FFF);············ //accumulate integer result
······· temp·= ( (value & ((1<<i)-1)) << (16-i) ); //accumulate rest R/65536
······· temp2 = rest ^ temp; //never overflow if sign(rest) != sign(temp)
······· rest += temp; //accumulate rest R/65536
······· //if (CPU.carry()) result++;················· //update result if rest overflows
······· if (temp2 >= 0) { //b15 clear, signs equal -> test for·overflow
········· if ((rest ^ temp) < 0) result++; //new rest has different sign than temp
······· }
····· }
····· fraction <<= 1; //next bit
··· }
··· if (rest < 0) result++; //roundoff
··· return result;
· }
See what results you get with this modified umulf.
regards peter
·
void myShiftOut( int dataPin, int clockPin, int bitCount, int bitOrder, byte val )
{
for( int i = 0; i < bitCount; i++ )
{
if ( bitOrder == LSBFIRST )
digitalWrite( dataPin, !! ( val & ( 1 << i ) ) );
else
digitalWrite( dataPin, !! ( val & ( 1 << ( ( bitCount - 1 ) - i ) ) ) );
digitalWrite( clockPin, HIGH );
digitalWrite( clockPin, LOW );
}
}
byte myShiftIn( int dataPin, int clockPin, int bitCount )
{
int pinState;
byte dataIn = 0;
pinMode( clockPin, OUTPUT );
pinMode( dataPin, INPUT );
for( int i = bitCount; i >= 0; i -- )
{
digitalWrite( clockPin, LOW );
delayMicroseconds( 20 );
int temp = digitalRead( dataPin );
if( temp )
{
pinState = 1;
dataIn = dataIn | ( 1 << i );
}
else
{
pinState = 0;
}
digitalWrite( clockPin, HIGH );
}
return dataIn;
}
PS: another question is if the shiftIn and shiftOut function from the javelin library is accessible to see?
thanks
Post Edited (kojiWa) : 4/19/2007 2:04:44 PM GMT
void myShiftOut(int dataPin, int clockPin, int bitCount, int bitOrder, int val ) {
· for( int i = 0; i < bitCount; i++) {
··· if (bitOrder == LSBFIRST) {
····· digitalWrite( dataPin, !! ( val & 1) );
····· val = val >> 1;
··· }
··· else {
····· digitalWrite( dataPin, !! ((val >> 15)&1) );
····· val = val << 1;
··· }
··· digitalWrite( clockPin, HIGH );
··· digitalWrite( clockPin, LOW );
· }
}
int myShiftIn(int dataPin, int clockPin, int bitCount) {
· int pinState;
· int dataIn = 0;
· pinMode( clockPin, OUTPUT );
· pinMode( dataPin, INPUT );
· for(int i = 0; i <bitCount; i++) {
··· digitalWrite( clockPin, LOW );
··· delayMicroseconds( 20 );
··· int temp = digitalRead( dataPin );
··· if (temp) pinState = 1;
··· else pinState = 0;
··· dataIn = (dataIn << 1) | pinState;
··· digitalWrite(clockPin,HIGH);
· }
}
regards peter
int myShiftIn(int dataPin, int clockPin, int bitCount) {
int pinState;
int dataIn = 0;
pinMode( clockPin, OUTPUT );
pinMode( dataPin, INPUT );
for(int i = 0; i <bitCount; i++) {
digitalWrite( clockPin, LOW );
delayMicroseconds( 20 );
int temp = digitalRead( dataPin );
if (temp) pinState = 1;
else pinState = 0;
dataIn = (dataIn << 1) | pinState;
digitalWrite(clockPin,HIGH);
}
Serial.println(dataIn);
return dataIn;
}