STM8S_010_I2C read and write EEPROM (hardware method)

Publisher:shengjuLatest update time:2019-11-09 Source: eefocusKeywords:STM8S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Ⅰ. Write in front


I believe that those who have read the previous article "A Deeper Understanding of I2C Bus, Protocol and Application" have a certain understanding of I2C. That article is about I2C, using microcontroller IO to simulate I2C to achieve read and write operations.


This article will describe the hardware I2C read and write operations, that is, the clock and data transmission process implemented by the processor's own hardware 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 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 clock pin (SCL). It allows connection to a standard (up to 100kHz) or fast (up to 400kHz) I2C bus.


1. Four modes of I2C


● Slave device sending mode


● Slave receiving mode


● Master device sending mode


● Master receiving mode


 


2. Main features of I2C


● Parallel bus/I2C bus protocol converter


● Multi-host function: This module can be used as both a master device and a slave device


● I2C master function


─ Generate start and stop signals


● I2C slave device function


─ Programmable I2C address detection


─ Stop position 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


─ Arbitration failure in master mode


─ Error in ACK after address/data transmission


─ False start or stop condition detected


─ Data overload or underload when clock stretching function is disabled


● 3 types of interrupts


─ 1 communication interruption


─ 1 error interrupt


─ 1 wake-up interrupt


● Wake-up function


─ In slave mode, if an address match is detected, the MCU can be woken up from low power mode


● Optional clock stretching function


 


3. Operation sequence required by main mode


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


● Configure the clock control register


● Configure the rise time register


● Program 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:


● Standard mode: 1MHz


● Fast mode: 4MHz


 


III. Software Engineering Source Code


1. About the project


The engineering code provided in this article is based on the previous software project "STM8S-A04_UART basic data transmission and reception" and adds an I2C interface. The way to read and write EEPROM is different from the previous "simulated I2C reading and writing" 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, usually 100K - 400K


I2C_SLAVE_ADDRESS7: slave device address. This address has no effect when used as a master device.


I2C_DUTYCYCLE_2: Fast mode


I2C_ACK_CURR: ACK


I2C_ADDMODE_7BIT: Device address bit number


16: Input clock (unit: M)


3.EEPROM_WriteByte writes 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 one 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 two more steps than writing a byte because when reading, there is an extra switching process from writing address to reading data.


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. Restart */


  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);


}


 


IV. Download


STM8S information:


http://pan.baidu.com/s/1o7Tb9Yq



Two versions of software source code project (STM8S-A10_I2C read and write EEPROM (hardware)):


http://pan.baidu.com/s/1c2EcRo0



Tip: If the network disk link is invalid, you can check the update link in the "bottom menu" of the WeChat public account.



Keywords:STM8S Reference address:STM8S_010_I2C read and write EEPROM (hardware method)

Previous article:A brief discussion of I2C bus (IV) -- Example of IIC driver file for STM8
Next article:STM8 I/O port simulates I2C

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

STM8S timer 1 accurate delay
1. Introduction This article describes how to use Timer 1 in the STM8S series to perform precise timing of 1S. 2. Experimental Platform Compiler software: IAR for STM8 1.42.2 Hardware platform: stm8s003f3p6 development board Emulator: ST-LINK Library version: STM8Sx_AN3298_FW_V4.0.0 3. Copyright St
[Microcontroller]
(6) s3c2440 uses I2C interface to access EEPROM
After reading and understanding the official protocol document of I2C, let's use s3c2440 and EEPROM to verify it.         Originally I wanted to use the SDA and SCL pins of s3c2440 to be reused as GPIO for simulation, but after a week of doing it without an oscilloscope, I couldn't figure it out, and finally gave up.
[Microcontroller]
STM8S heap and stack description
The STM8 data sheet gives the location and size of the stack. The stack is allocated from high to low, and the heap is allocated from low to high. The stack is a continuous storage area in the memory, which is used to store some temporary data. The stack is divided into the stack area (stack) and the heap area (Heap
[Microcontroller]
STM8S heap and stack description
How to configure the minimum pin of STM8S STM8L
STM8S has no peripheral circuits. The MCU CAP is connected to the 104 capacitor reset and connected to the pull-up resistor. All other pins are left floating. The current is tested using the following program as follows: (Similar experiments have been done with STM8L before, and the situation is basically the same) In
[Microcontroller]
STM8S interrupt wake-up, timer wake-up, window watchdog
1. STM8S external interrupt wake-up First understand the interrupt resources of STM8S Let's look at the interrupt management of STM8S. STM8S uses software priority and hardware priority to control the response of an interrupt. The software priority is compared first, and the hardware priority is compared only when
[Microcontroller]
STM8S interrupt wake-up, timer wake-up, window watchdog
ATmega128(EEPROM)
//atmega128 read and write EEPROM sample program, record the number of CPU startup times in EEPROM //Display the read data on the LED digital tube, and you can use the reset button to refresh the display //Compilation environment AVR Studio 4.17/AVR GCC //System clock 7.3728MHZ, set the fuse bit to external high-fre
[Microcontroller]
AT24C01-512eeprom read and write program based on AVR microcontroller
For AT24Cxx series eeprom memory, there is a page-skipping function when writing, no need to consider page boundaries, I2C is implemented by software simulation, and is being improved...   #define SDA1() PORTC|=1 PC1 //Data output 1, #define SDA0() PORTC&=~(1 PC1) //Data output 0 #define SDAout() DDRC|=1 PC1 //Dat
[Microcontroller]
stm8s clock source switching
The STM8 microcontroller has a rich clock source. The chip has both a 16MHZ high-speed RC oscillator and a 128KHZ low-speed RC oscillator. It can also connect a high-speed crystal oscillator externally. During system operation, it can be switched freely as needed. After the microcontroller is reset, the internal high-s
[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号