cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 ,i2c and ds1621

d4ng3r09
Associate II
Posted on May 13, 2015 at 10:02

I am using an stm32f4 dicovery with a ds16As indicated in the datasheets, i specified the adress as well as the device pins.

The problem is that when i run the program ,it seems to be stuck on the the first flag test instruction:while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_MODE_SELECT));

Can somebody tell me what is wrong?

#include ''stm32f4xx.h''
#include ''stm32f4xx_i2c.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_rcc.h''
#include ''misc.h''
int main(void)
{int data=0;
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStruct;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3 , ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_I2C3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_I2C3);
I2C_InitStruct.I2C_ClockSpeed = 50000;
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0;//i tried to put 144 as well but nothing changed
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C3, &I2C_InitStruct);
I2C_Cmd(I2C3, ENABLE);
while(1)
{ I2C_GenerateSTART(I2C3, ENABLE);
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress( I2C3,0x90,I2C_Direction_Receiver);//I2C_Direction_Transmitter
while( !I2C_CheckEvent(I2C3,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C3, 0xAA);//read temperature
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
I2C_Send7bitAddress( I2C3,0x90,I2C_Direction_Transmitter);
while( !I2C_CheckEvent(I2C3,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
data=I2C_ReceiveData(I2C3);
while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_RECEIVED))
I2C_GenerateSTOP(I2C3, ENABLE);
}
}

1 REPLY 1
Posted on May 13, 2015 at 13:39

You'll want to be enabling the GPIO clocks, before GPIO_Init()

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..