L3G4200D interfacing with PIC I2C

Status
Not open for further replies.

Chullaa

Advanced Member level 4
Joined
Aug 27, 2010
Messages
113
Helped
17
Reputation
34
Reaction score
17
Trophy points
1,298
Visit site
Activity points
2,019
Hi
I am trying to interface L3G4200D 3 axis gyroscope with PIC 16F877A, using i2c bus.

I tried to write the code but something is wrong in code, all I can get is -1 value.

here is the code

Code:
#include "D:\1\main.h"
#include <lcd.c>

unsigned int x,who;
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
///////////////////////////////DEVICE ADDRESS/////////////////////////////////
#define I2C_Gyro    0xD0 

///////////////////////////////REGISTER ADDRESS/////////////////////////////////
#define GYRO_outX    0x29
#define GYRO_outY    0x2B 
#define GYRO_outZ    0x2D 

/////////////////////////////////// FUNCTIONS ////////////////////////////////
BYTE read_sensor(BYTE device, BYTE reg) 
{    
   BYTE data; 
    
   i2c_start(); 
   i2c_write(device); 
   i2c_write(reg); 

   i2c_start(); 
   i2c_write(device+1); 
    data=i2c_read(0); 
   i2c_stop(); 

   return(data); 
} 

////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// MAIN ///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   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);

   // TODO: USER CODE!!
   trisc3=0;
   trisc4=1;
   trisc5=0;
  
  while(1)
  {
  x=102;

  who=read_sensor(I2C_Gyro,0x26);
  x=read_sensor(I2C_Gyro,0x28);
  
  lcd_init();
  lcd_putc("\f"); // Clear display
  lcd_gotoxy(1,1);
  
  printf(lcd_putc,"X: %d %d",x,who);
  printf("\n\r %d",x);
  delay_ms(200);
  }
}

SCL -----> SCL of pic
SDA -----> SDA of pic
Vcc -----> 3.3V
GND -----> GND
CS -----> 3.3V

REGARDS
Umair
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…