Design of Data Writer Based on I2C Bus

Publisher:lxy64420245Latest update time:2012-02-01 Source: 无忧电子开发网 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Introduction
The Inter-Integrated Circuit (IIC) bus is a bus used to connect microcontrollers and their peripheral devices. The main advantages of the bus are its simplicity and effectiveness. Since the interface is directly on the component, it occupies a small space, reduces the space of the circuit board and the number of chip pins, and is widely used in intelligent instruments. Some MCUs now provide IIC interfaces, but for MCUs that do not directly support IIC buses, software simulation is required. This article uses the AT89S52 microcontroller as the core, communicates with the PC, realizes the reading and writing functions of the EEPROM AT24C04 with IIC bus, and constructs a data writer for reading and writing tables, curves, parameters, etc. in instruments and other equipment. All programs are completed in C language.

2 Communication format between MCU and PC
The data communication format in this design is as follows: the first byte is the command sent to MCU, and the second byte is reserved. The last 16 bytes are the data to be written. However, only the first 2 bytes are sent at the beginning of the operation. There are several commands: write command (CMD_WRITE), read command (CMD_READ), operation end command (CMD_OVER), status check command (CMD_CHECKOK).
The communication process between the two parties is as follows: PC sends a command prefixed with CMD, and then monitors the serial port to wait for the MCU to return a ready response prefixed with RSP (RSP_WRITEREADY when writing; RSP_READREADY when reading). If the timeout is exceeded, an error prompt is given; after receiving the response, the read and write operations are performed; at the end, the PC sends an end command and waits for the end response from the MCU; if the response is received correctly, it prompts success, otherwise it prompts that the operation is completed but no response is received. In the write operation, a confirmation is performed every 18 bytes sent. In addition, since all contents in the chip are read out, the PC does not send an end command at this time but only waits for the end response.
When performing a write operation, the PC sends 18 bytes of data with CMD_WRITE as the command and waits for the MCU's RSP_WRITTEN. When performing a read operation, the PC first receives the 18 bytes of data sent by the MCU with RSP_READ as the response, and then sends a 1-byte command CMD_READ.

3 I2C bus timing simulation
The EEPROM used in this design is AT24C04, which can be used for serial bus. Since 89S52 does not directly support bus, only IO port can be used to simulate bus timing. 89S52 can perform bit addressing, which brings convenience to timing simulation.
(1) Communication start signal
According to regulations, the communication start signal is when SCL (serial clock signal) is at a high level, a falling edge is given on SDA (serial data signal), and after the falling edge, SCL must maintain a high level for 4μs. This process can be simulated by the following code:
_SDA=1;
_SCL=1;
_nop_()
;
_nop_(); _nop_();
_nop_();
_SDA=0; // Falling edge
_nop_();
_nop_();
_nop_ ();
_nop_(); // Maintain SCL at high level for 4us
_SCL=0;
(2) Communication stop signal
The communication stop signal is when SCL is at a high level and a rising edge appears on SDA, and the low level before SDA rises should be maintained for more than 4us. The simulation code for this process is similar to the start signal and is omitted here.
(3) Byte transfer completion confirmation signal
This signal is a positive pulse on SCL when SDA is low. This process can be simulated by the following code:
_SDA=0;
_SCL=1;
_nop_();
_nop_(); _nop_();
_SCL =0; _SDA=1; (4) Non-confirmation signal This signal is convenient for the control party to send a stop signal. When SDA is high, a positive pulse appears on SCL. Apart from this, the simulation code is similar to the byte transfer completion confirmation signal and will not be repeated here. Using the above basic operations and according to the timing relationship of AT24C04, a program for reading and writing AT24C04 can be written. Since the cache in AT24C04 is 16 bytes, the reading and writing are all in units of 16 bytes. The total size of AT24C04 is 512 bytes, which is divided into two pages. When operating, the page to be operated should also be specified at the same time.





4 Communication on the MCU side
After entering the read and write functions, the MCU must respond to the command sent by the PC to confirm that it is ready, so in the read and write program, the EEPROM is not operated immediately. The flowchart of data writing and reading is shown in Figure 1 and Figure 2. Among them, RSP_WRITEREADY is the response to the write ready, RSP_READREADY is the response to the read ready, RSP_READ is the response to the successful reading of a piece of data, and RSP_FIN is the response to the completion of the operation.

Figure 1 Writing to EEPROM

Figure 2 Reading into EPPROM

5 Serial communication on the PC side
The serial communication on the PC side is completed using the CSerialComm class. To complete serial communication on the PC, Microsoft's MSComm communication control can be used, but this requires the library file that encapsulates this control, otherwise the program cannot run independently. Therefore, this design uses the CSerialComm class written in Win32 API to complete it.
The CSerialComm class inherits from CIoController and encapsulates functions related to serial communication. The main member functions are as follows:
Open: Open the specified port. The parameter specifies the name of the port to be opened, which is a string. The serial port opened by this function is synchronous.
SetState: Set the state of the serial port. The parameter is a pointer to the CSerialState class.
SetTimeout: Set the timeout of the operation. If this time is exceeded, the read and write operations will return.
The CSerialComm class only provides basic serial port operations and cannot send window messages, which is not convenient for writing Windows message-based programs. Therefore, a class CI2CWriter is inherited from CSerialComm to further encapsulate the operation of the serial port. The main member functions are as follows:
InitPort: Calls the Open function of CSerialComm to open a serial port and retains the window pointer that owns this type of object in order to send messages.
Notify: Sends a window message to notify the current read and write status.
BeginWrite, BeginRead, BeginVerify, BeginCheckMCU: Start writing, reading, verifying, and checking the status of the MCU respectively. They will generate corresponding working threads.
OpenFile: This function is used to open the file to be written to the EEPROM. The parameter is the path name of the file to be written. Since the capacity of AT24C04 is 512 bytes, this function limits the length of the file. [page]
(1) PC-side write thread
The process is shown in Figure 3. After the write thread is created, it will send a write command CMD_WRITE to the MCU and then wait for the MCU's response RSP_WRITEREADY. After successfully receiving the response, the write thread will send data to the MCU using CMD_WRITE as the command. Each time a group is sent, the write thread will wait for the MCU to respond with RSP_WRITTEN. After successfully receiving this response, the write thread continues to send the following data. When writing is completed, the write thread sends the write end command CMD_OVER and waits for the MCU to respond with RSP_FIN to confirm that the write operation is completed. After successfully receiving this response, a prompt will pop up.

Figure 3 Flowchart of the write thread


The communication between the write thread and the interface thread is achieved by sending messages to the interface thread. The write thread can send the following messages: WM_ _WRITEOVER, WM_ _BLOCKFINISH, WM_ _COMMFAILED. The
WM_ _WRITEOVER message prompts the interface thread that the writing has been completed. At this time, the interface thread enables the checksum and read buttons, disables the write button, and adds a write completion message to the message box.
WM_ _BLOCKFINISH prompts the interface thread that a data block operation has been completed. After receiving this message, the interface thread sets a progress bar to display the current progress.
WM_ _COMMFAILED prompts the interface thread that the communication has failed, the read button is available, and the write button is unavailable.
(2) The read thread on the PC side
The process is shown in Figure 4. After the read thread is created, it will send a write command CMD_READ to the MCU, and then wait for the MCU's response RSP_READREADY. After successfully receiving the response, the read thread will send the CMD_READ command to the MCU and receive the data returned by the MCU. After successfully receiving the data, the read thread checks whether the first byte is RSP_READ. If so, it saves the received data and then issues the CMD_READ command again. This process repeats until all 512 bytes (32 blocks) are completed.

Figure 4 PC side readout flow chart


The communication between the read thread and the interface thread is also achieved by sending messages to the interface thread. The read thread can send the following messages: WM_ _REA DOVER, WM_I2C_BLOCKFINISH, WM_ _COMMFAILED. The latter two messages have the same meaning as the messages sent by the write thread, and the work they do is the same. WM_ _READOVER prompts the interface thread that the readout has been completed. After receiving this message, the interface thread will make the readout button that was disabled at the beginning of the readout enabled, clear the progress bar, and add a message of readout completion to the message box.

6 Conclusion
The above introduces the basic method of writing data from the PC to the EEPROM of the bus. It can be expanded to form a system, such as a text reader, or used as a module in other systems. The simulation code for the bus timing can be used as a general program.

References
[1] Li Qunfang. Single-Chip Microcomputer and Interface Technology (2nd Edition). Beijing: Electronic Industry Press, 2005
[2] Jim Beveridge. Multithreading Applications in Win32 Pearson Education

Reference address:Design of Data Writer Based on I2C Bus

Previous article:A design and application of parallel port expansion for C51 single chip microcomputer
Next article:An elevator voice announcement device based on single chip microcomputer control

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号