stm8s development (eight) use of IIC: IIC host communication!

Publisher:喜从中来Latest update time:2017-09-12 Source: eefocusKeywords:stm8s  IIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

We have talked about two commonly used serial ports, UART and SPI. This time, we will talk about another commonly used serial port: IIC (I2C) communication.


Popular Science IIC: Generally there are two signal lines, one is the bidirectional data line SDA, the other is the clock line SCL. All serial data SDA connected to the IIC bus device is connected to the bus SDA, and the clock line SCL of each device is connected to the bus SCL.


Communication process:
In master mode, the IIC interface starts data transmission and generates a clock signal. Serial data transmission always starts with a start condition and ends with a stop condition. Both the start condition and the stop condition are generated by software control in master mode.
In slave mode, the IIC interface can recognize its own address (7 or 10 bits) and the general call address. The software can control the opening or closing of the recognition of the general call address.
Data and address are transmitted in 8 bits/byte, with the high bit first. The 1 or 2 bytes following the start condition are the address (1 byte in 7-bit mode and 2 bytes in 10-bit mode). The address is only sent in master mode.
During the 9th clock after 8 clocks of a byte transmission, the receiver must send back an acknowledge bit (ACK) to the transmitter. Refer to the figure below.


For detailed protocol, please refer to: http://blog.csdn.net/subkiller/article/details/6854910


Similar to SPI, using IIC is nothing more than initialization, data sending, and data receiving, three major functions.

Initialization is divided into master and slave, but generally the microcontroller that communicates with external chips acts as the master.


  1. void IIC_Master_Init(void)  

  2. {  

  3.   CLK_PCKENR1 |= 0x01; //Enable IIC peripheral clock  

  4.     

  5.   PB_DDR &= 0xcf;  

  6.   PB_CR1 &= 0xcf;  

  7.   PB_CR2 &= 0xcf;  

  8.     

  9.   I2C_CR1 = 0x00; //Allow clock stretching, disable broadcast call, disable iic  

  10.   I2C_FREQR = 0x01; //Input clock frequency 8MHz  

  11.   I2C_OARH = 0x40; // seven-bit address mode  

  12.   I2C_OARL = 0xa0; //Own address 0xa0  

  13.   I2C_CCRL = 0xff; //  

  14.   I2C_CCRH = 0x00; //Standard mode  

  15.   I2C_TRISER = 0x02;  

  16.   I2C_CR1 |= 0x01; // Enable iic peripheral  

  17. }  


Here we use the IIC pins PB4 and PB5 on the STM8S105 chip. In addition, we need to enable the IIC clock.



Here we only give a simple example of sending and receiving data, because different external chips have different communication methods, but generally they are:

Read operation: Start -> Send peripheral address -> Start -> Send register address to be read -> Read one byte -> (may read another byte) -> ... -> End
Write operation: Start -> Send peripheral address -> Send register address to be written -> Write one byte -> 

  1. void IIC_Write_Byte(u8 DeviceAddress, u8 Address, u8 Data)  

  2. {  

  3.   vu8 temp = 0;  

  4.   while((I2C_SR3 & 0x02) != 0); //Wait for the IIC bus to be idle  

  5.   IIC_Start();  

  6.   while((I2C_SR1 & 0x01) == 0); //EV5, the start signal has been sent  

  7.   I2C_DR = (DeviceAddress & 0xfe); // Send the physical address of the iic slave device, the lowest bit is 0, write operation  

  8.   while((I2C_SR1 & 0x02) == 0); //The address has been sent  

  9.   temp = I2C_SR1; //Clear ADDR flag  

  10.   temp = I2C_SR3;  

  11.   while((I2C_SR1 & 0x80) == 0); //Wait for the send register to be empty  

  12.   I2C_DR = Address; //Send the register address to be written  

  13.   while((I2C_SR1 & 0x04) == 0); //Wait for sending to complete  

  14.   while((I2C_SR1 & 0x80) == 0); //Wait for the send register to be empty  

  15.   I2C_DR = Data; //Send the data to be written  

  16.   while((I2C_SR1 & 0x04) == 0); //Wait for sending to complete  

  17.   temp = I2C_SR1; //Clear BTF flag  

  18.   temp = I2C_DR;  

  19.   IIC_Stop(); //Send stop signal  

  20. }  

  21.   

  22. unsigned char IIC_Read_Byte(u8 DeviceAddress, u8 Address)  

  23. {  

  24.   vu8 temp = 0;  

  25.   short read_data = 0;  

  26.   while((I2C_SR3 & 0x02) != 0); //Wait for the IIC bus to be idle  

  27.   I2C_CR2 |= 0x04; //Enable ACK  

  28.   IIC_Start();  

  29.   while((I2C_SR1 & 0x01) == 0); //EV5, the start signal has been sent  

  30.   I2C_DR = (DeviceAddress & 0xfe); // Send the physical address of the iic slave device, the lowest bit is 0, write operation  

  31.   while((I2C_SR1 & 0x02) == 0); //The address has been sent  

  32.   temp = I2C_SR1; //Clear ADDR flag  

  33.   temp = I2C_SR3;  

  34.   while((I2C_SR1 & 0x80) == 0); //Wait for the send register to be empty  

  35.   I2C_DR = Address; // Send the register address to be read  

  36.   while((I2C_SR1 & 0x04) == 0); //Wait for data to be sent  

  37.     

  38.   IIC_Start();  

  39.   while((I2C_SR1 & 0x01) == 0); //EV5, the start signal has been sent  

  40.   I2C_DR = (DeviceAddress | 0x01); // Send the physical address of the IIC slave device, the lowest bit is 1, read operation  

  41.   while((I2C_SR1 & 0x02) == 0); //The address has been sent  

  42.   temp = I2C_SR1; //Clear ADDR flag  

  43.   temp = I2C_SR3;  

  44.   while((I2C_SR1 & 0x40) == 0); //Wait for the receive data register to be non-empty  

  45.   read_data = I2C_DR;  

  46.   I2C_CR2 &= 0xfb; //When reading data, sending stop must disable ack to release the slave  

  47.   temp = I2C_SR1; //Clear BTF flag  

  48.   temp = I2C_DR;  

  49.   IIC_Stop();  

  50.   return read_data;  

  51. }  



Attached is the project of stm8s in IAR environment, including the initialization code of on-chip hardware such as SPI, IIC, PWM, AWU, USART, EEPROM, etc.

http://download.csdn .NET /detail/devintt/9454188



Read operation: Start -> Send peripheral address -> Start -> Send register address to be read -> Read one byte -> (may read another byte) -> ... -> End


Keywords:stm8s  IIC Reference address:stm8s development (eight) use of IIC: IIC host communication!

Previous article:stm8s development (nine) Use of EEPROM: Use EEPROM to store data!
Next article:stm8s development (VII) Use of SPI: SPI host communication!

Recommended ReadingLatest update time:2024-11-16 16:33

2. Software simulation to implement the iic protocol (taking 51 as an example)
1. Overview of I2C Serial Bus 1. The I2C bus is a serial bus launched by PHLIPS. It is a high-performance serial bus with bus arbitration and high-speed and low-speed device synchronization functions required by multi-host systems. The I2C bus has only two bidirectional signal lines, one is the data line S
[Microcontroller]
2. Software simulation to implement the iic protocol (taking 51 as an example)
51 MCU - Revisiting IIC - General Software Package (Assembly, C Language) - 03
I feel a little unwilling, haha , so I tidied it up a little. We know that there are only a few 51 MCUs with IIC bus interfaces (in fact, I don't know which one~~). If it is a MCU without IIC bus, it is not necessary to expand the IIC bus interface, but only need to be simulated by software, which will undoubtedly p
[Microcontroller]
51 MCU - Revisiting IIC - General Software Package (Assembly, C Language) - 03
How to see the RAM ROM size when compiling STM8S using stvd
I just installed the STVD compiler, but it doesn't show how much RAM and ROM are used when compiling? There are two ways to solve this problem: one is to look at the .map file and the other is to add a patch. The specific steps are as follows. You can download the corresponding file in My Resources. http://download.
[Microcontroller]
stm8s_STVD generates interrupt_vector.c problem
When using STVD software to develop stm8sMCU, open the STVD software and the system automatically generates an interrupt vector file vector.c. The following code can be seen in it Refer to the official document PM0044 program manual of stm8s mcu, we can know that mcu has 32 4-byte interrupt vector entries, where the
[Microcontroller]
stm8s_STVD generates interrupt_vector.c problem
Small problem of STM8s using library to configure port
  When used, the PA2 port needs to be set to push-pull output to control an external power switch. The port initialization procedure is as follows:     GPIO_DeInit(GPIOA);     GPIO_Init(GPIOA,GPIO_PIN_2,GPIO_MODE_OUT_PP_HIGH_SLOW);   After the setting is completed, the port will immediately output a high level, so ano
[Microcontroller]
S3C2440 Test Program (VII) IIC Experiment 2--Read and Write EEPROM (Software Simulation IIC)
Use S3C2440's GPE14--SCL and GPE15--SDA to simulate IIC in software: Since it is not clear how to define the bit like 51, the following definition is made:    #define IIC_SDA_L (rGPEDAT = rGPEDAT & ~(1 15)) #define IIC_SDA_H (rGPEDAT = rGPEDAT | (1 15)) #define IIC_SCL_L (rGPEDAT = rGPEDAT & ~(1 14)) #define IIC_SCL_H
[Microcontroller]
S3C2440 Test Program (VII) IIC Experiment 2--Read and Write EEPROM (Software Simulation IIC)
BMP180 test based on STM32 platform (simulated IIC)
1. Test description: Use analog IIC to get the ID number, temperature, pressure and altitude from BMP180. 2. Test preparation: Hardware platform: Atomic Battleship V3 development board  Test tools: logic analyzer, serial port debugging tool 3. Data sheet interpretation: (1) First is the timing diagram of reading m
[Microcontroller]
BMP180 test based on STM32 platform (simulated IIC)
Data storage method based on EEPROM inside STM8S microprocessor
EEPROM (Electrically Erasable Programmable Read-Only Memory) refers to an electronically erasable read-only memory, which is a non-volatile memory. After the power is off, the stored data is still retained. To erase or rewrite the content, you only need to operate directly in the form of electronic signals. EEPROM i
[Microcontroller]
Data storage method based on EEPROM inside STM8S microprocessor
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号