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 CCS Technical Support

Controlling 3 servos using ADXL345 accelerometer

 
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

Controlling 3 servos using ADXL345 accelerometer
PostPosted: Sat Mar 24, 2012 6:56 am     Reply with quote

Hello,
I have a project that is stabilizing a camera with gyro sensor and accelerometer.
I'm using PIC16f887 and trying to write a code that is controlling 3 servo motors using ADXL345 accelerometer but without any success.
I really need your help for writing me the code.

Here is the code that work great for my ADXL345 3 axis:
Code:

#include <w.h>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(master, sda=PIN_C4, scl=PIN_C3, 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("%x",accel_data[i]);
      }
      fprintf("\n\r");
      delay_ms(10);
   }
}

Please help me with this code!!!
temtronic



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

View user's profile Send private message

PostPosted: Sat Mar 24, 2012 7:54 pm     Reply with quote

I'll give you 3 helpful hints...

1) Always add 'errors' to the use rs232(...) line whenever using the hardware UART.

2) You should specify what make/model of 'servo motors' you're referring to. They range from super simple generic RC hobby servo units to direct drive 3HP units with 1024 optical encoder feedback.

3) Selfcode variables.eg: instead of accel_data[2] use a better name, say accel_data_y0. It will help you remember what the data actually is and help others try to figure out what is wrong.

also...

Unless you're 100% sure of what you're doing, avoid using fast_io() !If you don't properly code the tris registers you will have problems.it is very,very rare you _need_ fast_io().

another freebie

When you're happy with a chunk of code, say 'read_the_accellerometer', turn it into a function. That way,it 'cleans up' the main code allowing you to concertrate on new code.
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