⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ladder logic for pic and avr.mht

📁 用PIC單片機與AVR單片機製作PLC控制的階梯圖程序。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
              sequencer)=20
              <LI>analog inputs, analog (PWM) outputs=20
              <LI>integer variables and arithmetic instructions=20
              <LI>easy-to-use serial communications, to a PC, LCD, or =
other=20
              device=20
              <LI>shift registers, look-up tables=20
              <LI>EEPROM variables, whose values are not forgotten when =
you lose=20
              power=20
              <LI>simulator, to test your program before you generate =
PIC/AVR=20
              code </LI></UL>
            <P>You can <A href=3D"http://cq.cx/ladder.pl#dl">download it =
for=20
            free.</A> Or, with more background:</P>
            <H3>Introduction</H3>
            <P>PLCs are often programmed in ladder logic. This is =
because PLCs=20
            originally replaced relay control systems, and forty years =
later, we=20
            still haven't quite let go. A PLC, like any microprocessor, =
executes=20
            a list of instructions in sequence. Ladder logic tools =
abstract=20
            this; you can program the PLC by wiring up relay contacts =
and coils=20
            on-screen, and the PLC runtime will simulate the circuit =
that you've=20
            drawn. Some of the relay contacts can be tied to input =
signals from=20
            the real world; some of the coils can be tied to outputs. =
That way=20
            you can make your simulated circuit interact with other =
devices, and=20
            actually control things. That is the point.</P>
            <P>Actually it's more general than that, because you can =
incorporate=20
            timers and counters and arithmetic operations that you =
couldn't=20
            (easily) perform with just relays. The circuit concept is =
still=20
            useful though, partly just because it's intuitive, but also =
because=20
            it abstracts the concurrency issues. It looks like =
this:</P><PRE style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: =
13px; PADDING-BOTTOM: 10px; PADDING-TOP: 3px; font-face: Lucida =
Console">         ||       Xa               Xb              Yout       =
||
       1 ||-------] [------+-------] [------+-------( )-------||
         ||                |                |                 ||
         ||                |       Xc       |                 ||
         ||                +-------]/[------+                 ||
</PRE>
            <P>This is a simple piece of combinational logic. There are =
three=20
            input terms, Xa, Xb, and Xc. There is one output term, Yout. =
The=20
            expression is Yout :=3D Xa and (Xb or (not Xc)). This makes =
sense if=20
            you think of Xa and Xb as normally open relay contacts, Xc =
as=20
            normally closed relay contacts, and Yout as a relay coil. Of =
course=20
            it gets more complicated than that:</P><PRE =
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 13px; =
PADDING-BOTTOM: 10px; PADDING-TOP: 3px; font-face: Lucida Console">      =
   ||                                                   ||
         ||                                      Asetpoint    ||
       1 ||-------------------------------------{READ ADC}----||
         ||                                                   ||
         ||                                    Atemperature   ||
         ||-------------------------------------{READ ADC}----||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||                        {SUB  min_temp  :=3D}        ||
       2 ||------------------------{ Asetpoint - 20  }--------||
         ||                                                   ||
         ||                        {ADD  max_temp  :=3D}        ||
         ||------------------------{ Asetpoint + 20  }--------||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||[Atemperature &gt;]                       Yheater     ||
       3 ||[ max_temp     ]+------------------------(R)-------||
         ||                |                                  ||
         ||     Xenable    |                                  ||
         ||-------]/[------+                                  ||
         ||                                                   ||
         ||[Atemperature &lt;]      Xenable          Yheater     ||
         ||[ min_temp     ]--------] [--------------(S)-------||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||                       {SUB  check_temp  :=3D}       ||
       4 ||-----------------------{ Asetpoint - 30    }-------||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||[Atemperature &gt;]                       Yis_hot     ||
       5 ||[ check_temp   ]-------------------------( )-------||
         ||                                                   ||
         ||                                                   ||
         ||                                                   ||
         ||------[END]----------------------------------------||
         ||                                                   ||
         ||                                                   ||
</PRE>
            <P>This is for a simple thermostat. There are two analog =
inputs; one=20
            of them is for the setpoint, so that it might, for example, =
be=20
            connected to a pot that the user turns to select the desired =

            temperature. The other provides the temperature measurement; =
it=20
            might be a semiconductor temperature sensor, or a platinum =
RTD with=20
            suitable interfacing circuitry. There is a digital output, =
Yheater.=20
            That might control a heating element, through a suitable =
switch (a=20
            TRIAC, or a relay, or a solid-state relay, or whatever). =
</P>
            <P>We close the loop with a simple hysteretic (bang-bang)=20
            controller. We have selected plus or minus 20 ADC units of=20
            hysteresis. That means that when the temperature falls below =

            (setpoint - 20), we turn on the heater, and when it climbs =
above=20
            (setpoint + 20), we turn the heater off.</P>
            <P>I chose to add a few small frills. First, there is an =
enable=20
            input: the heater is forced off when Xenable is low. I also =
added an=20
            indicator light, Yis_hot, to indicate that the temperature =
is within=20
            regulation. This compares against a threshold slightly =
colder than=20
            (setpoint - 20), so that the light does not flicker with the =
normal=20
            cycling of the thermostat.</P>
            <P>This is a trivial example, but it should be clear that =
the=20
            language is quite expressive. Ladder logic is not a =
general-purpose=20
            programming language, but it is Turing-complete, accepted in =

            industry, and, for a limited class of (mostly =
control-oriented)=20
            problems, surprisingly convenient.</P>
            <H3>A Ladder Logic Compiler for PIC16 and AVR</H3>
            <P>Modern sub-3.00 USD microcontrollers probably have about =
the=20
            computing power of a PLC circa 1975. They therefore provide =
more=20
            than enough MIPS to run reasonably complex ladder logic with =
a cycle=20
            time of a few milliseconds. I think PLCs usually have some =
sort of=20
            runtime that's kind of like an interpreter or a virtual =
machine, but=20
            if we're doing simple logic on a processor without much =
memory then=20
            a compiler might be a better idea.</P>
            <P>So I wrote a compiler. You start with an empty rung. You =
can add=20
            contacts (inputs) and coils (outputs) and more complicated=20
            structures to build up your program. Timers (TON, TOF, RTO) =
are=20
            supported. The max/min durations depend on the cycle time of =
the=20
            =91PLC,=92 which is configurable; timers can count from =
milliseconds to=20
            tens of minutes. There are counters and arithmetic =
operations (plus,=20
            minus, times, div).</P>
            <P>Circuit elements may be added in series or in parallel =
with=20
            existing elements. An I/O list is built from the ladder =
logic drawn.=20
            You can have internal relays (Rfoo), for which memory is=20
            automatically allocated, or inputs (Xfoo) and outputs =
(Yfoo), to=20
            which you must assign a pin on the microcontroller. The =
selection of=20
            pins available depends on the microcontroller. I have tried =
to=20
            support the most popular PICs and AVRs (see <A=20
            href=3D"http://cq.cx/ladder.pl#dl">below</A>).</P>
            <P>You can edit the program in graphical form:</P>
            <P><IMG src=3D"http://cq.cx/pics/ladder-sample.png"></P>
            <P>Then you can test the program by simulating it in real =
time. The=20
            program appears on screen with the energized (true) branches =

            highlighted, which makes it easy to debug. The state of all=20
            variables is shown at the bottom of the screen in the I/O =
list.</P>
            <P><IMG src=3D"http://cq.cx/pics/ladder-sim.png"></P>
            <P>Once the program works in simulation you can assign pins =
to the=20
            inputs and outputs and generate PIC or AVR code. The code =
generator=20
            isn't all that difficult. If you realize that a parallel =
circuit is=20
            an OR and a series circuit is an AND, it's a second-year CS=20
            assignment, and not a very long one. The editor is actually =
much=20
            more challenging. It would take some work to make a smart =
compiler,=20
            though. For the AVR a good register allocator would provide =
a major=20
            speedup. If you wanted to get very fancy then you could =
apply=20
            standard logic reduction algorithms, and maybe state =
reduction too.=20
            That would be much more difficult. The timers screw things =
up=20
            anyways.</P>
            <P>Even ignoring all that, my code generator for the AVR is =
very=20
            poor. The AVR back end still generates PIC code...for =
example, it=20
            does not really take advantage of the fact that the AVR has =
more=20
            than one register. Some of the code that it generates is =
just=20
            embarrassingly bad. The PIC back end is better, but not =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -