Hello
Who can help me with a test program in Basic PRo with PIC 16F877a for gyro L3G4200D.
It is the first time i want to use the gyro.
regards
L3G4200D
Re: L3G4200D
I have a some routines for reading the L3G4200D gyroscope and the LIS3LV02DL accelerometer (they are read almost the same way). The only things is that it's written in C for a dsPIC30F6014A and it use the I2C interface. If you think it can be usefull I can post it here.
Serge T.
Learning is an endeless process but it must start somewhere!
Learning is an endeless process but it must start somewhere!
Re: L3G4200D
Yes please
I will leard the C statements
thanks
I will leard the C statements
thanks
Re: L3G4200D
Wish this will help you.
Code: Select all
#define Accel_Wr 0x30
#define Accel_Rd 0x31
#define Gyro_Wr 0xD0
#define Gyro_Rd 0xD1
#define WHO_AM_I 0x0F
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
#define CTRL_REG6 0x25
#define OUT_X_L 0x28
#define OUT_X_H 0x29
#define OUT_Y_L 0x2A
#define OUT_Y_H 0x2B
#define OUT_Z_L 0x2C
#define OUT_Z_H 0x2D
#define FIFO_CTRL_REG 0x2E
//*** Prototypes ***//
void Accel_Init(void);
void Gyro_Init(void);
void Write1Byte(u8 Device_Wr,u8 I2Cadr,u8 I2Cdata);
u8 Read1Byte(u8 Device_Wr,u8 I2Cadr);
int TwoComp(u8 MSB, u8 LSB);
TriAxes GetSensor(u8 I2Cadr);
//*** Get Sensor ***//
TriAxes GetSensor(u8 I2Cadr)
{
u8 Xlow,Xhigh,Ylow,Yhigh,Zlow,Zhigh;
TriAxes SensorAxe;
Xlow = Read1Byte(I2Cadr,OUT_X_L);
Xhigh = Read1Byte(I2Cadr,OUT_X_H);
Ylow = Read1Byte(I2Cadr,OUT_Y_L);
Yhigh = Read1Byte(I2Cadr,OUT_Y_H);
Zlow = Read1Byte(I2Cadr,OUT_Z_L);
Zhigh = Read1Byte(I2Cadr,OUT_Z_H);
SensorAxe.X = TwoComp(Xhigh, Xlow);
SensorAxe.Y = TwoComp(Yhigh, Ylow);
SensorAxe.Z = TwoComp(Zhigh, Zlow);
return SensorAxe;
}
//*** Two's Complement ***//
int TwoComp(u8 MSB, u8 LSB)
{
int result;
u16 temp;
temp = (MSB * 256) + LSB;
//return temp;
result = ~temp + 1;
return result;
}
//*** Accel_Init ***//
void Accel_Init(void)
{
Write1Byte(Accel_Wr,CTRL_REG1,0x47); // Normal Mode, 50 Hz
//Write1Byte(Accel_Wr,CTRL_REG4,0x18); //0x0x-2G 0x1x-4G 0x2x-8G 0x3x-16G, 0xx8 HighRes 0xx0 LowRes
Write1Byte(Accel_Wr,CTRL_REG4,0x20); // 8G LowRes
}
//*** Gyro_Init ***//
void Gyro_Init(void)
{
Write1Byte(Gyro_Wr,CTRL_REG1,0x0F); // 100Hz, 12.5, Power Up
Write1Byte(Gyro_Wr,CTRL_REG4,0x20); // 0x00-250dps, 0x10-500dps, 0x20-2000dps
}
//*** Write1Byte ***//
void Write1Byte(u8 Device_Wr,u8 I2Cadr, u8 I2Cdata)
{
I2C1_Start();
I2C1_Write(Device_Wr);
I2C1_Write(I2Cadr);
I2C1_Write(I2Cdata);
I2C1_Stop();
}
//*** Read1Byte ***//
u8 Read1Byte(u8 Device_Wr,u8 I2Cadr)
{
u8 I2CRead;
I2C1_Start();
I2C1_Write(Device_Wr);
I2C1_Write(I2Cadr);
I2C1_Restart();
I2C1_Write(Device_Wr + 1);
I2CRead = I2C1_Read(1);
I2C1_Stop();
return I2CRead;
}
Serge T.
Learning is an endeless process but it must start somewhere!
Learning is an endeless process but it must start somewhere!
Re: L3G4200D
Thanks
I am going te study
regards jan
I am going te study
regards jan
Re: L3G4200D
Hi jan, maybe you already figure it but there is also a .h file to define new types and a structure for the 3 axis of the gyro and the accelerometer. If you have any question please feel free to ask.
Code: Select all
//*** New Types ***//
typedef unsigned short u8;
typedef unsigned int u16;
typedef unsigned long u32;
typedef struct{
int X;
int Y;
int Z;
}TriAxes;
Serge T.
Learning is an endeless process but it must start somewhere!
Learning is an endeless process but it must start somewhere!
Re: L3G4200D
Thanks
Re: L3G4200D
Hi Toley
I am looking at using your code to interface the L3G4200D with a dsPIC33EP63GS502 pic if that's alright? The issue I am having is that even when I include the <i2c.h> MPlab is not registering the commands such as I2C1_Start(). Any ideas why that might be.
Thanks
Dave
I am looking at using your code to interface the L3G4200D with a dsPIC33EP63GS502 pic if that's alright? The issue I am having is that even when I include the <i2c.h> MPlab is not registering the commands such as I2C1_Start(). Any ideas why that might be.
Thanks
Dave
Re: L3G4200D
Hi Toley
I am trying to implement your code on a dspic33EP64GS502. using the XC16 complier and mp lab. with the #include <i2c.h> included MP lad still doesn't seem to recognise commands such as I2C1_Start().
any recommendation's why?
I am fairly new to coding in C++ so sorry if its a basic question.
Thanks very much!
Dave
I am trying to implement your code on a dspic33EP64GS502. using the XC16 complier and mp lab. with the #include <i2c.h> included MP lad still doesn't seem to recognise commands such as I2C1_Start().
any recommendation's why?
I am fairly new to coding in C++ so sorry if its a basic question.
Thanks very much!
Dave