AnsweredAssumed Answered

stm32f0 problem with i2c connection...

Question asked by wilczynski.andrzej on May 16, 2013
Latest reply on Nov 7, 2014 by sung.chen_chung
on the terminal always gets only same 0. in which place I am doing something wrong? please help!



#include "stm32f0xx.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx_usart.h"
#include "stm32f0xx_i2c.h"
#include <stdio.h>
 
 
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
I2C_InitTypeDef  I2C_InitStructure;
 
 
#define MPU6050_W  0x68  //0xD0
#define MPU6050_R  0x69
#define MPU6050_RA_WHO_AM_I         0x75
#define SLAVE_ADDRESS7  0x68<<1
 
 
void USART_send( unsigned char data){
 
 
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    USART_SendData(USART1, data);
 
 
}
 
 
void USART_putstring(char* StringPtr){
 
 
while(*StringPtr != 0x00){
 USART_send(*StringPtr);
 StringPtr++;}
 
 
}
 
 
void frej(uint8_t value)
{
    int maska=0x01;
    int i;
    for(i=7;i>=0;i--){
        int l = (((int)value)>>i)&maska;
        char t = (char)(l+48);
        USART_send(t);
    }
 
 
    USART_send(' ');
}
 
 
void SingleRead(uint8_t RegisterAddr) {
    /*
         1. Send a start sequence
         2. Send I2C address of the device with the R/W bit low (W address)
         3. Send Internal address of the bearing register
         4. Send a start sequence again (repeated start)
         5. Send I2C address of the device with the R/W bit high (R address)
         6. Read data byte from i2c
         7. Send the stop sequence
    */
     
    I2C_GenerateSTART(I2C1, ENABLE);
 
 
    //I2C_AcknowledgeConfig(I2C1, ENABLE);
    I2C_SendData(I2C1, MPU6050_W);
 
 
    I2C_SendData(I2C1, RegisterAddr);
     
    I2C_GenerateSTART(I2C1, ENABLE);
 
 
    I2C_SendData(I2C1, MPU6050_R);
 
 
    frej(I2C_ReceiveData(I2C1));
    //I2C_AcknowledgeConfig(I2C1, DISABLE);
 
 
    I2C_GenerateSTOP(I2C1, ENABLE);
 
 
}
 
 
 
 
 
 
void LED_init(void){
 
 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
 
 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOC, &GPIO_InitStructure);
}
 
 
void USART_init(void){
 
 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
 
 
    //Configure pins:  Rx and Tx --------------------------
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 
    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);
 
 
    USART_Cmd(USART1,ENABLE);
}
 
 
void I2C_init(void){
 
 
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
 
 
    //Configure pins: SCL and SDA ------------------
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
 
 
    I2C_Cmd(I2C1, ENABLE);
 
 
    I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Disable;
    I2C_InitStructure.I2C_DigitalFilter = 0x00;
    I2C_InitStructure.I2C_OwnAddress1 = 0x68; // MPU6050 7-bit adress = 0x68, 8-bit adress = 0xD0;
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_Timing = 0xA0120227;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_Init(I2C1, &I2C_InitStructure);
 
 
}
 
 
 
 
 
 
 
 
int main(void)
{
 
 
      LED_init();
      USART_init();
      I2C_init();
 
 
 
 
 
 
 
 
      while(1)
      {
 
 
 
 
           
 
 
 
 
       while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
     
            
       SingleRead(MPU6050_RA_WHO_AM_I);  
 
 
 
 
           /*
 
 
           int i=0;
           if(USART_ReceiveData(USART1)=='x'){
 
 
               GPIO_SetBits(GPIOC, GPIO_Pin_8);
           }
           for(i=0;i<1000000;i++);
 
 
           GPIO_ResetBits(GPIOC, GPIO_Pin_8);
 
 
           for(i=0;i<1000000;i++);
 
 
           */
      }
}

Outcomes