cancel
Showing results for 
Search instead for 
Did you mean: 

Stm32f4 adxl345 i2c not working? Std periph

HGök.1
Associate II

I wrote code using the stm32f4 discovery card and adxl345 sensor using the i2c communication protocol. But Send data does not respond to my command. Please help me. I'll be crazy.

#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
 
 
#define Max  50
#define adxl_address 0x53<<1   //0xA6
 
GPIO_InitTypeDef  GPIOStructure;
I2C_InitTypeDef	I2CStructure;
char str[Max];
uint8_t cip=0;
void Config(void){
 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
 
	GPIOStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //PB6 and PB7
	GPIOStructure.GPIO_Mode = GPIO_Mode_AF;   // set to alternate function
	GPIOStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIOStructure.GPIO_OType = GPIO_OType_OD;   //set to open drain
	GPIOStructure.GPIO_PuPd = GPIO_PuPd_UP;   // pull up resistors
	GPIO_Init(GPIOB, &GPIOStructure);
 
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
 
 
	I2CStructure.I2C_ClockSpeed = 100000;
	I2CStructure.I2C_Mode = I2C_Mode_I2C;
	I2CStructure.I2C_DutyCycle = I2C_DutyCycle_2;
	I2CStructure.I2C_OwnAddress1 = 0x00;
	I2CStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2CStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_Init(I2C1, &I2CStructure);
 
	I2C_Cmd(I2C1, ENABLE);
}
void write_i2c(I2C_TypeDef* I2Cx,uint8_t address, uint8_t data){
 
 
    // wait until I2C1 is not busy anymore
 while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
 
  // I2C1 enable
 I2C_GenerateSTART(I2Cx, ENABLE);
 
 // wait for I2C1 EV5 --> Slave has acknowledged start condition
 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
 
 // Send slave Address for write
 I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Transmitter);
 
 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
 
 I2C_SendData(I2Cx, data);
 // wait for I2C1 EV8_2 --> byte has been transmitted
 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
 
 I2C_GenerateSTOP(I2Cx, ENABLE);
 
}
 
 
int read_i2c(I2C_TypeDef* I2Cx,uint8_t address)
{
 // wait until I2C1 is not busy anymore
 while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
 
 // I2C1 enable
 I2C_GenerateSTART(I2Cx, ENABLE);
 
 // wait for I2C1 EV5 --> Slave has acknowledged start condition
 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
 
 // Send slave Address for read
 I2C_Send7bitAddress(I2Cx, address, I2C_Direction_Receiver);
 
 
 while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
 
 I2C_AcknowledgeConfig(I2Cx, DISABLE);
 
 I2C_GenerateSTOP(I2Cx, ENABLE);
 
 // wait until one byte has been received
 while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
 
 // read data from I2C data register
 uint8_t data = I2C_ReceiveData(I2Cx);
 return data;
 
}
uint16_t x0,x1,Y0,Y1,z0,z1;
int main(void)
{
 
 Config();
 
   
     write_i2c(I2C1,0x2d, 0x00);
	 write_i2c(I2C1,0x2d, 0x08);
	 write_i2c(I2C1,0x31, 0x01);
  while (1)
  {
	   
	   
	     x0 = read_i2c(I2C1,adxl_address);
	     write_i2c(I2C1,adxl_address,0x33);
	     x1 = read_i2c(I2C1,adxl_address);
	     write_i2c(I2C1,adxl_address,0x34);
	     Y0 = read_i2c(I2C1,adxl_address);
	     write_i2c(I2C1,adxl_address,0x35);
	     Y1 = read_i2c(I2C1,adxl_address);
	     write_i2c(I2C1,adxl_address,0x36);
	     z0 = read_i2c(I2C1,adxl_address);
	     write_i2c(I2C1,adxl_address,0x37);
	     z1 = read_i2c(I2C1,adxl_address);
    
	    
 
  }
}
 
 
 
 
 
 
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size){
  /* TODO, implement your code here */
  return;
}
 
/*
 * Callback used by stm324xg_eval_audio_codec.c.
 * Refer to stm324xg_eval_audio_codec.h for more info.
 */
uint16_t EVAL_AUDIO_GetSampleCallBack(void){
  /* TODO, implement your code here */
  return -1;
}

0 REPLIES 0