Ladderlib
Library for execute ladder logic
Install / Use
/learn @hiperiondev/LadderlibREADME
<a name="readme-top"></a>
<!-- PROJECT LOGO --> <br /> <div align="center"> <a href="https://github.com/hiperiondev/ladderlib"> <img src="images/logo.png" alt="Logo" width="140" height="80"> </a> <h3 align="center">Ladderlib</h3> <p align="center"> C library for ladder logic programming <br /> </p> </div> <!-- ABOUT THE PROJECT -->About The Project
Ladderlib is a C library designed for implementing ladder logic programming, a method widely used in Programmable Logic Controllers (PLCs) for industrial automation. It provides a robust framework to define, execute, and manage ladder logic programs, supporting hardware interactions and custom function extensions.
Ladderlib enables developers to create ladder logic programs, which are graphical representations of control logic resembling electrical relay circuits. These programs are executed cyclically by PLCs to automate industrial processes, such as controlling machinery in manufacturing or chemical processing. The library supports defining logic through cells and networks, managing execution states, handling hardware inputs/outputs, and integrating custom functions for enhanced flexibility.
Key features include:
- Instruction Set: 39 instructions for defining logic operations.
- State Management: 6 states to control program execution.
- Error Handling: 8 error codes for robust operation.
- Hardware Integration: Function pointers for reading inputs and writing outputs.
- Custom Extensions: Support for adding user-defined functions.
The library is likely optimized for embedded systems.
Introduction to Ladder Logic
Ladder logic programming stands as the fundamental programming language for Programmable Logic Controllers (PLCs), powering approximately 80% of industrial automation systems worldwide.
It’s a visual programming methodology that evolved from traditional relay-based control systems, provides an intuitive approach to implementing complex control logic in manufacturing processes. The purpose of ladder logic programming is to simplify the process of programming and controlling complex machinery, such as assembly lines and manufacturing equipment. The transition from physical relay panels to digital programming has maintained the familiar ladder diagram format while incorporating advanced digital capabilities, making it the preferred choice for automation engineers and technicians.
Ladder logic is a fast and simple way of creating logic expressions for a PLC in order to automate repetitive machine tasks and sequences. It is used in a multitude of industrial automation applications.
Table of Contents
- Basic Logic Instructions
- API Documentation
- Enums and Structures
- Callback Prototypes
- Basic Usage
- Contributing
- License
- Contact
Basic Logic Instructions
Instruction Summary Table
The following table summarizes the instructions, their codes, categories, and symbols.
| Instruction | Code | Category | Symbol | Description |
|-------------|------|----------|--------|-------------|
| NOP | 0 | Specialized | None | No operation, placeholder |
| CONN | 1 | Specialized | Vertical lines | Connects rungs or branches |
| NEG | 2 | Specialized | <img src="images/png/NEG.png" alt="Image"> | Inverts logic state |
| NO | 3 | Basic Logic | <img src="images/png/NO.png" alt="Image"> | Normally Open contact |
| NC | 4 | Basic Logic | <img src="images/png/NC.png" alt="Image"> | Normally Closed contact |
| RE | 5 | Specialized | <img src="images/png/RE.png" alt="Image"> | Rising edge detection |
| FE | 6 | Specialized | <img src="images/png/FE.png" alt="Image"> | Falling edge detection |
| COIL | 7 | Basic Logic | <img src="images/png/COIL.png" alt="Image"> | Output coil |
| COILL | 8 | Basic Logic | <img src="images/png/COILL.png" alt="Image"> | Latched coil |
| COILU | 9 | Basic Logic | <img src="images/png/COILU.png" alt="Image"> | Unlatched coil |
| TON | 10 | Timer | <img src="images/png/TON.png" alt="Image"> | Timer On Delay |
| TOFF | 11 | Timer | <img src="images/png/TOF.png" alt="Image"> | Timer Off Delay |
| TP | 12 | Timer | <img src="images/png/TP.png" alt="Image"> | Timer Pulse |
| CTU | 13 | Counter | <img src="images/png/CTU.png" alt="Image"> | Count Up |
| CTD | 14 | Counter | <img src="images/png/CTD.png" alt="Image"> | Count Down |
| MOVE | 15 | Specialized | <img src="images/png/MOV.png" alt="Image"> | Moves data |
| SUB | 16 | Arithmetic | <img src="images/png/SUB.png" alt="Image"> | Subtraction |
| ADD | 17 | Arithmetic | <img src="images/png/ADD.png" alt="Image"> | Addition |
| MUL | 18 | Arithmetic | <img src="images/png/MUL.png" alt="Image"> | Multiplication |
| DIV | 19 | Arithmetic | <img src="images/png/DIV.png" alt="Image">| Division |
| MOD | 20 | Arithmetic | <img src="images/png/MOD.png" alt="Image"> | Modulus |
| SHL | 21 | Bitwise | <img src="images/png/SHL.png" alt="Image"> | Shift Left |
| SHR | 22 | Bitwise | <img src="images/png/SHR.png" alt="Image"> | Shift Right |
| ROL | 23 | Bitwise | <img src="images/png/ROL.png" alt="Image"> | Rotate Left |
| ROR | 24 | Bitwise | <img src="images/png/ROR.png" alt="Image"> | Rotate Right |
| AND | 25 | Bitwise | <img src="images/png/AND.png" alt="Image"> | Bitwise AND |
| OR | 26 | Bitwise | <img src="images/png/OR.png" alt="Image"> | Bitwise OR |
| XOR | 27 | Bitwise | <img src="images/png/XOR.png" alt="Image"> | Bitwise XOR |
| NOT | 28 | Bitwise | <img src="images/png/NOT.png" alt="Image"> | Bitwise NOT |
| EQ | 29 | Comparison | <img src="images/png/EQ.png" alt="Image"> | Equal |
| GT | 30 | Comparison | <img src="images/png/GT.png" alt="Image"> | Greater Than |
| GE | 31 | Comparison | <img src="images/png/GE.png" alt="Image"> | Greater Than or Equal |
| LT | 32 | Comparison | <img src="images/png/LT.png" alt="Image"> | Less Than |
| LE | 33 | Comparison | <img src="images/png/LE.png" alt="Image"> | Less Than or Equal |
| NE | 34 | Comparison | <img src="images/png/NE.png" alt="Image"> | Not Equal |
| FOREIGN | 35 | Specialized | Box labeled "FOREIGN" | External function call |
| TMOVE | 36 | Specialized | <img src="images/png/TMOV.png" alt="Image"> | Table data move |
| INV | 37 | Specialized | None | Invalid instruction |
Basic Instructions
Basic instructions manage Input/Output functions
NO (Normally Open Contact)
- Instruction Code:
LADDER_INS_NO(3) - Description: The Normally Open contact allows logic flow when its associated input is true, acting like a switch that closes when activated. It is used to check if an input condition (e.g., a sensor) is met.
- Example Use: Checking if a pushbutton is pressed to activate a motor.
NC (Normally Closed Contact)
- Instruction Code:
LADDER_INS_NC(4) - Description: The Normally Closed contact allows logic flow when its associated input is false, acting like a switch that opens when activated. It is used to proceed when a condition is not met.
- Example Use: Ensuring a safety switch is not triggered before starting a process.
COIL (Output Coil)
- Instruction Code:
LADDER_INS_COIL(7) - Description: The Coil instruction sets an output to true when the rung's logic is true, controlling devices like motors or lights. It represents the action taken when conditions are satisfied.
- Example Use: Turning on a conveyor belt when all conditions are met.
COILL (Coil Latch)
- Instruction Code:
LADDER_INS_COILL(8) - Description: The Coil Latch sets an output to true and maintains it (latches) even if the input conditions change, until unlatched. It is used for memory functions in control systems.
- Example Use: Keeping a pump running after a start button is released.
COILU (Coil Unlatch)
- Instruction Code:
LADDER_INS_COILU(9) - Description: The Coil Unlatch resets a latched output to false, releasing the latched state. It is paired with
COILLfor set/reset operations. - Example Use: Stopping a latched pump when a stop button is pressed.
Timer Instructions
Timer instructions manage time-based operations, delaying or pulsing outputs.
TON (Timer On Delay)
- Instruction Code:
LADDER_INS_TON(10) - Description: The Timer On Delay starts timing when its input is true. After a preset time, the output becomes true. It is used to delay actions, such as waiting before starting a motor.
- Example Use: Delaying a fan activation for 10 seconds after a heater starts.
TOFF (Timer Off Delay)
- Instruction Code:
LADDER_INS_TOFF(11) - Description: The Timer Off Delay starts timing when its input goes false. After a preset time, the output becomes false. It is used to extend the duration of an output.
- Example Use: Keeping a light on for 5 seconds after a switch is turned off.
TP (Timer Pulse)
- Instruction Code:
LADDER_INS_TP(12) - Description: The Timer Pulse generates a pulse of a specified duration when triggered by a true input. It is used for creating timed signals, such as a brief alarm.
- Example Use: Activating a buzzer for 2 seconds when a fault is detected.
Counter Instructions
Counter instructions track the number of events or occurrences.
CTU (Count Up)
- Instruction Code:
LADDER_INS_CTU(13) - Description: The Count Up counter increments its count each time the input transitions from false to true, up to a preset value. It is used to count items or events.
- Example Use: Counting the number of boxes on a conveyor belt.
CTD (Count Down)
- Instruction Code:
LADDER_INS_CTD(14)
