Hi,
I am trying to communicate with a L3G4200D gyrosensor (which is on the minimu-9 module: http://www.pololu.com/catalog/pr...) using an Atmega328P.
I am trying to read one byte from the gyrosensor. However, I am kind of confused when sending the address of the gyrosensor via TWI (I'm not sure how to do so!). According to the gyrosensor's datasheet, by default the address is 1101001b. When I call my ReadByte function, I am getting a value of just "1" everytime though. I am assigning the address like:
uint8_t address = 0x1101001;
Can you please help me get on the right track?
Here are the important segments of my current code:
TWI Set-up functions
// setups TWI (Two Wire Interface A.K.A I2C) void TWISetup(){ // Set up TWI module to 27.9KHz // SCLfreq = Fclock/(16+2*TWBR*(prescaler)) // Set up TWI but rate to 2 TWBR = 4; // Prescaler to 64 TWSR |= (1<<TWPS1) | (1<<TWPS0); // Enable TWI TWCR |= (1<<TWEN); } // Start signal function void TWIStart(){ // From datasheet page 226 TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // Send start condition while (!(TWCR & (1<<TWINT))); // Wait for TWINT Flag set. This indicates that the START condition has been transmitted } void TWIStop(){ TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // Transmit STOP condition } void TWIWrite(uint8_t data){ //Load SLA_W into TWDR Register. Clear TWINT bit in //TWCR to start transmission of address TWDR = data; TWCR = (1<<TWINT) | (1<<TWEN); //Wait for TWINT Flag set. This indicates that the SLA+W has //been transmitted, and ACK/NACK has been received. while (!(TWCR & (1<<TWINT))); } // Read with ACK (Acknowledge bit) uint8_t TWIReadACK(){ //TWI Interrupt Flag, TWI Enable Bit, TWI Enable Acknowledge Bit TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA); //Wait for TWINT Flag set. while (!(TWCR & (1<<TWINT))); return TWDR; } //read byte with NACK uint8_t TWIReadNACK() { TWCR = (1<<TWINT)|(1<<TWEN); //Wait for TWINT Flag set. while (!(TWCR & (1<<TWINT))); return TWDR; }
Read byte from gyro:
uint8_t ReadByte(uint8_t address,uint8_t *data){ TWIStart(); // Starts TWI if (TWIReadStatus() != 0x08) return -1; //send address TWIWrite(address); if (TWIReadStatus() != 0x28) return -1; // Send Start TWIStart(); if (TWIReadStatus() != 0x10) return -1; //select device and send read bit TWIWrite(address|1); if (TWIReadStatus() != 0x40) return -1; //Reads *data = TWIReadACK(); if (TWIReadStatus() != 0x58) return -1; TWIStop(); return 0; }
This is where I am trying to read each byte:
uint8_t data; uint8_t address = 0x1101001; while(1){ ReadByte(address,&data); }
Thanks in advance!