cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Read from Slave with Python

Hbal.1
Associate

Hi,

i use a stm8s003 and SPL for programming. I am not a profi programmer.

My goal is measure an Analog signal with stm8s and reading this with Python command from a Raspberry Pi.

Ok, i use STM example Program I2c slave. I can read some registers from stm8s with a little python code. But i dont now, which registers, that i read.

Has i2c a Data register?

Or can some body give me a little example like this: Read the value of int x over i2c bus

Below is the codes

I2C slave code:

int x=12; //i want read this value!!!!!

void main(void)

{

 /* system_clock / 1 */

 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

 I2C_DeInit();

 /* Initialize I2C peripheral */

#ifdef I2C_slave_7Bits_Address

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

#else

 I2C_Init(100000, SLAVE_ADDRESS, I2C_DUTYCYCLE_2, I2C_ACK_CURR,I2C_ADDMODE_10BIT, 16);

#endif

 /* Enable Error Interrupt*/

 I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE);

 /* Enable general interrupts */

 enableInterrupts();

 /*Main Loop */

 while (1)

 {

  /* infinite loop */

 }

}

PYTHON Code:

from smbus2 import SMBus

with SMBus(1) as bus:

b = bus.read_byte_data(0x18, 0) #this data adress, how it must to be

print(b)

0 REPLIES 0