cancel
Showing results for 
Search instead for 
Did you mean: 

stm8s I2C Communication

erhansakaryaa
Associate
Posted on February 12, 2016 at 14:20

Hello,

I want to use I2C Communication between two stm8s208cb MCUs. I use stm8s standard peripharel library. My code:

Master:

&sharpinclude ''stm8s.h''

&sharpdefine I2C_SPEED 300000

&sharpdefine OWN_ADDRESS    0xA0      

&sharpdefine SLAVE_ADDRESS    0x30

void delay_delay(unsigned int mul);

void main()

{

 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

 I2C_DeInit();

 I2C_Init(I2C_SPEED, 0xA0, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 16);

while (1)

{  

I2C_GenerateSTART(ENABLE);

I2C_Send7bitAddress(SLAVE_ADDRESS, I2C_DIRECTION_TX);

I2C_SendData(0x61);

I2C_GenerateSTOP(ENABLE);

delay_delay(500);

}

}

void delay_delay(unsigned int mul)

{

   for(; mul>0; mul--);

}

&sharpifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

  while (1)

  {}

}

&sharpendif

/*******************************************************************/

Slave:

&sharpinclude ''stm8s.h''

&sharpinclude ''stm8s_uart3.h''

&sharpinclude ''stdio.h''

&sharpinclude ''stm8s_i2c.h''

&sharpdefine I2C_SPEED 300000

&sharpdefine OWN_ADDRESS    0xA0      

&sharpdefine SLAVE_ADDRESS    0x30

void delay_delay(unsigned int mul);

void sendstring(char *s)

{

  while(*s){

    UART3->DR = (u8) (*s);

    while ((UART3->SR & (u8) UART3_FLAG_TXE) == RESET);

    s++;

  } 

}

void main(void)

{

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

UART3_DeInit();

UART3_Init((uint32_t)9600, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO, UART3_MODE_TXRX_ENABLE);

I2C_DeInit();

I2C_Init(300000, 0x30, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 16);

char veri_bas[1];

  

while (1)

{  

I2C_AcknowledgeConfig(DISABLE);

uint8_t data = I2C_ReceiveData(); //-datayy oku

I2C_AcknowledgeConfig(ENABLE);

sprintf(veri_bas ,''%i'' ,data);

sendstring(veri_bas);

delay_delay(500);

}

}

void delay_delay(unsigned int mul)

{

   for(; mul>0; mul--);

}

&sharpifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

  while (1)

  {}

}

&sharpendif

It does not work. Help please, I can't find the error.

Also:

Resistors(4.7K) are okey for I2C Communication. 

#stm8s208 #hal #stm8s-i2c #i2c
0 REPLIES 0