IIC protocol communication based on STM8

Publisher:BlissfulHikerLatest update time:2020-09-11 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Overview

  I2C (IIC, Inter-Integrated Circuit), a two-wire serial bus, was developed by PHILIPS to connect microcontrollers and their peripherals.


  It is a serial bus composed of data line SDA and clock SCL, which can send and receive data. It can transmit data bidirectionally between CPU and controlled IC, and between IC and IC. High-speed IIC bus can generally reach more than 400kbps. But in STM8, 400kHZ is the fastest speed.


       It is often involved in subsequent module debugging and is a very common and easy-to-use protocol.


2. Introduction to I2C in the STM8S103 manual

 

 

After reading the Chinese manual, I personally feel that it is relatively simple. I will post the specific usage later.

 

3. Detailed Analysis of I2C

I2C has five core functions in total, namely:

①Start signal

②Stop signal

③Response signal

④Send data

⑤Receive data

 

These five core basic functions are sufficient to communicate with most sensors.

The following is a detailed introduction to each part, and some main codes are attached for your reference.

3.1 Start signal

When SCL is at a high level, SDA changes from a high level to a low level; the start signal is a level jump timing signal, not a level signal, as shown in the dotted box in the figure above.

1
2
3
4
5
6
7
8
9
10
11
12
void Start_Signal_IIC_(void){
   
  //Start signal:
  GPIO_WriteHigh(GPIOD, GPIO_PIN_2); //Data line
  IIC_Delay_4us();
  GPIO_WriteHigh(GPIOD, GPIO_PIN_3); //Clock line
  IIC_Delay_4us();
  GPIO_WriteLow(GPIOD, GPIO_PIN_2); //Data line
  IIC_Delay_4us();
  GPIO_WriteLow(GPIOD, GPIO_PIN_3); //Clock line
  IIC_Delay_4us();
}

  

3.2 Stop Signal

When SCL is at a high level, SDA changes from a low level to a high level; the stop signal is also a level jump timing signal, not a level signal, as shown in the dotted box in the figure above.

1
2
3
4
5
6
7
void End_Data_IIC_()
  {
    GPIO_WriteLow(GPIOD, GPIO_PIN_2); //Pull the data line low
    IIC_Delay_4us();
    GPIO_WriteHigh(GPIOD, GPIO_PIN_3); //Pull the clock line high
    IIC_Delay_4us();
    GPIO_WriteHigh(GPIOD, GPIO_PIN_2); //Pull the data line high
1
}

  

3.3 Response signal

 

 

 

There are two types of response signals: active response signal and active non-response signal.

①Ack (actively pull down SDA to form a response signal)

 The data of the I2C bus are transmitted in bytes (8 bits). After the sending device sends a byte, the data bus is released during the 9th pulse of the clock, and the receiver sends an ACK (pulling the level of the data bus low) to indicate that the data has been successfully received.

1
2
3
4
5
6
7
8
9
//Active response signal
void vIIC_Ack()
{
  GPIO_WriteLow(GPIOD, GPIO_PIN_2);
  IIC_Delay_4us();
  GPIO_WriteHigh(GPIOD, GPIO_PIN_3);
  IIC_Delay_4us();
  GPIO_WriteLow(GPIOD, GPIO_PIN_3);
  IIC_Delay_4us();
}

 

②NAck (actively does not pull down SDA and does not form a response signal)

During the 9th pulse of the clock, the transmitter releases the data bus, and the receiver does not pull the data bus low, indicating a NACK. NACK has two uses:

a. Generally indicates that the receiver did not successfully receive the data byte;

b. When the receiver is the master, after receiving the last byte, it should send a NACK signal to notify the controlled transmitter to end data transmission and release the bus so that the master receiver can send a stop signal STOP.

1
2
3
4
5
6
7
8
9
10
// Actively do not answer
void vIIC_NAck()
{
  GPIO_WriteHigh(GPIOD, GPIO_PIN_2);
  IIC_Delay_4us();
  GPIO_WriteHigh(GPIOD, GPIO_PIN_3);
  IIC_Delay_4us();
  GPIO_WriteLow(GPIOD, GPIO_PIN_3);
  IIC_Delay_4us();
}

 ③ReadAck (wait for response signal)

    This signal is used when the host is waiting for a response from the slave after sending data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
u8 bIIC_ReadACK()
 {
   GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_IN_PU_IT); //Change SDA to input mode.
   GPIO_WriteHigh(GPIOD, GPIO_PIN_3); //Pull the clock line high.
   IIC_Delay_4us(); 
   if(IIC_SDA_R != 0)
      {
        //Low has response
        GPIO_WriteLow(GPIOD, GPIO_PIN_3);
        IIC_Delay_4us();
        GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_FAST);//SDA
        return 1;
      }
    else
       //High no response
      {
       GPIO_WriteLow(GPIOD, GPIO_PIN_3);
       IIC_Delay_4us();
       GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_FAST);//SDA
       return 0;
      }
 }

  

3.4 Sending Data

After sending the start signal, the communication begins and the master sends an 8-bit data. Then, the master releases the SDA line and waits for the confirmation signal (ACK) from the slave.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Send_Data_IIC_(uint8_t Data){
    int i;
//Pull down the data line and clock line
    GPIO_WriteLow(GPIOD, GPIO_PIN_3);
    GPIO_WriteLow(GPIOD, GPIO_PIN_2);
  for(i=0;i<8;i++)
  {
      if(Data&0x80)
      GPIO_WriteHigh(GPIOD, GPIO_PIN_2);
    else
      GPIO_WriteLow(GPIOD, GPIO_PIN_2);
     
    Data= Data<<1;
    IIC_Delay_2us();
    GPIO_WriteHigh(GPIOD, GPIO_PIN_3);
    IIC_Delay_4us();
    GPIO_WriteLow(GPIOD, GPIO_PIN_3);
    IIC_Delay_2us();
  }
}

  

3.5 Receiving Data

After sending the start signal, the communication starts, and the host sends an 8-bit data. Then, the slave receives the data and returns an acknowledgment signal (ACK) to the host. Only then does the host start receiving data. After the host completes receiving the data, it sends a NACK signal to the slave to notify the receiving end to end data reception.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Receive function
uint8_t uIIC_RecvByte()
{
  uint8_t i,uReceiveByte = 0;
  GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_IN_PU_IT);
  for(i=0;i<8;i++)
  {
   
    uReceiveByte <<= 1;
    IIC_Delay_4us();
    GPIO_WriteHigh(GPIOD, GPIO_PIN_3); //Read data level when the clock line is high
    IIC_Delay_4us();
    if(IIC_SDA_R !=0 )
    {
      uReceiveByte|=0x01;
    }
    IIC_Delay_4us();
    GPIO_WriteLow(GPIOD, GPIO_PIN_3);
    IIC_Delay_4us();
  }
   
   GPIO_Init(GPIOD, GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_FAST);
   return uReceiveByte;
}


3.6 I2C communication overall process

 

The following figure perfectly illustrates the changes of SDA and SCL lines during the whole process of iic from receiving to sending. Combined with the above explanation and process code, this figure can help memory and understanding.


4. Routines

 

4.1 Compilation environment:

        IAR is used here for compilation, which is relatively easy to use. In the future, when using the STM32 development board, it is recommended to use CUBE to directly generate the initialization function, which is very convenient to use with Keil5.


4.2 Main chip:

  My main chip is 103 in the STM8S series. Among them, STM8S 003, 005, 103, 105 have the same configuration (peripherals and CPU frequency, FLASH), and can be burned if the code is the same.


4.3 Code & Analysis

       The iic code can drive almost all clock modules on the market, so the code here can call the code of the clock module. I have explained each function, you can learn more about it.


      From the above code, we can know that this protocol is composed of data line and clock line. Data transmission and reception require pulling up the data line or pulling down the clock line. So it is recommended to use the library function to pull up and down directly. If you want convenience, adding a macro definition can be more intuitive and convenient.

 

5. Conclusion

The above are the core functions of iic. I have written each function very clearly. In the next blog, I will post the sensor communication blog based on the iic clock module. You can continue to read the next blog for reference.

Keywords:STM8 Reference address:IIC protocol communication based on STM8

Previous article:Renaming an existing project to another project in STVD
Next article:Detailed explanation of STM8 IAP upgrade program design - IAR environment

Recommended ReadingLatest update time:2024-11-15 18:03

Weird problem when IAR compiles stm8
Recently, I used the software iar for stm8 to debug the stm8 series chips. When using the timer interrupt, I found a problem. The interrupt entry of the timer in the program is as follows #pragma vector=ITC_IRQ_TIM1_OVF + 2 __interrupt void TIM1_UPD_OVF_BRK_IRQHandler(void)  {              /* Clear the update int
[Microcontroller]
STM8 16-bit general-purpose timer (TIM2, TIM3, TIM5)
introduce This chapter introduces the STM8 general-purpose timers TIM2, TIM3 and TIM5, where TIM2 has 3 channels, TIM3 has 2 channels, and TIM5 is similar to TIM2 but with two additional registers for timer synchronization and cascading. The general-purpose timer consists of a 16-bit auto-load counter with a programma
[Microcontroller]
STM8 16-bit general-purpose timer (TIM2, TIM3, TIM5)
About the failure of using IAR software stlink to download stm8 program
I used to use IAR normally, but this time I used stlink to download, and stlink could not download for unknown reasons. I turned off the power and restarted it. Sometimes it worked, but sometimes it really did not work. The prompt message was as shown in the figure below. Failed to initialize communcation with hardw
[Microcontroller]
About the failure of using IAR software stlink to download stm8 program
STM8 study notes---Implementation of serial port printf function
When using a microcontroller, the serial port is a frequently used function, especially in the process of debugging the code, it is often necessary to use the serial port to print out certain variable values ​​to determine whether the program execution process is normal. However, the microcontroller does not have a pr
[Microcontroller]
STM8 study notes---Implementation of serial port printf function
STM8, delay function
Copy the code from Atom Brother: http://www.openedv.com/posts/list/17347/htm Atom's tutorial has more detailed comments and netizens' replies, which are only recorded here. This is based on IAR. Under Atom's post, some netizens replied to STVD. main.c #include "stm8l15x_conf.h" volatile u8 fac_us=0; void delay_init(
[Microcontroller]
Based on the use of STM8 microcontroller I2C method to realize read and write operations
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 from parallel to serial data when sending. Interrupts can be enabled or disabled. The interface is connected to the I2C bus through the data pin (SDA) and the cl
[Microcontroller]
Based on the use of STM8 microcontroller I2C method to realize read and write operations
[Zhongquan STM8 47 Lectures] Working Principle of CAT24WC Memory
Features Compatible with 400KHz I2C bus 1.8 to 6.0 V operating voltage range Low power CMOS technology Write protection function When WP is high, it enters the write protection state. Page Write Buffer 1 million program/erase cycles Data can be stored for 100 years 8-pin DIP SOIC or TSSOP package Commercial and indust
[Microcontroller]
[Zhongquan STM8 47 Lectures] Working Principle of CAT24WC Memory
STM8 study notes---serial port uart1
To use the uart1 serial port, you need to use two files: stm8s_uart1.c and stm8s_uart1.h 1. Create a project directory structure as follows: 2. Write the uart.h file as follows: #ifndef __UART_H #define __UART_H #include "stm8s.h" #include "stm8s_clk.h" void USART_Configuration(void); //Serial port configuration f
[Microcontroller]
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号