Use of IIC library of stm8

Publisher:zhihuaLatest update time:2019-12-17 Source: eefocusKeywords:stm8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:stm8 Reference address:Use of IIC library of stm8

Previous article:STM8 GPIO modes
Next article:DS18B20 timing analysis detailed steps

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号