Quiero usar la comunicación I2C entre dos MCU stm8s208cb. Yo uso stm8s standard peripharel library. Mi código:
Maestro:
#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
/ *********************************************** ********************* / Esclavo:
#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
No funciona. Ayuda por favor, no puedo encontrar el error.
También: Los resistores (4.7K) son clave para la comunicación I2C.