Read and write operations based on the use of STM8 microcontroller I2C

STM8 hardware I2C knowledge

The I2C module of STM8S can not only receive and send data, but also convert data from serial to parallel data when receiving, and convert data from parallel to serial data when sending. You can enable or disable interrupts. The interface is connected to the I2C bus through data pins (SDA) and clock pins (SCL). Allows connection to standard (up to 100kHz) or fast (up to 400kHz) I2C bus.

Read and write operations based on using STM8 microcontroller I2C mode

1. 4 modes of I2C

● Send mode from device

● Receive mode from device

● Main device sending mode

● Master receiving mode

2. The main features of I2C

● Parallel bus/I2C bus protocol converter

● Multi-master function: the module can be used as a master device or as a slave device

●I2C master device function

─ Generate start and stop signals

●I2C slave function

─ Programmable I2C address detection

─ Stop bit detection

● Generate and detect 7-bit/10-bit address and general call

● Support different communication speeds

─ Standard speed (up to 100 kHz)

─ Fast (up to 400 kHz)

● Status flag:

─ Transmitter/receiver mode flag

─I2C Bus Busy Flag

─ Loss of arbitration in main mode

─ Address/data transmission response (ACK) error

─ Detect wrong start or stop condition

─ Data overload or underload when the clock stretching function is prohibited

●3 types of interrupts

─1 communication interruption

─1 error interrupt

─1 wake-up interrupt

● Wake-up function

─ If an address match is detected in the slave mode, the MCU can be woken up from the low-power mode

● Optional clock stretching function

3. The sequence of operations required by the main mode

● Set the input clock of the module in the I2C_FREQR register to generate the correct timing

● Configure the clock control register

● Configure the rise time register

● Programming the I2C_CR1 register to start the peripheral

● Set the START bit in the I2C_CR1 register to 1, to generate a start condition

●The input clock frequency of the I2C module must be at least:

● In standard mode: 1MHz

● In fast mode: 4MHz

Software engineering source code 1. About engineering

The project code provided in this article is based on the previous software project "STM8S-A04_UART basic sending and receiving data" by adding I2C interface modification. The way to read and write EEPROM is different from the previous "analog I2C read and write" method.

2. Hardware I2C initialization

void I2C_Initializes(void)

{

CLK_PeripheralClockConfig (CLK_PERIPHERAL_I2C, ENABLE);

I2C_Cmd (ENABLE);

I2C_Init (I2C_SPEED, I2C_SLAVE_ADDRESS7, I2C_DUTYCYCLE_2, I2C_ACK_CURR,

I2C_ADDMODE_7BIT, 16);

}

I2C_SPEED: I2C speed, generally 100K-400K

I2C_SLAVE_ADDRESS7: Slave device address. When used as a master device, this address has no effect.

I2C_DUTYCYCLE_2: Fast mode

I2C_ACK_CURR: Acknowledge

I2C_ADDMODE_7BIT: Device address bits

16: Input clock (unit M)

3. EEPROM_WriteByte write one byte

Writing a byte is divided into 5 steps:

void EEPROM_WriteByte (uint16_t Addr, uint8_t Data)

{

while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));

/* 1. Start */

I2C_GenerateSTART(ENABLE);

while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

/* 2. Device address/write*/

I2C_Send7bitAddress (EEPROM_DEV_ADDR, I2C_DIRECTION_TX);

while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

/* 3. Data address*/

#if (8 == EEPROM_WORD_ADDR_SIZE)

I2C_SendData ((Addr&0x00FF));

while (! I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED));

#else

I2C_SendData((uint8_t)(Addr》》8));

while (! I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_SendData((uint8_t)(Addr&0x00FF));

while (! I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED));

#endif

/* 4. Write a byte of data*/

I2C_SendData(Data);

while (! I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* 5.Stop*/

I2C_GenerateSTOP(ENABLE);

}

4. EEPROM_ReadByte reads one byte

Reading a byte requires 2 more steps than writing a byte. The reason is that there is a switching process from writing addresses to reading data when reading.

void EEPROM_ReadByte (uint16_t Addr, uint8_t *Data)

{

while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));

/* 1. Start */

I2C_GenerateSTART(ENABLE);

while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

/* 2. Device address/write*/

I2C_Send7bitAddress (EEPROM_DEV_ADDR, I2C_DIRECTION_TX);

while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

/* 3. Data address*/

#if (8 == EEPROM_WORD_ADDR_SIZE)

I2C_SendData ((Addr&0x00FF));

while (! I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED));

#else

I2C_SendData((uint8_t)(Addr》》8));

while (! I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED));

I2C_SendData((uint8_t)(Addr&0x00FF));

while (! I2C_CheckEvent (I2C_EVENT_MASTER_BYTE_TRANSMITTED));

#endif

/* 4. Start over*/

I2C_GenerateSTART(ENABLE);

while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));

/* 5. Device address/read*/

I2C_Send7bitAddress (EEPROM_DEV_ADDR, I2C_DIRECTION_RX);

while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

/* 6. Read one byte of data*/

I2C_AcknowledgeConfig (I2C_ACK_NONE);

while(I2C_GetFlagStatus(I2C_FLAG_RXNOTEMPTY) == RESET);

*Data = I2C_ReceiveData();

/* 7.Stop*/

I2C_GenerateSTOP(ENABLE);

}

Portable Mini Projector

Shenzhen Happybate Trading Co.,LTD , https://www.happybateprojectors.com