Posted: Mon Mar 29, 2010 8:30 am Post subject: SparkFun HMC6343 i2c Code
Here is some code for the SparkFun HMC6343 Compass/Tilt sensor. It's not very different from other Roboard i2c code floating around, but thought I'd pass it along.
Code:
#include <roboard>
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argz, char** argv)
{
if (!i2c_Initialize(I2CIRQ_DISABLE)) {
cout << "Failed to Initialize i2c." <<endl;
}
i2c0master_StartN(0x32>>1, I2C_WRITE, 1);
i2c0master_WriteN(0x71); // heading data
usleep( 1000 ); // 1 ms
cout << "Press y and enter when finished calibrating to save calibration."<<endl>> a;
if (a == 'y') {
i2c0master_StartN(0x32>>1, I2C_WRITE, 1);
i2c0master_WriteN(0x7E); // heading data
cout <<"Calibration Saved"<<endl>>1, I2C_WRITE, 1);
i2c0master_WriteN(0x50); // heading data
//i2c0master_WriteN(0x55); // tilt data
//i2c0master_WriteN(0x40); // accel data
//0x45 // mag data
usleep( 1000 ); // 1 ms
Just noticed that above the "if (a == 'y') { " statements the "cin >> a" got truncated becuase of the bugs with the greater-than and less-than characters.
Project :
Version :
Date : 7/12/2010
Author :
Company :
Comments:
Chip type : ATmega8535
Program type : Application
Clock frequency : 11.059200 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 128
*****************************************************/
/* this part is used to convert binary to ascii. for example, if the heading result 271 degree, then
1. mod it with 1000 result in 271, then devide it with 100 result will be 2 (rounded) then add the result with 0 ascii (48) then it store at heading[0];
2. mod it with 100 result in 71, then devide it with 10 result will be 7 (rounded) then add the result with 0 ascii (48) then it store at heading[1];
3. mod it with 10 result in 1, then devide it with 1 result will be 1 (rounded) then add the result with 0 ascii (48) then it store at heading[3];
*/
pitch[0] = pitcha % 1000 / 100 + 48;
pitch[1] = pitcha % 100 / 10 + 48;
pitch[2] = pitcha % 10 / 1 + 48;
pitch[3] = NULL;
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum