Gyroscope L3G4200D in arduino due


hello guys!
i'm using gyroscope l3g4200d , have created 2 codes,one protocol i2c , 1
spi.

code i2c
quote
#include <wire.h>

double x, y, z;
byte highbyte, lowbyte;

void setup()
{
  wire.begin();                                     
  serial.begin(9600);                               

  wire.begintransmission(105);                       
  wire.write(0x20);                                 
  wire.write(0x0f);                                 
  wire.endtransmission();                           
  wire.begintransmission(105);                       
  wire.write(0x23);                                 
  wire.write(0x90);                                 
  wire.endtransmission();                           

}

void loop()
{
  delay(250);                                         
  gyro_value();                                   
  print_xyz();                                   
}

void gyro_value()
{
  wire.begintransmission(105);                       
  wire.write(168);                                   
  wire.endtransmission();                           
  wire.requestfrom(105, 6);                         
  while(wire.available() < 6);
                     
  lowbyte = wire.read();                             
  highbyte = wire.read();                           
  x = ((highbyte<<8)|lowbyte);  smiley = number "8" , can not change         
 
  lowbyte = wire.read();                           
  highbyte = wire.read();                           
  y = ((highbyte<<8)|lowbyte);  smiley = number "8" , can not change               

  lowbyte = wire.read();                           
  highbyte = wire.read();                           
  z = ((highbyte<<8)|lowbyte);  smiley = number "8" , can not change                   

}

void print_output()
{
  serial.print("x:");
  serial.print(x/57.14286,0);             

  serial.print("  y:");
  serial.print(y/57.14286,0);               
 
  serial.print("  z:");
  serial.print(z/57.14286,0);                 
}
code spi
quote
#include <spi.h>

#define cs 53
#define mosi 51
#define miso 50
#define sck 52

double x, y, z;
byte highbyte, lowbyte;

void setup()
{
  serial.begin(9600);

  spi.begin();
  spi.setdatamode(spi_mode0);
  spi.setclockdivider(spi_clock_div2);

  pinmode(cs, output);
 
  digitalwrite(cs, low);
  spi.transfer(0x20 & 0x7f);
  spi.transfer(0x0f);
  digitalwrite(cs, high);

  digitalwrite(cs, low);
  spi.transfer(0x23 & 0x7f);
  spi.transfer(0x90);
  digitalwrite(cs, high);             
 
}

void loop()
{
  delay(250);
  gyro_value();
  print_xyz();
}

void gyro_value()
{

  digitalwrite(cs, low);
  spi.transfer(0x28 | 0x80);
  lowbyte = spi.transfer(0x00);
  digitalwrite(cs, high);
  digitalwrite(cs, low);
  spi.transfer(0x29 | 0x80);
  highbyte = spi.transfer(0x00);
  digitalwrite(cs, high);
  x = ((highbyte<<8)|lowbyte);  smiley = number "8" , can not change   


  digitalwrite(cs, low);
  spi.transfer(0x2a | 0x80);
  lowbyte = spi.transfer(0x00);
  digitalwrite(cs, high);
  digitalwrite(cs, low);
  spi.transfer(0x2b | 0x80);
  highbyte = spi.transfer(0x00);
  digitalwrite(cs, high);
  y = ((highbyte<<8)|lowbyte);  smiley = number "8" , can not change   
 

  digitalwrite(cs, low);
  spi.transfer(0x2c | 0x80);
  lowbyte = spi.transfer(0x00);
  digitalwrite(cs, high);
  digitalwrite(cs, low);
  spi.transfer(0x2d | 0x80);
  highbyte = spi.transfer(0x00);
  digitalwrite(cs, high);
  z = ((highbyte<<8)|lowbyte);  smiley = number "8" , can not change   

}


void print_output()
{
  serial.print("x:");
  serial.print(x/57.14286,0);             

  serial.print("  y:");
  serial.print(y/57.14286,0);               
 
  serial.print("  z:");
  serial.print(z/57.14286,0);                 
}
i'm using them in arduino mega , output(x,y,z) i'm getting definetely right.
when i'm running these 2 codes in arduino due i'm getting wrong reults don't fit previous.
do have idea why happening?
i have tried using other libraries didn't work out on arduino due.
any appreciated!

please edit post change quote tags code tags. otherwise forum software eats little bit of code.

see smileys? that's code being misinterpreted.

you might take opportunity edit typo in title.


Arduino Forum > Products > Arduino Due (Moderator: fabioc84) > Gyroscope L3G4200D in arduino due


arduino

Comments

Popular posts from this blog

Error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode - Raspberry Pi Forums

class MPU6050 has no member named begin

missing filename after '-o'