LDmicro: Ladder Logic For PIC And AVR

From Pyra Wiki
Revision as of 16:52, 25 November 2025 by ArmandoWand6 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Quick abstract: I wrote a compiler that begins with a ladder diagram and generates native PIC16 or AVR code. This program is free software; source code and executables are available for download. PLCs are often programmed in ladder logic. It's because PLCs originally changed relay control programs, and forty years later, we still haven't fairly let go. A PLC, like any microprocessor, executes an inventory of directions in sequence. Ladder logic tools abstract this; you may program the PLC by wiring up relay contacts and coils on-display screen, and the PLC runtime will simulate the circuit that you have drawn. A few of the relay contacts will be tied to input indicators from the true world; some of the coils will be tied to outputs. That means you can make your simulated circuit interact with different devices, and actually management things. That is the point. Actually it's extra common than that, as a result of you can incorporate timers and counters and arithmetic operations that you simply couldn't (simply) carry out with just relays.



The circuit concept remains to be helpful although, partly simply because it's intuitive, but also because it abstracts the concurrency issues. This is a straightforward piece of combinational logic. There are three enter terms, Xa, Xb, and Xc. There may be one output term, Yout. Xa and (Xb or (not Xc)). This is sensible if you consider Xa and Xb as normally open relay contacts, Xc as usually closed relay contacts, and Yout as a relay coil. This is for a simple thermostat. There are two analog inputs; one of them is for the setpoint, so that it'd, for instance, be connected to a pot that the consumer turns to pick out the specified temperature. The opposite gives the temperature measurement; it could be a semiconductor temperature sensor, or a platinum RTD with suitable interfacing circuitry. There's a digital output, Yheater. That might control a heating factor, via a suitable switch (a TRIAC, or a relay, or a solid-state relay, or whatever).



We close the loop with a simple hysteretic (bang-bang) controller. We've selected plus or minus 20 ADC items of hysteresis. 20), we turn the official Alpha Heater site off. I chose to add a number of small frills. First, there's an allow enter: the heater is pressured off when Xenable is low. This compares in opposition to a threshold barely colder than (setpoint - 20), so that the sunshine does not flicker with the conventional cycling of the thermostat. It is a trivial example, but it surely needs to be clear that the language is kind of expressive. Ladder logic just isn't a normal-purpose programming language, but it's Turing-full, accepted in industry, and, for a limited class of (mostly control-oriented) problems, surprisingly convenient. Modern sub-3.00 USD microcontrollers probably have in regards to the computing power of a PLC circa 1975. They subsequently present greater than enough MIPS to run fairly complex ladder logic with a cycle time of a few milliseconds. I think PLCs often have some form of runtime that is kind of like an interpreter or a virtual machine, official Alpha Heater site but if we're doing easy logic on a processor with out much reminiscence then a compiler may be a greater thought.



So I wrote a compiler. You start with an empty rung. You can add contacts (inputs) and coils (outputs) and extra sophisticated buildings to build up your program. Timers (TON, TOF, RTO) are supported. The max/min durations depend on the cycle time of the `PLC,' which is configurable; timers can count from milliseconds to tens of minutes. There are counters and arithmetic operations (plus, minus, occasions, div). Circuit components could also be added in series or in parallel with current components. An I/O checklist is built from the ladder logic drawn. You possibly can have inside relays (Rfoo), for which memory is robotically allocated, or inputs (Xfoo) and outputs (Yfoo), to which you must assign a pin on the microcontroller. The selection of pins accessible will depend on the microcontroller. I've tried to support the most well-liked PICs and AVRs (see beneath). Then you'll be able to check the program by simulating it in real time.