roboforum.ru

Технический форум по робототехнике.

ITG3205 проблема получения данных c гироскопа

Программирование микроконтроллеров AVR, PIC, ARM.
Разработка и изготовление печатных плат для модулей.

ITG3205 проблема получения данных c гироскопа

Сообщение gppsoft » 24 апр 2016, 07:21

Парни. Не могу понять как получить данные с этого гироскопа. Я не использую arduino библиотеки и пишу в AVRStudio. Проблема в том, что у меня не считываются данные. Не могу понять почему. Вот в этом примере все все работает. Не могу понять в чем разница. Помогите разобраться пожалуйста.

I2C:

Код: Выделить всёРазвернуть
void I2C::init(){
   TWBR = (uint8_t)TWBR_val;
}

bool I2C::startTransmit(uint8_t address)
{
   // reset TWI control register
   TWCR = 0;
   // transmit START condition
   TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
   // wait for end of transmission
   while( !(TWCR & (1<<TWINT)) );
   
   // check if the start condition was successfully transmitted
   if((TWSR & 0xF8) != TW_START){ return false; }
   
   // load slave address into data register
   TWDR = address;
   // start transmission of address
   TWCR = (1<<TWINT) | (1<<TWEN);
   // wait for end of transmission
   while( !(TWCR & (1<<TWINT)) );
   
   // check if the device has acknowledged the READ / WRITE mode
   uint8_t twst = TW_STATUS & 0xF8;
   if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return false;
   
   return true;
}

void I2C::startTransmitWait(uint8_t address)
{
   uint8_t   twst;

   while ( 1 )
   {
      // send START condition
      TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
      
      // wait until transmission completed
      while(!(TWCR & (1<<TWINT)));
      
      // check value of TWI Status Register. Mask prescaler bits.
      twst = TW_STATUS & 0xF8;
      if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
      
      // send device address
      TWDR = address;
      TWCR = (1<<TWINT) | (1<<TWEN);
      
      // wail until transmission completed
      while(!(TWCR & (1<<TWINT)));
      
      // check value of TWI Status Register. Mask prescaler bits.
      twst = TW_STATUS & 0xF8;
      if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
      {
         /* device busy, send stop condition to terminate write operation */
         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
         
         // wait until stop condition is executed and bus released
         while(TWCR & (1<<TWSTO));
         
         continue;
      }
      //if( twst != TW_MT_SLA_ACK) return 1;
      break;
   }

}/* i2c_start_wait */

void I2C::stopTransmit()
{
    /* send stop condition */
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
    // wait until stop condition is executed and bus released
    while(TWCR & (1<<TWSTO));
}

bool I2C::write(uint8_t data)
{
   // load data into data register
   TWDR = data;
   // start transmission of data
   TWCR = (1<<TWINT) | (1<<TWEN);
   // wait for end of transmission
   while( !(TWCR & (1<<TWINT)) );
   
   if( (TWSR & 0xF8) != TW_MT_DATA_ACK ){ return false; }
   
   return true;
}

uint8_t I2C::readAck()
{
   // start TWI module and acknowledge data after reception
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
   // wait for end of transmission
   while( !(TWCR & (1<<TWINT)) );
   // return received data from TWDR
   return TWDR;
}

uint8_t I2C::readNAck()
{
   // start receiving without acknowledging reception
   TWCR = (1<<TWINT) | (1<<TWEN);
   // wait for end of transmission
   while( !(TWCR & (1<<TWINT)) );
   // return received data from TWDR
   return TWDR;
}



ITG3205:
Код: Выделить всёРазвернуть
#include "ITG3205.h"


void ITG3205::init(I2C *i2c, UART *uart){
   this->i2c=i2c;
   this->uart=uart;
   
   uart->writeLn("Init ITG3205");
   
   i2c->startTransmit(GYRO_ADDR);
   i2c->write(G_PWR_MGM);
   i2c->write(0x00);
   i2c->stopTransmit();
   
   i2c->startTransmit(GYRO_ADDR);
   i2c->write(G_SMPLRT_DIV);
   i2c->write(0x07);
   i2c->stopTransmit();
   
   i2c->startTransmit(GYRO_ADDR);
   i2c->write(G_DLPF_FS);
   i2c->write(0x1E);
   i2c->stopTransmit();
   
   i2c->startTransmit(GYRO_ADDR);
   i2c->write(G_INT_CFG);
   i2c->write(0x00);
   i2c->stopTransmit();
}

void ITG3205::getXdata(uint8_t *lowByte, uint8_t *hightByte){
   
   //========Получение младшего байта X====
   i2c->startTransmit(GYRO_ADDR);
   i2c->write(GYRO_XOUT_L);
   i2c->stopTransmit();
   
   i2c->startTransmit(GYRO_ADDR);
   *lowByte = i2c->readAck();
   i2c->stopTransmit();
   //======================================
   
   //========Получение старшего байта X====
   i2c->startTransmit(GYRO_ADDR);
   i2c->write(GYRO_XOUT_H);
   i2c->stopTransmit();
   
   i2c->startTransmit(GYRO_ADDR);
   *hightByte = i2c->readAck();
   i2c->stopTransmit();
   //======================================
}


Где-то в коде:
Код: Выделить всёРазвернуть
uint8_t xLow;
uint8_t xHight;
      
itg3205.getXdata(&xLow,&xHight);
      
dtostrf(xLow, 3, 5, itmp); uart.write(itmp);  uart.write(' ');
dtostrf(xHight, 3, 5, itmp);  uart.write(itmp);  uart.write(' ');
      
uart.write("\r\n");
      
_delay_ms(50);


Результат:

104.00000 104.00000
104.00000 104.00000
104.00000 104.00000
104.00000 104.00000

все время вот эти числа :(

Добавлено спустя 47 минут:
Пробовал еще так:
Код: Выделить всёРазвернуть
void ITG3205::getXdata(int *gx, int *gy, int *gz, int *gt){
   
   uint8_t i = 0;
   uint8_t buff[8];

   i2c->startTransmit(GYRO_ADDR);
   i2c->write(0x1B);
   i2c->startTransmit(GYRO_ADDR);
   for(i=0; i<8; i++) {
      if(i==8-1)
         buff[i] = i2c->readNAck();
      else
         buff[i] = i2c->readNAck();
   }
   i2c->stopTransmit();

   *gx = ((buff[2] << 8) | buff[3]);
   *gy= ((buff[4] << 8) | buff[5]);
   *gz = ((buff[6] << 8) | buff[7]);
   *gt = (buff[0] << 8) | buff[1];

}



Код: Выделить всёРазвернуть
int x;
      int y;
      int z;
      int temp;
      
      itg3205.getXdata(&x,&y,&z,&temp);
      
      dtostrf(x, 3, 5, itmp); uart.write(itmp);  uart.write(' ');
      dtostrf(y, 3, 5, itmp);  uart.write(itmp);  uart.write(' ');
      dtostrf(z, 3, 5, itmp);  uart.write(itmp);  uart.write(' ');
      dtostrf(temp, 3, 5, itmp);  uart.write(itmp);  uart.write(' ');
      
      uart.write("\r\n");
      
      _delay_ms(50);


Теперь другое число во всех переменных.

26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
26728.00000 26728.00000 26728.00000 26728.00000
gppsoft
 
Сообщения: 86
Зарегистрирован: 09 апр 2014, 07:56

Вернуться в Микроконтроллеры

Кто сейчас на конференции

Сейчас этот форум просматривают: Bing [Bot] и гости: 0

cron