L3G4200D

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
jbv000
Posts: 46
Joined: 21 Oct 2006 17:14
Location: The Netherlands Krimpen a/d IJssel

L3G4200D

#1 Post by jbv000 » 13 Nov 2011 12:53

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

Toley
Posts: 922
Joined: 03 Sep 2008 16:17

Re: L3G4200D

#2 Post by Toley » 13 Nov 2011 13:43

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!

jbv000
Posts: 46
Joined: 21 Oct 2006 17:14
Location: The Netherlands Krimpen a/d IJssel

Re: L3G4200D

#3 Post by jbv000 » 13 Nov 2011 19:06

Yes please
I will leard the C statements
thanks

Toley
Posts: 922
Joined: 03 Sep 2008 16:17

Re: L3G4200D

#4 Post by Toley » 14 Nov 2011 00:41

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!

jbv000
Posts: 46
Joined: 21 Oct 2006 17:14
Location: The Netherlands Krimpen a/d IJssel

Re: L3G4200D

#5 Post by jbv000 » 14 Nov 2011 19:34

Thanks

I am going te study

regards jan

Toley
Posts: 922
Joined: 03 Sep 2008 16:17

Re: L3G4200D

#6 Post by Toley » 15 Nov 2011 14:32

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!

jbv000
Posts: 46
Joined: 21 Oct 2006 17:14
Location: The Netherlands Krimpen a/d IJssel

Re: L3G4200D

#7 Post by jbv000 » 15 Nov 2011 14:52

Thanks

dave1993
Posts: 2
Joined: 25 Mar 2016 19:24

Re: L3G4200D

#8 Post by dave1993 » 25 Mar 2016 19:31

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

dave1993
Posts: 2
Joined: 25 Mar 2016 19:24

Re: L3G4200D

#9 Post by dave1993 » 25 Mar 2016 21:08

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

Post Reply

Return to “mikroBasic PRO for PIC General”