I. Introduction
stm8 is a low-power MCU chip, which has the advantages of stm32 library functions and rich resources. It also has the characteristics of low price and low power consumption. In some projects, it can play a good role. Here I will introduce the IIC hardware library function driver code and implementation of stm8.
2.IIC basic operation
The basic operation of IIC is divided into read operation and write operation. These two operations can perform some basic operations on the device. You also need to know the address of the device. You can check the specific IIC timing on the Internet.
3. Implementation Code
1. First, start the IIC clock to ensure normal operation. CLK_PeripheralClockConfig (CLK_Peripheral_I2C1, ENABLE); // Start the IIC1 clock
2. Initialize the IIC again
void Init(void)
{
I2C_DeInit(I2C1);
I2C_Init(I2C1,IICSPEED, host_address, I2C_Mode_I2C,I2C_DutyCycle_2, I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
I2C_Cmd(I2C1,ENABLE);
}
It can be used to set the IIC communication time, host address, slave address and other parameters. IICSPEED should not exceed 400K, which will cause communication instability. It is best to be around 100K. Host_address is the address of the host, which can be set by yourself.
3.IIC write operation
/****************************************************************************
* Name: I2C_WriteByte(uint8_t addr,uint8_t data)
* Function: Write IIC.
* Input parameter: uint8_t addr register address
* uint8_t data write data
* Output parameter: None
* Description: Write to the register of the touch screen, all variables are in hexadecimal
**************************************************************************/
void I2C_WriteByte(uint8_t addr,uint8_t data)
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); //Wait for idle time
I2C_GenerateSTART(I2C1, ENABLE); //Turn on I2C1
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5, master mode*/
I2C_Send7bitAddress(I2C1, I2C1_WRITE_ADDRESS7, I2C_Direction_Transmitter); //Device address -- default 0xD0
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, addr); //Register address
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, data); //Send data
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_AcknowledgeConfig(I2C1,ENABLE);
I2C_GenerateSTOP(I2C1, ENABLE); //Shut down I2C1 bus
}
The above code is the IIC write function, which can write to the device register. I2C1_WRITE_ADDRESS7 represents the write address of the device IIC, which can be changed according to the device address. Each step is a call to the IIC library function. If you perform single-step debugging and the address register is correct, you can see that the slave will have a response signal for each operation step.
4.IIC read operation
/****************************************************************************
* Name: uint8_t I2C_ReadByte(uint8_t addr)
* Function: Perform IIC read operation.
* Input parameter: uint8_t addr register address
* Output parameter: uint8_t i saves the read data and returns it as a parameter
* Note: Read the registers of the touch screen. The variables are all in hexadecimal. The IIC write operation
must write the register and then read the register, otherwise it will fail
************************************************************************/
uint8_t I2C_ReadByte(uint8_t addr)
{
uint8_t i;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);//Turn on I2C1
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5, master mode*/
I2C_Send7bitAddress(I2C1, I2C1_WRITE_ADDRESS7, I2C_Direction_Transmitter); //Device address -- default 0xD0
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_SendData(I2C1, addr);//register addresswhile
(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1, ENABLE);//turn on I2C1
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5, master mode*/
I2C_Send7bitAddress(I2C1, I2C1_READ_ADDRESS7, I2C_Direction_Receiver);//device address-- default 0xD1
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while (!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED));
i=I2C_ReceiveData(I2C1);//Read data
I2C_AcknowledgeConfig(I2C1,DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);//Shut down I2C1 bus
return i;
}
The read operation of IIC is different from the write operation. The read operation must first be written, and the register address to be read must be written. Of course, the address must also be sent as the write address. Then write the device read address, the register address to be read, and read the data. I2C1_READ_ADDRESS7 is the read address of the device. The response variables of the write operation and the read operation are also different. Please pay attention to this.
IV. Conclusion
The library function of hardware IIC realizes communication, with small code implementation, high stability and fast speed after adjustment. The disadvantage is that it may take up part of the memory space and is inconvenient to debug, because all library functions are used, and it is not easy to find problems.
Previous article:STM8 GPIO modes
Next article:DS18B20 timing analysis detailed steps
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [NXP Rapid IoT Evaluation] BLE Transmission Sensor Value Test
- 1
- [Hua Diao Experience] 15 Try to build the Arduino development environment of Beetle ESP32 C3
- [GD32E231 DIY] DAP RTT, beats all printf formats, swo is no longer a thing
- Live Replay | Microchip Security Solutions Seminar Series (Continuously Updated)
- Python still lacks 4 powerful features
- How to do image processing with ARM microcontroller
- stm32h755/757, can such m7+m4 heterogeneous multi-core MCU execute two instructions at the same time?
- New Book Preview: Programming Microcontrollers with Python
- The msp430 microcontroller sends floating point numbers to the host computer through the serial port