Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Wire.h"
- /* the c++ frontend libraries, dubbed Wirish, are always included in sketches.
- However, low level libraries, dubbed libmaple, must be included manually */
- #include "wirish.h"
- #include "i2c.h"
- #define TIMEOUT 30
- #define XL345_ADDR 0x53 // use the 7 bit I2C address of the xl345
- #define XL345_DEVID 0xE5
- #define ITG3200_ADDR 0x69
- #define ITG3200_DEVID 0x69
- /* AD0 is tied to VCC */
- #define ITG_3200_I2C_ADDRESS 0x69
- #define ITG_3200_WHO_AM_I 0x0
- #define ITG_3200_SMPLRT_DIV 0x15
- #define ITG_3200_DLPF_FS 0x16
- #define ITG_3200_INT_CFG 0x17
- #define ITG_3200_INT_STATUS 0x1A
- #define ITG_3200_TEMP_OUT_H 0x1B
- #define ITG_3200_TEMP_OUT_L 0x1B
- #define ITG_3200_GYRO_XOUT_H 0x1D
- #define ITG_3200_GYRO_XOUT_L 0x1E
- #define ITG_3200_GYRO_YOUT_H 0x1F
- #define ITG_3200_GYRO_YOUT_L 0x20
- #define ITG_3200_GYRO_ZOUT_H 0x21
- #define ITG_3200_GYRO_ZOUT_L 0x22
- #define ITG_3200_PWR_MGM 0x3E
- // ADXL Control Registers
- #define ADXLREG_BW_RATE 0x2C
- #define ADXLREG_POWER_CTL 0x2D
- #define ADXLREG_DATA_FORMAT 0x31
- #define ADXLREG_FIFO_CTL 0x38
- #define ADXLREG_BW_RATE 0x2C
- #define ADXLREG_TAP_AXES 0x2A
- #define ADXLREG_DUR 0x21
- //ADXL Data Registers
- #define ADXLREG_DEVID 0x00
- #define ADXLREG_DATAX0 0x32
- #define ADXLREG_DATAX1 0x33
- #define ADXLREG_DATAY0 0x34
- #define ADXLREG_DATAY1 0x35
- #define ADXLREG_DATAZ0 0x36
- #define ADXLREG_DATAZ1 0x37
- static uint8 itg3200_read(uint8 reg) {
- Wire.beginTransmission(ITG3200_ADDR);
- Wire.send(reg);
- Wire.endTransmission();
- Wire.requestFrom(ITG3200_ADDR, 1);
- int now = millis();
- while (!Wire.available()) {
- if (millis() - now > TIMEOUT) {
- SerialUSB.println("TIMEOUT!");
- return 0;
- }
- }
- return Wire.receive();
- }
- void itg3200_init_wire() {
- Wire.beginTransmission(ITG3200_ADDR);
- Wire.send(ITG_3200_WHO_AM_I);
- Wire.endTransmission();
- Wire.requestFrom(ITG3200_ADDR, 1);
- while (!Wire.available())
- ;
- uint8 id = Wire.receive();
- SerialUSB.print("Chip id: 0x");
- SerialUSB.println(id, HEX);
- }
- void writeADXL(uint8 reg, uint8 data) {
- /* all i2c transactions send and receive arrays of i2c_msg objects */
- i2c_msg msgs[1]; // we dont do any bursting here, so we only need one i2c_msg object
- uint8 msg_data[2];
- msg_data = {reg,data};
- msgs[0].addr = XL345_ADDR;
- msgs[0].flags = 0; // write
- msgs[0].length = 2;
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,0);
- }
- void xl345_init() {
- /* all i2c transactions send and receive arrays of i2c_msg objects */
- i2c_msg msgs[1]; // we dont do any bursting here, so we only need one i2c_msg object
- uint8 msg_data[2];
- /* first make sure everything is wired up properly, by reading the device ID
- The device ID is located in address 0x00 and should always read back 0xE5
- To read this device ID, we must first WRITE "0x00" to the device to set the
- address to read on the next transaction. Then we must read 1 byte.
- */
- /* i2c_msg objects have fields:
- addr - the 7 bit i2c address of the receiving device
- flags - could be I2C_MSG_READ and/or I2C_MSG_10BIT_ADDR, or 0 for regular write
- length - the number of bytes to transact
- data - pointer to data array being xmitted/received
- */
- msg_data = {0x00,0x00};
- msgs[0].addr = XL345_ADDR;
- msgs[0].flags = 0;
- msgs[0].length = 1; // just one byte for the address to read, 0x00
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,0);
- msgs[0].addr = XL345_ADDR;
- msgs[0].flags = I2C_MSG_READ;
- msgs[0].length = 1; // just one byte for the address to read, 0x00
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,0);
- /* now we check msg_data for our 0xE5 magic number */
- uint8 dev_id = msg_data[0];
- SerialUSB.print("Read device ID from xl345: ");
- SerialUSB.println(dev_id,HEX);
- if (dev_id != XL345_DEVID) {
- SerialUSB.println("Error, incorrect xl345 devid!");
- SerialUSB.println("Halting program, hit reset...");
- waitForButtonPress(0);
- }
- /* to enable the xl345 into read mode we need to send three i2c messages
- each messages represents a write into a particular configuration register
- of the xl345.
- register 0x2C is "BW_RATE" register, which we set to 0x0A (
- register 0x2D is the "POWER_CTRL" register, which we set to 0x08 (
- register 0x31 is the "DATa_FORMAT" register, which we set to 0x04 (
- */
- writeADXL(ADXLREG_POWER_CTL, 0x00);
- writeADXL(ADXLREG_POWER_CTL, 0x08);
- }
- void itg3200_init() {
- /* initialize the itg-3200 3 axis gyro. See xl345_init for more info on i2c */
- i2c_msg msgs[1];
- uint8 msg_data[2];
- /* verify that itg is properly connected by WHOAMI register (should read ITG3200_ADDR) */
- msg_data = {0x00,0x00};
- msgs[0].addr = ITG3200_ADDR;
- msgs[0].flags = 0;
- msgs[0].length = 1; // just one byte for the address to read, 0x00
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,0);
- msgs[0].flags = I2C_MSG_READ;
- msgs[0].length = 1; // just one byte for the address to read, 0x00
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,0);
- /* now we check msg_data for our 0xE5 magic number */
- uint8 dev_id = msg_data[0];
- SerialUSB.print("Read device ID from itg3200: ");
- SerialUSB.println(dev_id,HEX);
- if (dev_id != ITG3200_DEVID) {
- SerialUSB.println("Error, incorrect itg3200 devid!");
- SerialUSB.println("Halting program, hit reset...");
- waitForButtonPress(0);
- }
- }
- uint8 readITG(uint8 reg) {
- i2c_msg msgs[1]; // we dont do any bursting here, so we only need one i2c_msg object
- uint8 msg_data[1];
- msg_data = {0x00};
- msg_data[0] = reg;
- msgs[0].addr = ITG3200_ADDR;
- msgs[0].flags = 0;
- msgs[0].length = 1; // just one byte for the address to read, 0x00
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,50);
- msgs[0].addr = ITG3200_ADDR;
- msgs[0].flags = I2C_MSG_READ;
- msgs[0].length = 1; // just one byte for the address to read, 0x00
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,50);
- return msg_data[0];
- }
- int itg_read_x_w() {
- return ((itg3200_read(ITG_3200_GYRO_XOUT_H) << 8) |
- (itg3200_read(ITG_3200_GYRO_XOUT_L)));
- }
- int itg_read_y_w() {
- return ((itg3200_read(ITG_3200_GYRO_YOUT_H) << 8) |
- (itg3200_read(ITG_3200_GYRO_YOUT_L)));
- }
- int itg_read_z_w() {
- return ((itg3200_read(ITG_3200_GYRO_ZOUT_H) << 8) |
- (itg3200_read(ITG_3200_GYRO_ZOUT_L)));
- }
- int itg_read_x() {
- return ((readITG(ITG_3200_GYRO_XOUT_H) << 8) |
- (readITG(ITG_3200_GYRO_XOUT_L)));
- }
- int itg_read_y() {
- return ((readITG(ITG_3200_GYRO_YOUT_H) << 8) |
- (readITG(ITG_3200_GYRO_YOUT_L)));
- }
- int itg_read_z() {
- return ((readITG(ITG_3200_GYRO_ZOUT_H) << 8) |
- (readITG(ITG_3200_GYRO_ZOUT_L)));
- }
- void readADXL_burst(uint8 reg, uint8 len, uint8 *msg_data) {
- i2c_msg msgs[1];
- msg_data[0] = reg;
- msgs[0].addr = XL345_ADDR;
- msgs[0].flags = 0;
- msgs[0].length = 1; // just one byte for the address to read, 0x00
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,0);
- msgs[0].addr = XL345_ADDR;
- msgs[0].flags = I2C_MSG_READ;
- msgs[0].length = len; // just one byte for the address to read, 0x00
- msgs[0].data = msg_data;
- i2c_master_xfer(I2C2, msgs, 1,0);
- }
- int xl345_read_axis(uint8 AXIS_REG) {
- int axis = 0;
- uint8 temp[2]={0};
- readADXL_burst(AXIS_REG,2,temp);
- axis=((temp[1]<<8)|temp[0]);
- return axis;
- }
- void setup() {
- /* start the sketch when we hit the button, timeout=0 (wait forever) */
- waitForButtonPress(0);
- SerialUSB.println("Initializing sensors");
- /* configure I2C port 2 (pins 0, 1) with no special option flags (second argument)*/
- i2c_master_enable(I2C2, 0);
- //Wire.begin(0,1);
- delay(100); // give time for the peripheral to come up
- xl345_init();
- itg3200_init();
- //itg3200_init_wire();
- waitForButtonPress();
- }
- void loop() {
- delay(250);
- int x = itg_read_x();
- int y = itg_read_y();
- int z = itg_read_z();
- SerialUSB.print(x,HEX);
- SerialUSB.print(", ");
- SerialUSB.print(y,HEX);
- SerialUSB.print(", ");
- SerialUSB.print(z,HEX);
- SerialUSB.print(" ");
- x = xl345_read_axis(ADXLREG_DATAX0);
- y = xl345_read_axis(ADXLREG_DATAY0);
- z = xl345_read_axis(ADXLREG_DATAZ0);
- SerialUSB.print(x,HEX);
- SerialUSB.print(", ");
- SerialUSB.print(y,HEX);
- SerialUSB.print(", ");
- SerialUSB.println(z,HEX);
- }
Advertisement
Add Comment
Please, Sign In to add comment