cancel
Showing results for 
Search instead for 
Did you mean: 

I2C problem

s1170198
Associate II
Posted on December 03, 2012 at 10:23

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.

&sharpinclude ''stm32f10x.h''

&sharpinclude ''platform_config.h''

&sharpinclude ''com_config.h''

&sharpinclude ''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);

&sharpif defined (REMAP_I2C1)

Remap_I2C1_Configuration();

&sharpendif

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

}

#walk-before-you-run #debugging
7 REPLIES 7
rob239955_stm1
Associate II
Posted on December 03, 2012 at 17:52

What problem are you seeing?

s1170198
Associate II
Posted on December 04, 2012 at 06:03

Thank you for replying.

I apologize for not providing you with enough explanation.

I want to display the value of the acceleration of an x-axis on Tera Term.

I tried it in various ways, but it was not able to be solved.

Please teach me a solution.

Posted on December 04, 2012 at 09:18

You didn't answer the question!

What problem(s), exactly, are you seeing?

What debugging have you done in an attempt to resolve the problem(s)?

What did you discover during your debugging?

Debugging is an essential skill in any form of development - here are some tips:

http://www.8052.com/faqs/120313

http://www.eetimes.com/discussion/break-point/4027526/Developing-a-good-bedside-manner?pageNumber=0

Posted on December 04, 2012 at 09:59

''I am a beginner about the microcontoroller''

Do you have any prior experience with programming and/or electronics in general?

Before getting into I2C, accelerometers, etc, have you done the basic steps of blinking an LED and sending a string out of the serial port?

http://bit.ly/T6pTIA

s1170198
Associate II
Posted on December 05, 2012 at 04:00

I did not yet do debugging.

I do it now.

I have done the basic steps of blinking an LED and sending a string out of the serial port.

In addition, I controlled a DC motor using PWM.

s1170198
Associate II
Posted on December 05, 2012 at 04:39

Although it had forgotten to write, I was able to read data from EEPROM by I2C, being able to see a sample code.

s1170198
Associate II
Posted on December 11, 2012 at 09:50

I found the problem.

There was a problem in the portion in writeDevice().

 I2C_Send7bitAddress(I2C1,address,I2C_Direction_Transmitter);  while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

The microcomputer did not become transmitter mode here.

Please solve this problem.