Hello Everyone,
First off let me say thank you for taking the time to read this post, I will try to explain to the best of my ability my problem, however if anything remains unclear please post or pm me and I will get back to you within the hour!
The Problem: I have an IMU 3000 & ADXL 345 Fusion board from Sparkfun, https://www.sparkfun.com/products/10252
I am using an I2C connection type for all communications between my PIC 18F4520 and the board. I am using a logic leveler to go from the 5v 18f pic pins to the 3v3 sda, scl pins on the board. I have used some sample code to see If I get anything back on the lines and the pic successfully picked up on the Accel ack and Gyro Acks, so I suspect nothing is wrong with my hardware (side note I have also got the accelerometer to work on its own). For some reason whenever I compile and run the following code,
#include <18f4520.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(master, sda=pin_C4, scl=pin_C3, force_hw)
#define GYRO 0x68
#define GYRO_WRITE 0xD0
#define GYRO_READ 0xD1
#define GYRO_DATA 0x1D
#define ACCEL 0x53
#define ACCEL_WRITE 0xA6
#define ACCEL_READ 0xA7
#define ACCEL_DATA 0x32
#define ADXL345_POWER_CTL 0x2d
#define ACCEL_MEASURE_MODE 0x08
int buffer[12];
int gyro_x;
int gyro_y;
int gyro_z;
int accel_x;
int accel_y;
int accel_z;
int i;
void writeTo(int device, int address, int val){
i2c_start();
i2c_write(device);
i2c_write(address);
i2c_write(val);
i2c_stop();
}
void main()
{
writeTo(GYRO,0x16,0x0B);
writeTo(GYRO, 0x18,ACCEL_DATA);
writeTo(GYRO,0x14,ACCEL);
writeTo(GYRO, 0x3D,ACCEL_MEASURE_MODE);
writeTo(ACCEL, ADXL345_POWER_CTL, 8);
writeTo(GYRO,0x3d,0x28);
while(true) {
i2c_start();
i2c_write(GYRO);
i2c_write(GYRO_DATA);
i2c_start();
i2c_write(GYRO_READ);
buffer[0]=i2c_read();
buffer[1]=i2c_read();
buffer[2]=i2c_read();
buffer[3]=i2c_read();
buffer[4]=i2c_read();
buffer[5]=i2c_read();
buffer[6]=i2c_read();
buffer[7]=i2c_read();
buffer[8]=i2c_read();
buffer[9]=i2c_read();
buffer[10]=i2c_read();
buffer[11]=i2c_read(0);
i2c_stop();
gyro_x=buffer[0] << 8 | buffer[1];
gyro_y=buffer[2] << 8 | buffer[3];
gyro_z=buffer[4] << 8 | buffer[5];
accel_y=buffer[7] << 8 | buffer[6];
accel_x=buffer[9] << 8 | buffer[8];
accel_z=buffer[11] << 8 |buffer[10];
printf("%i",gyro_x);
printf(",");
printf("%i",gyro_y);
printf(",");
printf("%i",gyro_z);
printf(",");
printf("%i",accel_x);
printf(",");
printf("%i",accel_y);
printf(",");
printf("%i",accel_z);
printf("\n\r");
delay_ms(100);
}
}
I get the following results,
0,0,0,0,0,0
0,0,83,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0
0,17,0,0,119,104
0,0,0,0,0,0
0,0,0,50,0,0
0,0,0,0,0,0
0,0,0,0,0,0
121,28,113,-17,-80,-54
0,0,0,0,0,0
0,83,0,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0
-113,0,32,104,0,0
0,0,0,0,0,0
0,0,0,0,50,0
0,0,0,0,0,0
0,0,0,0,0,0
-41,-99,-74,33,-101,-50
0,0,0,0,0,0
83,0,50,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0
0,32,0,0,104,0
0,0,0,0,0,0
0,0,0,0,0,0
0,0,0,0,0,0
0,0,0,0,0,-111
119,0,104,0,0,0
0,0,0,0,0,0
0,50,0,0,0,0
0,0,0,0,0,0
As this is my first I2C project I am not sure what is wrong with my code, I suspect there is a problem with which the order I called the registers are wrong or the registers are wrong in general. I have read both datasheets and feel very stuck. If anyone could go through my code and make any suggestions, comments, I would be forever grateful!!
Again thank you for taking the time to read this post!