i have got code written for -lis3lv02dl sensor, from that i have changed for the sensor minIMU9v2 in order to read only accelerometer value. bu8t i do no what to replace in place of E& and 40 in main program. it is not working, also that program written to display data in PC using Bluetooth module, but i want to display in lcd. kindly do the need ful.
my code changed from actual code:
#include <18f4550.h>
#device ADC=10
#fuses HS,MCLR,NOWDT,NOPROTECT,NOLVP,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,stream=Rec_Dat) /*Set USART to 9600 Baudrate */
#use I2C(master,sda=PIN_B0, scl=PIN_B1) /*Set I2C Settings */
#define LCD_ENABLE_PIN PIN_C2
#define LCD_RS_PIN PIN_C0
#define LCD_RW_PIN PIN_C1
#define DATA_PORT PORTD
#include <lcd.c>
//long int n,ch0;
unsigned int i,I2c_Data[10];
int x_acc, y_acc, z_acc; //x,y,z-data_16bit
float X_out; //Define floating variables for results in G units
float Y_out;
float Z_out;
int read_acc(int address_out); //reading
long acc_change(int data_H,int data_L); //change
float convert_into_g(int16 data); //Converts into gravity values
#define WRITE 0x32
#define READ 0x33
#define REG1 0x20
#define REG2 0x21
#define OUTX_L_A 0x28
#define OUTX_H_A 0x29
#define OUTY_L_A 0x2A
#define OUTY_H_A 0x2B
#define OUTZ_L_A 0x2C
#define OUTZ_H_A 0x2D
//#define LED PIN_B2
int read_acc(int address_out)
{
int DL=0x00, DH=0x00;
int out = 0;
i2c_start(); //Start communication
i2c_write(WRITE); //prepare the device to receive data
i2c_write(address_out|0x80); //address register, autoincrement of address
i2c_start(); //
i2c_write(READ); //Prepares the device to be read
DL=i2c_read(); //Read data LSB
DH=i2c_read(0); //Read data MSB
i2c_stop(); //Stop condition
//concatenating result
out=DL|((DH&0x0F)<<8); //Join the LSB and the MSB
return(out); //Returns the result
}
float convert_into_g(int data)
{
float output;
output=(float)data; //Converts integer data into float
if (data>=2048) //check if the result is indicating a negative value
{
output=(output/1024)-4; //Converts into g
}
if (data<2048) //if the result indicates a positive value
{
output=output/1024; //converts into g
}
return(output); //Returns the result
}
void main()
{
//setup_adc_ports(NO_ANALOGS);
lcd_init();
set_tris_c(0xff);
set_tris_d(0x00);
//i2c_start();
//i2c_write(0x6B);
//WriteData(0x30,0xFF);
i2c_start(); //Sends a start command to the device
i2c_write(WRITE); //Sends a single byte preparing the device to receive data
i2c_write(REG1); //Indicate the device's addres to be modified
i2c_write(0xE7); //11100111 = C7-->Turn on device activating all the axis, 640Hz sampling rate
i2c_stop(); //Stop I2C transmission
i2c_start(); //Start I2C communication
i2c_write(WRITE); //Sends command to write data
i2c_write(REG2); //Address the control register 2
i2c_write(0x40); //01000001 --> 2g, no update between readings,12bits right justified
i2c_stop(); //Close trasmmision
while(true)
{
lcd_gotoxy(1,1);
lcd_putc(" acc:");
//output_high(LED);
x_acc = read_acc(OUTX_L_A); //Read data for the x axis
y_acc = read_acc(OUTY_L_A); //Read data for the y axis
z_acc = read_acc(OUTZ_L_A); //Read data for the z axis
X_out=convert_into_g(x_acc); //Converts binary value into g
Y_out=convert_into_g(y_acc);
Z_out=convert_into_g(z_acc);
lcd_gotoxy(2,2);
printf( lcd_putc X " %g ", X_out); //display //send results over RS232
printf(lcd_putc Y " %g ", Y_out); //display
printf(lcd_putc Z "%g \n \r", Z_out); //display
delay_ms(10); //delay for sampling 100Hz
//output_low(LED);
i2c_stop();
//delay_ms(1000);
}
}