cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f0 problem with i2c connection...

teacha
Associate II
Posted on May 16, 2013 at 13:11

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

8 REPLIES 8
Posted on May 16, 2013 at 15:54

in which place I am doing something wrong?

The basic problem is you're jamming stuff into the I2C peripheral without regard to the status and progress.

while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_***));

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
neil
Associate
Posted on May 30, 2013 at 12:53

Did you get your code working after implimenting the advice from Clive1 ?

Could you post your revised code ?  I have been trying to use it to get going with I2C on the STM32F0xx discovery board.

I have a DS1340 RTC clock chip hooked up to my board.  I know how to use the chip as I have it working fine from a PIC.  However when connected to the STM32 all I can see on my scope is the first 10 clocks but not the slave address address I expect.

teacha
Associate II
Posted on June 02, 2013 at 22:23

now im trying in this way.. but something its wrong.. on a terminal im getting only ''error2''.. any ideas? ;)


#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_RA_WHO_AM_I 0x75
#define Slave_address 0x68
#define mpu6050_FLAG_TIMEOUT ((uint32_t)0x1000)
#define mpu6050_LONG_TIMEOUT ((uint32_t)(500 * mpu6050_FLAG_TIMEOUT))
void USART_send( unsigned char data){
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, data);
}
void USART_print(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(' ');
}
uint8_t ReadReg(uint8_t RegName)
{
uint8_t mpu6050_BufferRX[2] ={0,0};
uint8_t tmp = 0;
uint32_t DataNum = 0;
int mpu6050_Timeout = 0;
/* Test on BUSY Flag */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error1 '');
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2C1, Slave_address, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
/* Wait until TXIS flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error2 '');
}
/* Send Register address */
I2C_SendData(I2C1, (uint8_t)RegName);
/* Wait until TC flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TC) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error3 '');
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(I2C1, Slave_address, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
/* Reset local variable */
DataNum = 0;
/* Wait until all data are received */
while (DataNum != 1)
{
/* Wait until RXNE flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_RXNE) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error4 '');
}
/* Read data from RXDR */
mpu6050_BufferRX[DataNum]= I2C_ReceiveData(I2C1);
/* Update number of received data */
DataNum++;
}
/* Wait until STOPF flag is set */
mpu6050_Timeout = mpu6050_LONG_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET)
{
if((mpu6050_Timeout--) == 0) USART_print('' error5 '');
}
/* Clear STOPF flag */
I2C_ClearFlag(I2C1, I2C_ICR_STOPCF);
// !< Store LM75_I2C received data
//tmp = (uint16_t)(LM75_BufferRX[0] << 8);
//tmp |= LM75_BufferRX[0];
tmp = mpu6050_BufferRX[0];
// return a Reg value
return (uint8_t)tmp;
}
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);
/* Configure the I2C clock source. The clock is derived from the HSI */
RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
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_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; // 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_Timing = 0x20310A0D;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
int main(void)
{
LED_init();
USART_init();
I2C_init();
int i=0;
while(1)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
ReadReg(MPU6050_RA_WHO_AM_I);
for(i=0;i<10000000;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++);
*/
}
}

sanjib
Associate III
Posted on May 28, 2014 at 15:33

Hey it's working now if it is working please send me the code

sanjib
Associate III
Posted on May 29, 2014 at 12:10 OK,I found the problem. Flag I2C_ISR_TXIS did not get because the device on the i2c did not respond to the sent address. Need to convert the Slave address to 7 bits.

(SlaveAddr & 0x7f) << 1

bg
Associate
Posted on September 21, 2014 at 14:47

can you send it also to me.

sanjib
Associate III
Posted on November 07, 2014 at 09:23

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6iA&d=%2Fa%2F0X0000000btl%2FqavVvCQ6cHEM7XjwY1mrv1hx_hQL8ATOFkACHEiLfI0&asPdf=false
chen
Associate II
Posted on November 07, 2014 at 13:06

Hi

1. Please start a new thread since this is a new problem/discussion (it is tiresome to read through the entire thread to find this is a new thing!).

2. You have not said which device you are using

3. ''when I am using pb6 and pb7 pins as SCL and SDA respectively but when i use Pb10 and pb11 for SCL and SDA it is now working why. could any body say why?''

For a STM32F405 PB6/7 are connected to I2C_1

BUT PB10/11 are connected to I2C_2