0

我正在使用将 Arduino 库转换为MSP430库的 Energia库。我将陀螺仪连接到Launchpad并使用所需的上拉电阻构建电路。以下是 Sparkfun 陀螺仪的示例代码:

//The Wire library is used for I²C communication
#include <Wire.h>

//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;

//I²C 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 I²C address
//is 0x69.
char itgAddress = 0x69;

//In the setup section of the sketch the serial port will be configured, the I²C
//communication will be initialized, and the ITG-3200 will be configured.
void setup()
{
    //Create a serial connection using a 9600 bit/s baud rate.
    Serial.begin(9600);

    //Initialize the I²C communication. This will set the Arduino up as the 'Master'  device.
    Wire.begin();

    //Read the WHO_AM_I register and print the result
    char id=4;
    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));
    //Set the sample rate to 100 Hz
    //itgWrite(itgAddress, SMPLRT_DIV, 9);
}

//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.
    //int xRate, yRate, zRate;

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

    //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 10 ms before reading the values again. (Remember, the output rate was set 
    //to 100 Hz and one reading per 10 ms = 100 Hz.)
    char id = 4;
    id = itgRead(itgAddress, 0x00);
    Serial.print("ID: ");
    Serial.println(id, HEX);
    delay(1000);
}

//This function will write a value to a register on the itg-3200.
//
//Parameters:
//  char address: The I²C 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 I²C device
    Wire.beginTransmission(address);
    //Tell the I²C 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 I²C 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 I²C device.
    unsigned char data = 5;

    //Send the register address to be read.
    Wire.beginTransmission(address);

    //Send the Register Address
    Wire.write(registerAddress);

    //End the communication sequence.
    Wire.endTransmission();

    //Ask the I²C device for data
    //Wire.beginTransmission(address);
    Wire.requestFrom(address, 1);

    //Wait for a response from the I²C device
    if(!Wire.available()){
        //Save the data sent from the I²C device
        data = Wire.read();
    }

    //End the communication sequence.
    // Wire.endTransmission();

    //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;
}

//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;
}

我将示波器连接到 SDA 和 SCL 总线上以测量结果是否正确。在前 8 个 SCL 周期中,SDA 总线是我所期望的,7 位从地址和 1 位读取。但是对应于来自陀螺仪的 ACK 的第 9 个 SCL 周期应该为低的 SDA 似乎是错误的。陀螺仪永远不会将 SDK 降低到低电平以确认主机。

我最近买了这个产品,所以我认为它没有问题。我猜主人从不释放 SDA 总线来检查 ACK。我在这里想念什么?

4

0 回答 0