?? an ldmicro tutorial.mht
字號:
microcontrollers, but=20
that you have never used LDmicro. If you don't know very =
much about=20
ladder logic or PLCs, then <A=20
href=3D"http://www.plcs.net/contents.shtml">the plcs.net =
tutorial</A>=20
might be helpful to you.</P>
<P>Our device will have one pushbutton, and one LED. At =
startup, the=20
LED will be off. When you press the pushbutton once, the LED =
will=20
turn steady on. The second time you press the pushbutton, =
the LED=20
will start blinking. The third time that you press the =
button, the=20
LED will turn off again. On subsequent presses, the cycle =
will=20
repeat.</P>
<H3>Microcontroller Selection and Schematic</H3>
<P>We will be using a PIC16F876, which is easily available =
from=20
Digikey or other online distributors. It comes in a number =
of=20
different packages; I chose a DIP.</P>
<P>This is our schematic:</P>
<P><IMG style=3D"PADDING-LEFT: 10px"=20
src=3D"http://cq.cx/pics/ldtut-schematic.png"></P>
<P>The microcontroller (IC1) is part number =
PIC16F876-20I/SP-ND at=20
<A href=3D"http://www.digikey.com/">Digikey.</A> Almost any=20
three-terminal resonator (U1) will do; you might try a =
535-9356-ND=20
or an X909-ND.</P>
<P>The only thing that might confuse you is that the =
pushbutton goes=20
to Vdd, and there is a pull-down. You might be more used to =
seeing a=20
pushbutton to ground with a pull-up. For TTL, this mattered. =
For=20
modern CMOS it does not, and I find this =91active HIGH=92 =
arrangement=20
less confusing than the traditional =91active LOW=92 =
circuit.</P>
<P>Also, I chose to use a ceramic resonator with internal=20
capacitors, U1, instead of a crystal and two ~20 pF =
caps. A=20
crystal would work just as well and it would be more =
accurate, but=20
it would be a little bit more expensive, and you would need =
more=20
parts.</P>
<P>You could build this circuit in many different ways. I =
built it=20
on a solderless breadboard, and it ended up looking like =
this:</P>
<P><IMG=20
style=3D"BORDER-RIGHT: black 1px solid; BORDER-TOP: black =
1px solid; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid" =
src=3D"http://cq.cx/pics/ldtut-breadboard.jpg"></P>
<P>(The resistor values pictured are not quite the same as =
the=20
schematic; none of them are critical.)</P>
<H3>Ladder Diagram for the Program</H3>
<P>First, we are going to need an oscillator to generate the =
=91blinking=92 signal for the LED. There is a standard way =
to do this in=20
ladder logic:</P><PRE style=3D"FONT-SIZE: 13px; =
PADDING-BOTTOM: 10px; PADDING-TOP: 10px; font-face: Lucida Console"> =
|| Rosc Tosc_on Tosc_off Rosc =
||
1 ||-------] [--------[TON 250.0 ms]---[TOF 250.0 =
ms]---------(/)-------||
</PRE>
<P>This will flash at 1/((250+250) ms), or 2 Hz, =
or twice=20
per second. The duty cycle will be 50%=97250 ms =
on, then=20
250 ms off. This circuit can make any kind of =
oscillator, with=20
whatever period or duty cycle you require, so it is a good =
one to=20
remember.</P>
<P>Also notice that we have chosen to use an internal relay =
(=91Rfoo=92)=20
instead of one attached to an I/O pin (=91Yfoo=92 or =
=91Xfoo=92). This makes=20
sense, because there is no particular reason to bring that =
signal=20
out to a pin. LDmicro will automatically assign memory for =
the=20
internal relay.</P>
<P>Our program will have three states: off, steady on, and =
blinking.=20
The program should change its state on each rising edge of =
the=20
signal from the pushbutton. This is a good application for a =
circular counter. We will say that =91state 0=92 is =
=91off,=92 =91state 1=92 is=20
=91steady on,=92 and =91state 2=92 is =91blinking.=92 The =
counter counts 0, 1,=20
2, 0, 1, 2, ..., so if we just let the rung-in condition of =
the=20
counter be the pushbutton input, then everything will work =
like we=20
want:</P><PRE style=3D"FONT-SIZE: 13px; PADDING-BOTTOM: =
10px; PADDING-TOP: 10px; font-face: Lucida Console"> || =
Xbutton Cstate ||
2 ||-------] [---------------------------------------------{CTC =
0:2}----||
</PRE>
<P>Now the only thing left is to use the program state to =
set the=20
state of the LED. We can do it like this:</P><PRE =
style=3D"FONT-SIZE: 13px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px; =
font-face: Lucida Console"> || [Cstate =3D=3D] =
Yled ||
3 ||---[ 1 ]-------------------+------------------------( =
)-------||
|| | =
||
|| [Cstate =3D=3D] Rosc | =
||
||---[ 2 ]----------] [------+ =
||
</PRE>
<P>It should be easy to convince yourself that this does =
what we=20
want. If the program is in state 1, then the =91Cstate =
=3D=3D 1=92=20
instruction energizes =91Yled=92, as desired. In state 2, =
the =91Cstate =3D=3D=20
2=92 instruction energizes =91Yled=92, but only when =
=91Rosc=92 is also true.=20
Since =91Rosc=92 is oscillating, that means that the LED =
will blink, as=20
desired. Finally, in state 0, neither of the equals =
instructions=20
will be true, so there is no way that =91Yled=92 could ever =
turn on.</P>
<H3>Entering the Ladder Diagram</H3>
<P>Now that we have our circuit, we can draw it in LDmicro. =
When you=20
start LDmicro, you will see a single empty rung:</P>
<P><IMG src=3D"http://cq.cx/pics/ldtut-empty-rung.png"></P>
<P>We want to enter the first rung from the listing above. =
We will=20
start with the coil, so choose Instruction -> Insert =
Coil. This=20
will create a coil named =91Ynew.=92 This is what we want, =
except that=20
the name is wrong, and it should be negated. Double-click =
the coil;=20
this will bring up a dialog where we can fill that in:</P>
<P><IMG src=3D"http://cq.cx/pics/ldtut-osc-coil.png"></P>
<P>Now we can insert the rest of that rung in the same way. =
Click on=20
the left edge of the coil, so that the cursor is vertical, =
and to=20
the left of the coil. Now choose Instruction -> Insert =
TON=20
(Delayed Turn On). Once again double-click the timer to =
rename it=20
and set the period. Add the TOF timer and the contacts in =
the same=20
way.</P>
<P>Now we want to enter the second rung, so choose Edit =
-> Insert=20
Rung After. Then click on the second rung to move the cursor =
there:</P>
<P><IMG =
src=3D"http://cq.cx/pics/ldtut-second-rung-empty.png"></P>
<P>The second rung is easy: just fill in the two =
instructions in the=20
right order, by placing the cursor where you want to insert =
and then=20
choosing Instruction -> Insert .... Remember to assign a =
name=20
(=91Xbutton=92) to the contacts, and to set the name and =
upper limit of=20
the counter. Then choose Edit -> Insert Rung After again. =
Your=20
program should look like this:</P>
<P><IMG =
src=3D"http://cq.cx/pics/ldtut-third-rung-empty.png"></P>
<P>The third rung will be a bit trickier, because it has =
parallel=20
branches. That means that you have to think about the order =
in which=20
you insert the instructions. First, insert the coil, and =
rename=20
it:</P>
<P><IMG =
src=3D"http://cq.cx/pics/ldtut-third-rung-coil.png"></P>
<P>Now insert the first equals instruction to the left of =
the coil,=20
as usual, and fill in the correct variable name and value. =
After you=20
do that, add the parallel branch. You can do this by =
clicking on the=20
bottom edge of the equals instruction; the cursor will be =
horizontal=20
and below that equals instruction:</P>
<P><IMG =
src=3D"http://cq.cx/pics/ldtut-third-rung-one-cond.png"></P>
<P>Now choose Instruction -> Insert EQU (Compare for =
Equals).=20
Since your cursor is below the first equals instruction, the =
new=20
equals instruction will be inserted below that instruction, =
in=20
parallel with it. Rename it as usual. To finish the rung, =
you must=20
insert the =91Rosc=92 contacts to the right of the second =
equals=20
instruction. To do this, click on the right edge of the =
second=20
equals instruction:</P>
<P><IMG =
src=3D"http://cq.cx/pics/ldtut-third-rung-both-cond.png"></P>
<P>At this point you can choose Instruction -> Insert =
Coil; the=20
coil will be inserted in series with the second equals =
instruction,=20
as you require. Rename it and you are done:</P>
<P><IMG =
src=3D"http://cq.cx/pics/ldtut-third-rung-complete.png"></P>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -