AnsweredAssumed Answered

I2C problem

Question asked by YuJ on Dec 3, 2012
Latest reply on Dec 11, 2012 by YuJ
I am a beginner about the microcontoroller. I use STBee. I want to connect a STBee and a sensor(IMU 6DOF ITG3200/ADXL345).
At first I want to let you display a value of the x-axis.
Please teach a solution.

#include "stm32f10x.h"
#include "platform_config.h"
#include "com_config.h"
#include "delay.h"

void I2C_Configuration(void);
void writeDevice(uint8_t, uint8_t , uint8_t);
uint8_t readDevice(uint8_t, uint8_t);
//ITG-3200
uint8_t WHO_AM_I    = 0x00;
uint8_t SMPLRT_DVI  = 0x15;
uint8_t DLPF_FS     = 0x16;
uint8_t TEMP_OUT_H  = 0x1B;
uint8_t TEMP_OUT_L  = 0x1C;
uint8_t GYRO_XOUT_H = 0x1D;
uint8_t GYRO_XOUT_L = 0x1E;
uint8_t GYRO_YOUT_H = 0x1F;
uint8_t GYRO_YOUT_L = 0x20;
uint8_t GYRO_ZOUT_H = 0x21;
uint8_t GYRO_ZOUT_L = 0x22;

//ADXL345
uint8_t DEVID       = 0x00;
uint8_t BW_RATE     = 0x2C;
uint8_t POWER_CTL   = 0x2D;
uint8_t INT_ENABLE  = 0x2E;
uint8_t DATA_FORMAT = 0x31;
uint8_t DATAX0      = 0x32;
uint8_t DATAX1      = 0x33;
uint8_t DATAY0      = 0x34;
uint8_t DATAY1      = 0x35;
uint8_t DATAZ0      = 0x36;
uint8_t DATAZ1      = 0x37;

uint8_t gyroAddress = 0x68;
uint8_t accAddress  = 0x53;

int main(void)
{
    uint16_t X=0;
    BoardInit();
    COM_Configuration();
    cprintf(Welcome_Message);
    I2C_Configuration();
    //ITG-3200
    writeDevice(gyroAddress,DLPF_FS,0x1B);
    writeDevice(gyroAddress,SMPLRT_DVI,0x09);
    //ADXL345
    writeDevice(accAddress,DATA_FORMAT,0x0B);
    writeDevice(accAddress,BW_RATE,0x0A);
    writeDevice(accAddress,POWER_CTL,0x08);
    writeDevice(accAddress,INT_ENABLE,0x80);
while(1){
    X = (uint16_t)(readDevice(accAddress,DATAX1))<<8 | (uint16_t)(readDevice(accAddress,DATAX0));

    cprintf("X = %d/n",X);
    delay_ms(300);
}
}

void writeDevice(uint8_t address, uint8_t registerAddress, uint8_t data){

    I2C_GenerateSTART(I2C1,ENABLE);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2C1,address,I2C_Direction_Transmitter);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    I2C_SendData(I2C1,registerAddress);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_SendData(I2C1,data);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    I2C_GenerateSTOP(I2C1,ENABLE);
}

uint8_t readDevice(uint8_t address, uint8_t registerAddress){
    uint8_t data=0;
    while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
    I2C_GenerateSTART(I2C1,ENABLE);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2C1,address,I2C_Direction_Transmitter);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    I2C_SendData(I2C1,(uint8_t)registerAddress);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_GenerateSTART(I2C1,ENABLE);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2C1,address,I2C_Direction_Receiver);
    while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    I2C_AcknowledgeConfig(I2C1,DISABLE);
    I2C_GenerateSTOP(I2C1,ENABLE);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));
    I2C_AcknowledgeConfig(I2C1, DISABLE);
    data=(uint8_t) I2C_ReceiveData(I2C1);
    return data;
}

void I2C_Configuration(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  I2C_InitTypeDef  I2C_InitStructure;

  /* I2C Periph clock enable */
  RCC_APB1PeriphClockCmd(I2C1_RCC, ENABLE);
  /* GPIO Periph clock enable */
  RCC_APB2PeriphClockCmd(I2C1_GPIO_RCC, ENABLE);

  /* Configure I2C pins: SCL and SDA */
  GPIO_InitStructure.GPIO_Pin =  I2C1_SCL_PIN | I2C1_SDA_PIN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  GPIO_Init(I2C1_PORT, &GPIO_InitStructure);

#if defined (REMAP_I2C1)
Remap_I2C1_Configuration();
#endif

  /* I2C configuration */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = 50000;

  /* Apply I2C configuration */
  I2C_Init(I2C1, &I2C_InitStructure);

  /* I2C Peripheral Enable */
  I2C_Cmd(I2C1, ENABLE);
}

Outcomes