AnsweredAssumed Answered

STM32F0x I2C problem

Question asked by hello188 on Oct 31, 2013
Latest reply on Nov 19, 2013 by Amel N
Hi, I am trying to communicate with MCP4725(12bit DAC) via I2C using STM32F030C8T6. The program gets stuck at one of the status checking part. Can somebody look at my code and see what the problem is?

//Code to Initialize I2C

void InitI2C(void){
 //PB8 = SCL1, PB9 = SDA1
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);//I2C1
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_1);
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_1);
 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8; //SCL 
 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
 GPIO_Init(GPIOB, &GPIO_InitStruct);
 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
 GPIO_Init(GPIOB, &GPIO_InitStruct);
 I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;//NOt SMBUS host
 I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
 I2C_InitStruct.I2C_DigitalFilter = 0x00;
 I2C_InitStruct.I2C_OwnAddress1 = 0x00;
 I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
 I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
 I2C_InitStruct.I2C_Timing = 0x10400CDB;//STM의 I2C Configuration tool
 I2C_Init(I2C1, &I2C_InitStruct);
 I2C_Cmd(I2C1, ENABLE);

//code to communicate with I2C

void WriteMCP4725(uint16_t data){
 uint8_t high_buffer, low_buffer;
 if(data > 4095) data = 4095;
 high_buffer = (uint8_t)((data>>8) & 0xFF);
 low_buffer = (uint8_t)(data & 0xFF);

 while(I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET);
 I2C_TransferHandling(I2C1, 0xC0, 2, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); //Generate Start Condition
 while(I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET);
 I2C_SendData(I2C1, high_buffer);
 while(I2C_GetFlagStatus(I2C1, I2C_ISR_TCR) == RESET);
 I2C_SendData(I2C1, low_buffer);
 while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET);
 I2C_ClearFlag(I2C1, I2C_ICR_STOPCF);

am i doing something wrong??

my program doesn't get past the
while(I2C_GetFlagStatus(I2C1, I2C_ISR_TCR) == RESET);

Here is the code that works with PIC and CCS compiler
void ext_dac_out(uint8_t dac_type, uint8_t out_sel, uint16_t dac_buffer){
 int8 buffer;
 int8 high_buffer, low_buffer;
buffer = 0b11000000;//MCP4725
high_buffer = dac_buffer>>8;
  low_buffer = dac_buffer;
i2c_write(buffer);//address byte, write mode
i2c_write(high_buffer);//fast write commands(C2,C1,PD1,PD0,D11-D8)
  i2c_write(low_buffer);     //write data(D7-D0)

Thank you