cancel
Showing results for 
Search instead for 
Did you mean: 

Connection of an accelerometer MMA8452Q to the correspondent STM32 peripheral

ilvuciis
Associate
Posted on November 17, 2016 at 21:02

I have some issues with the STM32 code for Connection of an accelerometer MMA8452Q to the correspondent STM32 peripheral I2C communication.

What is missing?

#include ''stm32f10x.h''

#include ''lcd.h''

#include ''stdio.h''

volatile u8 X, Y, Z;

void usart_config(){

void RCC_Config_HSE_PLL_Max()

{

RCC_DeInit();

RCC_HSEConfig(RCC_HSE_ON);

ErrorStatus HSEStartUpStatus;

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)

{

FLASH_SetLatency(FLASH_Latency_2);

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

RCC_HCLKConfig(RCC_SYSCLK_Div1);

RCC_PCLK1Config(RCC_HCLK_Div1);

RCC_PCLK2Config(RCC_HCLK_Div1);

RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_12);

RCC_PLLCmd(ENABLE);

while( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET );

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource() != 0x08);

}

else

while(1);

}

//configuration of the LED

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//USART configuration

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

USART_InitTypeDef USART_InitStructure;

//default baudrate - 9600

USART_InitStructure.USART_BaudRate = 9600;

//databits - 8

USART_InitStructure.USART_WordLength = USART_WordLength_8b;

//stopbits - 1

USART_InitStructure.USART_StopBits = USART_StopBits_1;

//odd parity

USART_InitStructure.USART_Parity = USART_Parity_Odd;

//no hardware flowcontrol

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

//both receiving and transmitting mode

USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

USART_Init(USART2, &USART_InitStructure);

USART_Cmd(USART2, ENABLE);

//USART interruption configuration

NVIC_InitTypeDef NVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

}

void i2cconfig()

{

    GPIO_InitTypeDef  GPIO_InitStructure;

    I2C_InitTypeDef  I2C_InitStructure;

    /*enable I2C*/

    I2C_Cmd(I2C2,ENABLE);

    /* I2C2 clock enable */

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

    // I2C2 SDA and SCL configuration, SCL is pin11 and SDA is pin 12 for I2C2

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

    I2C_InitStructure.I2C_ClockSpeed = 500000;

    I2C_InitStructure.I2C_OwnAddress1 = 0;

    I2C_Init(I2C2, &I2C_InitStructure);

    I2C_Cmd(I2C2, ENABLE);

}

void I2C_receiver_one()

{

I2C_GenerateSTART(I2C2, ENABLE); //Start condition to start communicating with the slave

while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)));

I2C_Send7bitAddress(I2C2, 0x1D, I2C_Direction_Transmitter);

 while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

I2C_SendData(I2C2,0x2A);

while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_GenerateSTART(I2C2, ENABLE); //Start condition to start communicating with the slave

while(!(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)));

I2C_Send7bitAddress(I2C2, 0x1D, I2C_Direction_Receiver);

    // Test Receive mode Flag

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED));

  X = I2C_ReceiveData(I2C2);

  I2C_AcknowledgeConfig(I2C2, DISABLE);

I2C_GenerateSTOP(I2C2, ENABLE);

while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF)); // stop bit flag

}

int main(void)

{

RCC_Config_HSE_PLL_Max();

lcd_init ();

i2cconfig();

usart_config();

I2C_receiver_one();

while(1)

   {

   }

}

And the handler:

extern X;

extern Y;

extern Z;

void USART2_IRQHandler(void)

{

//variables for the received data and for the switch status

volatile uint8_t RxData;

//if it is receiving

if(USART_GetITStatus(USART2, USART_IT_RXNE)== SET)

{

// while it is still receiving data do....

while( USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET );

//attribution of the received data to the rxdata variable

    RxData = USART_ReceiveData(USART2);

  if (RxData=='a')

  {

  GPIO_SetBits(GPIOA,GPIO_Pin_2);

  //u8 X, Y, Z;

  char buffer1 [10];

  char buffer2 [10];

  char buffer3 [10];

  sprintf(buffer1,''X = %d'', X);

  sprintf(buffer2,''Y = %d'', Y);

  sprintf(buffer3,''Z = %d'', Z);

lcd_draw_string(5,20,buffer1,1000,1);

  lcd_draw_string(5,60,buffer2,1000,1);

    lcd_draw_string(5,100,buffer3,1000,1);

    while( USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET );

    RxData = USART_ReceiveData(USART2);

    USART_ClearFlag(USART2,USART_FLAG_RXNE);

    }

USART_ClearFlag(USART2,USART_FLAG_RXNE);

USART_ClearITPendingBit(USART2, USART_IT_RXNE);

}

}

1 REPLY 1
Walid FTITI_O
Senior II
Posted on November 29, 2016 at 12:03

Hi aleksandrova.ilva, 

You would refer to the demonstration example in 

http://www.st.com/en/embedded-software/stm32cubef4.html

at this path:

STM32Cube_FW_F4_V1.14.0\Projects\STM32F4-Discovery\Demonstrations

This demonstartion example interface the MEMS sensor (LIS302DL for revB or LIS3DSCH for revC), when you move the board you observe the four Leds blinking according to the motion direction.

You may then interface the MMA8452Q the same way as LIS302DL.

-Hannibal-