CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

IMU Sensor Stick 9DOF with PIC16F877A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Neimen



Joined: 16 Mar 2012
Posts: 3

View user's profile Send private message

IMU Sensor Stick 9DOF with PIC16F877A
PostPosted: Fri Mar 16, 2012 4:19 am     Reply with quote

Hello,
I'm using the PIC16F877A in order to print data from IMU sensor stick, but having problems. http://www.sparkfun.com/products/10183

The sensors addresses are:

*Digital Accelerometer ADXL345
Read-0xA7
Write-0xA6

*MEMS Gyro ITG3200
Read-0xD1
Write-0xD0

*Digital Compass HMC5843
Read-0x3D
Write-0x3C

I need an example code for reading and printing the data from the sensor stick.
Thank you for the help,
Neimen.
temtronic



Joined: 01 Jul 2010
Posts: 9208
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 16, 2012 5:51 am     Reply with quote

First, you should post your code so we can make comments.

Second, a quick glance of the product sheet says there is a minor defect with the board, so you HAVE done the necessary repairs ???

Third, those read/write addresses make me think the stick is just 3 I2C devices. If so, it's very simple to cut code for the 'drivers'. In fact any or all of them may have been already done. Have you checked the CCS 'drivers' folder, the 'code library', searched this forum?

Forth, I don't have that device so I won't write a driver for it,since I can't test it in the 'real world'. Now, if you want to send me 2 pieces of the device, I'll be happy to cut the code,which I can do in about a week.

5th...be sure to use proper logic level convertors as you've got a 5 volt PIC and a 3 volt peripheral. Be a real shame to destroy them !!
SpaceXDebris



Joined: 15 Dec 2009
Posts: 3

View user's profile Send private message

PostPosted: Wed Mar 21, 2012 5:17 pm     Reply with quote

I got some numbers from the accel.

The sticking point was forgetting the "force_hw" flag. Without it, I was seeing 0xFF for all the data, which is a problem I've seen noticed mentioned several times around these boards. It's not the cleanest, but I hope that someone finds this code helpful.

Code:

#include <30F4011.h>
#fuses HS,NOWDT, PUT64
#use delay(clock=30M)
#use rs232(baud=57600, xmit=PIN_F5, rcv=PIN_F4, stream=COM_A)
#use i2c(master, sda=PIN_F2, scl=PIN_F3, force_hw)

#use fast_io(e)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define ACCEL_WRITE_ADDR 0xA6
#define ACCEL_READ_ADDR 0xA7
#define ACCEL_DATA_ADDR 0x32
#define ACCEL_PWRCTRL_ADDR 0x2d
#define ACCEL_MEASURE_MODE 0x08

void main ()
{
   int i;
   int accel_data[6];
   
   set_tris_e(0x00);

   // initialize
   for (i = 0; i < 6; i++) {
      accel_data[i] = 0;
   }

   while(true) {

      // Tell the accelerometer to wake up
      i2c_start();
      i2c_write(ACCEL_WRITE_ADDR);
      i2c_write(ACCEL_PWRCTRL_ADDR);
      i2c_write(ACCEL_MEASURE_MODE);
      i2c_stop();

      // Read data from the accel
      i2c_start();
      i2c_write(ACCEL_WRITE_ADDR);
      i2c_write(ACCEL_DATA_ADDR);
      i2c_start();
      i2c_write(ACCEL_READ_ADDR);
      accel_data[0] = i2c_read(); //x0
      accel_data[1] = i2c_read(); //x1
      accel_data[2] = i2c_read(); //y0
      accel_data[3] = i2c_read(); //y1
      accel_data[4] = i2c_read(); //z0
      accel_data[5] = i2c_read(0); // z1, NACK on last read
      i2c_stop();

      // Display
      for (i = 0; i < 6; i++) {
         fprintf(COM_A,"%x",accel_data[i]);
      }
      fprintf(COM_A,"\n\r");
      delay_ms(10);
   }
}


Cheers,
Chris
Jampe



Joined: 09 Mar 2012
Posts: 27

View user's profile Send private message

PostPosted: Thu Mar 22, 2012 12:27 am     Reply with quote

SpaceXDebris, works great! Thank you!
Neimen



Joined: 16 Mar 2012
Posts: 3

View user's profile Send private message

PostPosted: Thu Mar 22, 2012 2:10 am     Reply with quote

SpaceXDebris Thank you very much! The code works great and it was very helpful!
lazuribicci



Joined: 06 Feb 2010
Posts: 10

View user's profile Send private message

PostPosted: Thu Mar 29, 2012 2:25 am     Reply with quote

it is not work for me. I read only 0xFF
Ttelmah



Joined: 11 Mar 2010
Posts: 19470

View user's profile Send private message

PostPosted: Thu Mar 29, 2012 2:33 am     Reply with quote

The usual reason for reading 0xFF on I2C, is no pull up resistors on the bus.

Best Wishes
lazuribicci



Joined: 06 Feb 2010
Posts: 10

View user's profile Send private message

PostPosted: Thu Mar 29, 2012 2:36 am     Reply with quote

thank you for reply. but I use pull up resistors.
temtronic



Joined: 01 Jul 2010
Posts: 9208
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 29, 2012 6:59 am     Reply with quote

laz....
which PIC are you using ?
did you add pullups or are you just using the modules pullups?

there's an I2C test program on this forum, search for it, then run it, see what happens.
lazuribicci



Joined: 06 Feb 2010
Posts: 10

View user's profile Send private message

PostPosted: Thu Mar 29, 2012 7:17 am     Reply with quote

I tested circuit with oscilloscope. clock and data signals are seen well. but adxl345 doesn't send anything. data voltage is Vcc during reading.
BTW, CS pin is pulled up and ALT ADDRESS pin is grounded.

mcu is dspic30f4011
temtronic



Joined: 01 Jul 2010
Posts: 9208
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 29, 2012 11:20 am     Reply with quote

So logic dictates that since the other two sensors are working 100% that the accelerometer chip is kaput.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 29, 2012 12:00 pm     Reply with quote

Here is an i2c bus scanner program that will report the addresses of any
i2c slave devices that it finds on the bus. Modify the #include, #fuses,
and the #use statements so it will work with your board. Then run it
and see what devices it finds:
http://www.ccsinfo.com/forum/viewtopic.php?t=47707&start=21
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group