Hi all,
I have a problem on reading angular rate data
This is my code:
#include "main.h"
#include "gyro.c"
void Write1Byte(int8,int8);
unsigned int8 Read(int8);
int16 x=0,y=0,z=0;
unsigned int8 X_L=0,X_H=0,Y_L=0,Y_H=0,Z_L=0,Z_H=0;
void main()
{
SET_TRIS_D( 0x0F );
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
delay_ms(10);
Write1Byte(CTRL_REG1,0b00001111);
delay_ms(10);
Write1Byte(CTRL_REG2,0b00000000);
delay_ms(10);
Write1Byte(CTRL_REG3,0b00000000);
delay_ms(10);
Write1Byte(CTRL_REG4,0b10100000);
delay_ms(10);
Write1Byte(CTRL_REG5,0b00000000);
delay_ms(10);
while(1)
{
X_L=Read(OUT_X_L);
X_H=Read(OUT_X_H);
Y_L=Read(OUT_Y_L);
Y_H=Read(OUT_Y_H);
Z_L=Read(OUT_Z_L);
Z_H=Read(OUT_Z_H);
fprintf(pc," x_h=%u x_l=%u , y_h=%u y_l=%u , Z_h=%u Z_l=%u \n\r\ ",X_H,X_L,Y_H,Y_L,Z_H,Z_L);
fprintf(pc,"X=%Ld\n\r\n\r",x);
X_L=0;
X_H=0;
Y_L=0;
Y_H=0;
Z_L=0;
Z_H=0;
delay_ms(100);
output_toggle(pin_c0);
}
//TODO: User Code
}
void Write1Byte(int8 reg,int8 data)
{
i2c_start();
i2c_write(0b11010000);
i2c_write(reg);
i2c_write(data);
i2c_stop();
}
unsigned int8 Read(int8 reg)
{
unsigned int8 R=0;
i2c_start();
i2c_write(0b11010000);
i2c_write(reg);
i2c_stop();
i2c_start();
i2c_write(0b11010001);
R=i2c_read(0);
i2c_stop();
return R;
}
and this is my data :
x_l=11 x_h=160 y_l=249 y_h=10 z_l=5 z_h=227
x_l=11 x_h=161 y_l=249 y_h=11 z_l=5 z_h=229
x_l=11 x_h=159 y_l=249 y_h=2 z_l=5 z_h=225
x_l=11 x_h=157 y_l=249 y_h=5 z_l=5 z_h=224
x_l=11 x_h=150 y_l=249 y_h=11 z_l=5 z_h=231
pls help me!!!