For over forty years, PLCs have been a mainstay of industrial control. Their functions have continuously improved, and their application areas have expanded, playing an immeasurable role in the advancement of industrial control technology and social development. With their high reliability and ease of operation, PLCs have dominated the industrial control industry for decades. While PLCs possess inherent advantages, to survive in the face of ever-changing customer demands, they must break through traditional models and actively seek innovation to adapt to new market developments. Embedded PLCs, with their low-cost advantage, perfectly meet this need. An embedded PLC refers to a PLC that uses a SoC (System-on-a-Chip) embedded system chip and an embedded real-time operating system to implement PLC functions and can be programmed using the IEC61131-3 standard programming language. With the development of high-performance ARM embedded microprocessors, this paper designs a new generation of micro embedded PLCs. This paper introduces the architecture of embedded PLCs, including their hardware and software design schemes.
1. Hardware Structure Design of Embedded PLC
1.1 Selection of Microcontroller Chip
The CPU is the core of the PLC. It can recognize various instructions input by the user in a specific format and, according to the instructions and the current state of the field I/O signals, issue corresponding control instructions to complete the predetermined control tasks. This design uses the Philips LPC2294 microcontroller. The LPC2294 is a 32-bit ARM7TDMI-S based CPU chip that supports real-time emulation and tracing. It features 256kB of embedded high-speed Flash memory and 16kB of on-chip SRAM. The LPC2294 is packaged in a 144-pin package, has extremely low power consumption, up to 112 general-purpose I/O ports, 9 edge-triggered or level-triggered external interrupt pins, a maximum operating crystal oscillator of 60MHz, multiple 32-bit timers, a PWM unit, a real-time clock and watchdog timer, an 8-channel 10-bit ADC with a conversion time as low as 2.44μs, 4 advanced CAN interfaces, 2 UARTs (16C550), high-speed I2C (400kbit/s), and 2 SPI buses. The LPC2294's abundant hardware resources and comprehensive functions make this microcontroller particularly suitable for automotive, industrial control applications, medical systems, and fault-tolerant maintenance buses.
1.2 Overall Structure of the Hardware System
This system uses the ARM chip LPC2294 as the CPU and is designed with a basic mode of 14 PNP inputs and 10 relay outputs. The overall hardware structure includes:
The system includes a power supply and reset module, an ARM microcontroller, a Flash memory expansion module, a digital input/output module, an analog input/output module, an RS485 interface, and a CAN interface communication module. The system structure is shown in Figure 1.
1.2.1 Digital Input/Output Interface Circuit
Figure 2 shows a single-channel switch input. The front end of this circuit uses a first-order filter circuit composed of resistors and capacitors to prevent external interference signals from entering the system. The external input control switch signal (DC 24V) is input to the optocoupler (PC816) through input point 10.0 and a current-limiting resistor. M is the common input terminal for input points 10.0 to 10.7. Because port P0.23 is set to input mode and has no internal pull-up resistor, an external pull-up resistor is required to prevent the port from floating. When input 10.0 is at 24V, the photodiode in the optocoupler conducts, the output of the phototransistor is pulled low, the LED indicating the input status is lit, and P0.23 is set low. When the CPU accesses this signal, it sets the value of the input process image register corresponding to this input point to 1. When the input terminal of 10.0 is 0V, P0.23 is high. When the CPU accesses this signal, it sets the value of the input process image register corresponding to this input point to 0. The circuits and working principles corresponding to the other input points are the same.
Figure 3 shows the relay output module diagram. The diode connected in parallel across the relay coil serves as a freewheeling current. The module operates as follows: When the internal output process image register is 1, port P1.16 of the LPC2294 outputs 0, the phototransistor conducts, the relay coil is energized, and the output point is connected; conversely, when the internal output process image register is 0, port P1.16 outputs 1, the relay coil is de-energized, and the output point is disconnected.
It is important to note that when the LPC2294's GPIO port is initially powered on, the voltage at its output port (such as P1.16 in this diagram) is unstable. This can easily lead to malfunctions of external relays and cause instability in the operation of external devices. Therefore, we designed the circuit shown in Figure 4 to improve the stability of the relay output.
This is a monostable circuit composed of an NE555 timer, where VCC5.0D is connected to the collector of the optocoupler in Figure 3. Its working principle is as follows: Initially, when the system is powered on, the voltage levels of pins 2 and 6 cannot change abruptly and remain low. Analysis of the NE555's internal circuit shows that at this time, pin 3 outputs a high level, and the circuit begins charging the R and C circuits. As time progresses, the voltage levels of pins 2 and 6 continuously rise. When they reach 23VCC, pin 3 flips to a low level, turning on the transistor, and VCC5.0D outputs 5V. Thus, after a period of time following system power-on, once the I/O port voltage stabilizes, the optocoupler is powered on and begins to work. The duration tW of the quasi-stable state depends on the values of the external resistor R and capacitor C. tW is equal to the time required for the capacitor voltage to rise from 0 to 23VCC during charging, i.e.,
1.2.2 Analog Input Circuit Design
First, the current signal output by the field sensor is converted into a 0~5V voltage signal through resistor R66 for acquisition. Considering anti-interference and protection of the microprocessor circuit, a linear optocoupler HCNR201 is added to the output of the conversion circuit. The hardware circuit is shown in Figure 5.
1.2.3 Serial Communication Interface Circuit Design
To ensure compatibility with other industrial control products, we adopted the RS-485 interface standard in our design. To convert TTL levels to RS485 levels, we selected the SP485E transceiver. The SP485E chip offers data transmission rates up to 10Mbps, and its most significant feature is the ESD protection circuitry provided for both the transmitter output and receiver input pins. The interface circuit is shown in Figure 6.
2. Software System Design of Embedded PLC
Embedded PLC software is divided into two parts: runtime system software and development system software. The runtime system is responsible for managing the entire system, compiling and executing user programs, saving all data, and completing communication with the outside world. The development system is user-facing, handling the editing and conversion of PLC programs.
2.1 PLC Operating System Software
This system is responsible for allocating memory for the application, loading the application into the allocated memory, and then executing the program's instructions. If the program requests services from the underlying operating system, the runtime system must also handle the relevant service requests. This runtime system is developed based on the embedded operating system μC/OS-II. Choosing an embedded operating system improves the software system's anti-interference capabilities, system reliability, and application software development efficiency, while shortening the development cycle. The main work involved in porting μC/OS-II was modifying the code related to the ARM processor, which is concentrated in three files.
① The OS_CPU.H file contains processor-related constants, macros, and type definitions defined using #define. The definitions of these data types in the file are as follows:
typedef unsigned char BOOLEAN;
typedef unsigned char INT8U;
typedef signed char INT8S;
typedef unsigned short INT16U;
typedef signed short INT16S;
typedef unsigned int INT32U;
typedef signed int INT32S;
typedef floatFP32;
typedef doubleFP64;
typedef unsigned int OS_STK;
The following are some definitions related to the ARM7 architecture:
#define OS_CRITICAL_METHOD2
__swi(0×00)voidOS_TASK_SW(void);
__swi(0×01)void_OSStartHighRdy(void);
__swi(0×02)voidOS_ENTER_CRITICAL(void);
__swi(0×03)voidOS_EXIT_CRITICAL(void);_
_swi(0×40)void*GetOSFunctionAddr(intIndex);
__swi(0×41)void*GetUsrFunctionAddr(intIndex);
__swi(0×42)voidOSISRBegin(void);
__swi(0×43)intOSISRNeedSwap(void);
__swi(0×80)voidChangeToSYSMode(void);
__swi(0×81)voidChangeToUSRMode(void);
__swi(0×82)voidTaskIsARM(INT8Uprio);
__swi(0×83)voidTaskIsTHUMB(INT8Uprio);
/* The above functions need to be declared in the porting file OS_CPU.H. */
/* #define OS_STK_GROWTH1
The OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL() functions in this code segment implement the functionality of turning the processor on and off.
②The OS_CPU_C.C file contains the task stack structure initialization function OSTaskStkInit(), which must be initialized according to the task stack structure uniformly defined during porting. Additionally, there are 9 system-defined hook functions that must be declared, but they can be empty (without any code). In this porting, these hook functions are all empty functions.
③ The porting of the OS_CPU_A.S file includes four functions: OSStartHighRdy() called in the multi-task startup function, OSCtxSw() task switching function, OSIntCtxSw() interrupt task switching function, and OSTickISR() clock tick service function.
This completes the porting of the μC/OS-II kernel. Subsequent user programs are all extensions built upon this foundation.
2.2 PLC Development System Software
The main task of this system is to enable users to write PLC programs, so a corresponding programming platform also needs to be designed. The design of the programming platform mainly includes the design of the programming interface, the editor, the conversion module, the compiler, and the communication module. The software system structure diagram is shown in Figure 7.
Users write PLC programs on a programming platform. Here, the SWOPC-FXGP/WIN-C programming software for the FX series PLCs is used as the programming platform, and ladder diagrams and instruction lists can be used as the programming language. Then, a conversion program transforms the compiled object file into C language. The conversion program is essentially an interpreter that translates the instruction list of the programming software line by line to generate user instructions independent of the processor's instruction set. Using this method as the host computer programming platform saves workload.
3 System Testing
The designed PLC software system was embedded into an LPC2294-based embedded development platform and connected to the PLC input/output hardware interface board to form a 14-input, 10-output PLC system. The corresponding PLC ladder diagram was written in the upper-level development system, compiled, and loaded into the embedded PLC's runtime system. The ladder diagram is shown in Figure 8.
Pressing switch 0 illuminates LED0, and after a 4-second delay, LED1 illuminates. Pressing switch 1 illuminates LED2, while LED0 turns off.
The test results above show that the prototype's test results are consistent with the theoretical analysis results. The designed PLC control system hardware, software, and μCOS-II operating system have achieved the expected control effect and met the design requirements.
4. Conclusion
This paper addresses some shortcomings of conventional PLCs and proposes an embedded PLC design based on the LPC2294. This embedded PLC offers flexible and customizable hardware, software, and communication functionalities, better meeting the requirements of various levels of electromechanical equipment. Based entirely on embedded system technology, this PLC is ready to use, and its SOC chip, embedded operating system, and IEC61131-3 compliant programming language standard programming environment are readily available on the market. Therefore, the application and promotion prospects of this embedded PLC in the Chinese market are very promising.