|
|
View previous topic :: View next topic |
Author |
Message |
Neimen
Joined: 16 Mar 2012 Posts: 3
|
Controlling 3 servos using ADXL345 accelerometer |
Posted: Sat Mar 24, 2012 6:56 am |
|
|
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
|
|
Posted: Sat Mar 24, 2012 7:54 pm |
|
|
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. |
|
|
|
|
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
|