cancel
Showing results for 
Search instead for 
Did you mean: 

Hello, I am trying to initialize I2C, but getting continuously high signal on both SDA and SCL pins . Can anyone suggest what can be the issue?

HJada
Associate

#include "stm8s.h"

#define I2C_PORT   (GPIOB)

#define SDA_pin   (GPIO_PIN_5)

#define SCL_pin   (GPIO_PIN_4)

#define SLAVE_ADDRESS 0x60

#define I2C_OWN_ADDRESS 0x00

static void CLK_Config(void);

void I2C_init(void);

void I2C_Write(uint8_t I2C_Slave_Address,uint8_t idata);

void main(void)

{

/*Configure the HSE Clock*/

 CLK_Config();

  

 GPIO_Init(I2C_PORT,((GPIO_Pin_TypeDef)(SCL_pin | SDA_pin)),GPIO_MODE_OUT_PP_HIGH_FAST);

  

 /*I2C Init*/

 I2C_init();  

  

 I2C_Write(SLAVE_ADDRESS,0xA1);

  

}

/* for clock configuration */

static void CLK_Config(void)

{

  /*Default Initialization*/

  CLK_DeInit();   

   

  /*Configure the HSE */

  CLK_HSECmd(ENABLE);

  //CLK_HSICmd(ENABLE);

  /* Configure the Fcpu to DIV1*/

  CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1);

   

  CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);

  //CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);

   

  /* Output Fcpu on CLK_CCO pin */

  //CLK_CCOConfig(CLK_OUTPUT_HSI);   

}

void I2C_init(void)

{  

 CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, ENABLE);

  

 /* I2C Peripheral Enable */

 I2C_Cmd(ENABLE);  

  

 /* I2C configuration */

 I2C_DeInit(); 

  

 /* I2C configuration after enabling it */

 I2C_Init(I2C_SPEED, I2C_OWN_ADDRESS, I2C_DUTYCYCLE_2, I2C_ACK_CURR,I2C_ADDMODE_7BIT, 16);  

}

void I2C_Write(uint8_t I2C_Slave_Address,uint8_t idata)

{  

 I2C_GenerateSTART(ENABLE);

 while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));

 while(!(I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT)));

 I2C_Send7bitAddress(I2C_Slave_Address,I2C_DIRECTION_TX);

 while(!(I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)));

 I2C_SendData(idata);

 while(!(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED)));

 I2C_GenerateSTOP(ENABLE);

}

3 REPLIES 3
HJada
Associate

Kindly provide the solution for this, its kind a urgent.... We are using STM8AF6266TCX Microcontroller and trying to interface DAC-MCP4728T-E/UN.

We have verified the I2C bus hardware by connecting DAC with Arduino Uno Board and its working fine.

Mon2
Senior III

Hi. Your code is selected HSE for the clock so do you have an external clock source? If yes, what is the value? If not, you need to use the HSI to select the internal clock. What is the result if using HSI?

A bit new to STM8 but making progress with our target custom PCB.

From where did you get this source code? Is it from a working example or pieced together from scratch?

In the meantime, review:

http://embedded-lab.com/blog/starting-stm8-microcontrollers/26/

For I2C, as you most likely already know, the I2C pins should be open drain and with local pull-up to +3v3 (typical) @ 2k2 - 10k resistor value (not critical).

Please post your update.

Mon2
Senior III

GPIO_Init(I2C_PORT,((GPIO_Pin_TypeDef)(SCL_pin | SDA_pin)),GPIO_MODE_OUT_PP_HIGH_FAST);

the use of GPIO_MODE_OUT_PP_HIGH_FAST is not correct for I2C bus use. This definition will configure the I2C port pins as Push-Pull style meaning that:

if the pin is HIGH, the pins will be hard driven to a logic HIGH ("1") - NOT OK

if the pin is LOW, the pins will be hard driven to be a logic LOW ("0") - OK

rather you should be using OPEN DRAIN configuration with local pull-up resistors to +3v3.

With this method,

if the pin is HIGH, the pins will be soft pulled to a logic HIGH ("1") through the local pull-up resistors - OK

if the pin is LOW, the pins will be hard driven to be a logic LOW ("0") - OK

The open drain solution allows for either party to assert a logic HIGH and no one gets hurt due to the local resistor performing the high status - not being hard driven.

Check this application note on I2C use on the STM8:

https://my.st.com/content/my_st_com/en/products/embedded-software/mcu-mpu-embedded-software/stm8-embedded-software/stsw-stm8004.license=1557193594245.product=STSW-STM8004.version=1.0.0.html

which details on how to configure the STM8 port pins for OD (open drain) - change the port pin definitions to suit your project.

Hope this helps.