0
\$\begingroup\$

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

Master:

#include "stm8s.h"
#define I2C_SPEED 300000
#define OWN_ADDRESS    0xA0      
#define 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--);
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {}
}
#endif

/*******************************************************************/ Slave:

#include "stm8s.h"
#include "stm8s_uart3.h"
#include "stdio.h"
#include "stm8s_i2c.h"


#define I2C_SPEED 300000
#define OWN_ADDRESS    0xA0      
#define 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--);
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {}
}
#endif

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

Also: Resistors(4.7K) are okey for I2C Communication.

\$\endgroup\$

closed as unclear what you're asking by David, Bence Kaulics, Daniel Grillo, Sparky256, Voltage Spike Aug 2 '16 at 15:56

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ If you have a logic analyzer or an oscilloscope. Prob SDA and SCL pins. Get a screenshot of the activity that happens when you attempt to send message and add it to this question. Otherwise we can play 50 questions \$\endgroup\$ – Iancovici Feb 12 '16 at 14:12
  • 1
    \$\begingroup\$ "It does not work". Very descriptive. Perhaps you could elaborate on what you expect it to do and what it is doing. \$\endgroup\$ – Tom Carpenter Feb 12 '16 at 15:11
  • \$\begingroup\$ It boarders on disrespect to this community to write "It does not work, help" Don't post your code and expect the community to come up with the answer, please document your question clearly with all supporting information including scope screenshots and schematics. You just told us to fix your car but refuse to open up the hood so we can look at the engine. Please edit your question use clear communication and it might be reopened, Thanks \$\endgroup\$ – Voltage Spike Aug 2 '16 at 15:55
2
\$\begingroup\$

Interrupts are needed in your master and slave code to sync transmission and reception; your while loops wont cut it since there is no guarantee that they are both executing the proper bits of code simultaneously. You need to look at the I2C example code here on I2C data exchange between 2 STMs.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.