cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0x I2C problem

jksystem247
Associate II
Posted on October 31, 2013 at 09:04

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

 RCC_I2CCLKConfig(RCC_I2C1CLK_HSI); //I2C 

 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);//I2C1 GPIO 

 GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_1);

 GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_1);

 

 

 GPIO_StructInit(&GPIO_InitStruct);

 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_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

 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);

part

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_start();

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)

  i2c_stop();

Thank you

1 REPLY 1
Amel NASRI
ST Employee
Posted on November 19, 2013 at 11:08

Which StandardLib version are you using?

The flag names should be ''I2C_FLAG_xxxx'' not ''I2C_ISR_xxxx'' or ''I2C_ICR_TCR''.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.