Skip to content

Commit

Permalink
solved a bug, provided 2 example
Browse files Browse the repository at this point in the history
asicnI2C.ino does not work properly, use asicnI2Ctest2.ino for reference
(it use ADXL345)
  • Loading branch information
lestofante committed Aug 8, 2012
1 parent af9c143 commit 35456db
Show file tree
Hide file tree
Showing 10 changed files with 1,705 additions and 9 deletions.
12 changes: 10 additions & 2 deletions asincI2C/Wire.cpp
Expand Up @@ -54,6 +54,11 @@ TwoWire::TwoWire()


// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////


void TwoWire::begin(void)
{
begin(true, false);
}

void TwoWire::begin(boolean pullUp, boolean fast) void TwoWire::begin(boolean pullUp, boolean fast)
{ {
rxBufferIndex = 0; rxBufferIndex = 0;
Expand Down Expand Up @@ -197,14 +202,17 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop
if(quantity > BUFFER_LENGTH){ if(quantity > BUFFER_LENGTH){
quantity = BUFFER_LENGTH; quantity = BUFFER_LENGTH;
} }
// Serial.println("tw_r1");
// perform blocking read into buffer // perform blocking read into buffer
uint8_t read = twi_askReadFrom(address, quantity, sendStop); uint8_t read = twi_askReadFrom(address, quantity, sendStop);

// Serial.println("tw_r2");
/* ADDED BY LESTO FOR RETROCOMPATIBILITY */ /* ADDED BY LESTO FOR RETROCOMPATIBILITY */
while (twi_AsincReadNotReady(quantity) != 0){ while (twi_AsincReadNotReady(quantity) != 0){
// Serial.print("tw_err: ");
// Serial.println( twi_AsincReadNotReady(quantity) );
continue; continue;
} }
// Serial.println("tw_r3");
read = twi_AsincReadFrom(rxBuffer, quantity); read = twi_AsincReadFrom(rxBuffer, quantity);
/* END ADD */ /* END ADD */


Expand Down
1 change: 1 addition & 0 deletions asincI2C/Wire.h
Expand Up @@ -55,6 +55,7 @@ class TwoWire : public Stream
void begin(int, boolean, boolean); void begin(int, boolean, boolean);
void begin(uint8_t); void begin(uint8_t);
void begin(int); void begin(int);
void begin(void);
void beginTransmission(uint8_t); void beginTransmission(uint8_t);
void beginTransmission(int); void beginTransmission(int);
uint8_t endTransmission(void); uint8_t endTransmission(void);
Expand Down
172 changes: 170 additions & 2 deletions asincI2C/asincI2C.ino
@@ -1,7 +1,175 @@
//fast test copyed from http://www.sparkfun.com/tutorials/265
//thank you sparkfun!


//The Wire library is used for I2C communication
#include <Wire.h>


void setup(){ //This is a list of registers in the ITG-3200. Registers are parameters that determine how the sensor will behave, or they can hold data that represent the
//sensors current status.
//To learn more about the registers on the ITG-3200, download and read the datasheet.
char WHO_AM_I = 0x00;
char SMPLRT_DIV= 0x15;
char DLPF_FS = 0x16;
char GYRO_XOUT_H = 0x1D;
char GYRO_XOUT_L = 0x1E;
char GYRO_YOUT_H = 0x1F;
char GYRO_YOUT_L = 0x20;
char GYRO_ZOUT_H = 0x21;
char GYRO_ZOUT_L = 0x22;

//This is a list of settings that can be loaded into the registers.
//DLPF, Full Scale Register Bits
//FS_SEL must be set to 3 for proper operation
//Set DLPF_CFG to 3 for 1kHz Fint and 42 Hz Low Pass Filter
char DLPF_CFG_0 = 1<<0;
char DLPF_CFG_1 = 1<<1;
char DLPF_CFG_2 = 1<<2;
char DLPF_FS_SEL_0 = 1<<3;
char DLPF_FS_SEL_1 = 1<<4;

//I2C devices each have an address. The address is defined in the datasheet for the device. The ITG-3200 breakout board can have different address depending on how
//the jumper on top of the board is configured. By default, the jumper is connected to the VDD pin. When the jumper is connected to the VDD pin the I2C address
//is 0x69.
char itgAddress = 0x69;


//In the setup section of the sketch the serial port will be configured, the i2c communication will be initialized, and the itg-3200 will be configured.
void setup()
{
//Create a serial connection using a 9600bps baud rate.
Serial.begin(9600);
delay(1000);
//Initialize the I2C communication. This will set the Arduino up as the 'Master' device.
Serial.println('1');
Wire.begin();
Serial.println('2');
//Read the WHO_AM_I register and print the result
char id=0;
id = itgRead(itgAddress, 0x00);
Serial.print("ID: ");
Serial.println(id, HEX);
//Configure the gyroscope
//Set the gyroscope scale for the outputs to +/-2000 degrees per second
itgWrite(itgAddress, DLPF_FS, (DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0));
Serial.println('3');
//Set the sample rate to 100 hz
itgWrite(itgAddress, SMPLRT_DIV, 9);
Serial.println('4');
}

//The loop section of the sketch will read the X,Y and Z output rates from the gyroscope and output them in the Serial Terminal
void loop()
{
//Create variables to hold the output rates.
// Serial.println('a');
int xRate, yRate, zRate;

//Read the x,y and z output rates from the gyroscope.
// Serial.println('b');
xRate = readX();
yRate = readY();
zRate = readZ();
// Serial.println('c');

//Print the output rates to the terminal, seperated by a TAB character.
Serial.print(xRate);
Serial.print('\t');
Serial.print(yRate);
Serial.print('\t');
Serial.println(zRate);

//Wait 10ms before reading the values again. (Remember, the output rate was set to 100hz and 1reading per 10ms = 100hz.)
delay(10);
}

//This function will write a value to a register on the itg-3200.
//Parameters:
// char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
// char registerAddress: The address of the register on the sensor that should be written to.
// char data: The value to be written to the specified register.
void itgWrite(char address, char registerAddress, char data)
{
//Initiate a communication sequence with the desired i2c device
Wire.beginTransmission(address);
//Tell the I2C address which register we are writing to
Wire.write(registerAddress);
//Send the value to write to the specified register
Wire.write(data);
//End the communication sequence
Wire.endTransmission();
}

//This function will read the data from a specified register on the ITG-3200 and return the value.
//Parameters:
// char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
// char registerAddress: The address of the register on the sensor that should be read
//Return:
// unsigned char: The value currently residing in the specified register
unsigned char itgRead(char address, char registerAddress)
{
//This variable will hold the contents read from the i2c device.
unsigned char data=0;
// Serial.println("r1");
//Send the register address to be read.
Wire.beginTransmission(address);
//Send the Register Address
// Serial.println("r2");
Wire.write(registerAddress);
//End the communication sequence.
// Serial.println("r3");
Wire.endTransmission();
// Serial.println("r4");
//Ask the I2C device for data
Wire.beginTransmission(address);
// Serial.println("r4.1");
Wire.requestFrom(address, 1);
// Serial.println("r5");
//Wait for a response from the I2C device
if(Wire.available()){
// Serial.println("r6");
//Save the data sent from the I2C device
data = Wire.read();
}

//End the communication sequence.
Wire.endTransmission();
// Serial.println("r7");
//Return the data read during the operation
return data;
}

//This function is used to read the X-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second.
//Usage: int xRate = readX();
int readX(void)
{
int data=0;
data = itgRead(itgAddress, GYRO_XOUT_H)<<8;
data |= itgRead(itgAddress, GYRO_XOUT_L);

return data;
}

//This function is used to read the Y-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second.
//Usage: int yRate = readY();
int readY(void)
{
int data=0;
data = itgRead(itgAddress, GYRO_YOUT_H)<<8;
data |= itgRead(itgAddress, GYRO_YOUT_L);

return data;
} }


void loop(){ //This function is used to read the Z-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second.
//Usage: int zRate = readZ();
int readZ(void)
{
int data=0;
data = itgRead(itgAddress, GYRO_ZOUT_H)<<8;
data |= itgRead(itgAddress, GYRO_ZOUT_L);

return data;
} }
8 changes: 3 additions & 5 deletions asincI2C/twi.c
Expand Up @@ -72,11 +72,9 @@ static boolean twi_receiving, twi_writing;
void twi_init(boolean pullUp, boolean fastI2C) void twi_init(boolean pullUp, boolean fastI2C)
{ {
unsigned long frequency = 100000; unsigned long frequency = 100000;
if (fastI2C){
if (fastI2C)
frequency = 400000; frequency = 400000;
}else{
frequency = 100000;
}


// initialize state // initialize state
twi_state = TWI_READY; twi_state = TWI_READY;
Expand Down Expand Up @@ -215,7 +213,7 @@ uint8_t twi_AsincReadNotReady(uint8_t length){
return 2; //error, waiting for right status return 2; //error, waiting for right status


//wait until all data arrive //wait until all data arrive
if (twi_masterBufferIndex < length){ if (twi_masterBufferIndex < length-1){
return 3; //error, missing some data return 3; //error, missing some data
} }


Expand Down

0 comments on commit 35456db

Please sign in to comment.