📄 ldmicro.txt
字号:
down) the associated count on every rising edge of the rung input
condition (i.e. what the rung input condition goes from false to
true). The output condition from the counter is true if the counter
variable is greater than or equal to 5, and false otherwise. The
rung output condition may be true even if the input condition is
false; it only depends on the counter variable. You can have CTU
and CTD instructions with the same name, in order to increment and
decrement the same counter. The RES instruction can reset a counter,
or you can perform general variable operations on the count variable.
> CIRCULAR COUNTER Cname
--{CTC 0:7}--
A circular counter works like a normal CTU counter, except that
after reaching its upper limit, it resets its counter variable
back to 0. For example, the counter shown above would count 0, 1,
2, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 2,.... This is useful in
combination with conditional statements on the variable `Cname';
you can use this like a sequencer. CTC counters clock on the rising
edge of the rung input condition condition. This instruction must
be the rightmost instruction in its rung.
> SHIFT REGISTER {SHIFT REG }
-{ reg0..3 }-
A shift register is associated with a set of variables. For example,
this shift register is associated with the variables `reg0', `reg1',
`reg2', and `reg3'. The input to the shift register is `reg0'. On
every rising edge of the rung-in condition, the shift register will
shift right. That means that it assigns `reg3 := reg2', `reg2 :=
reg1'. and `reg1 := reg0'. `reg0' is left unchanged. A large shift
register can easily consume a lot of memory. This instruction must
be the rightmost instruction in its rung.
> LOOK-UP TABLE {dest := }
-{ LUT[i] }-
A look-up table is an ordered set of n values. When the rung-in
condition is true, the integer variable `dest' is set equal to the
entry in the lookup table corresponding to the integer variable
`i'. The index starts from zero, so `i' must be between 0 and
(n-1). The behaviour of this instruction is not defined if the
index is outside this range. This instruction must be the rightmost
instruction in its rung.
> PIECEWISE LINEAR TABLE {yvar := }
-{ PWL[xvar] }-
This is a good way to approximate a complicated function or
curve. It might, for example, be useful if you are trying to apply
a calibration curve to convert a raw output voltage from a sensor
into more convenient units.
Assume that you are trying to approximate a function that converts
an integer input variable, x, to an integer output variable, y. You
know the function at several points; for example, you might know that
f(0) = 2
f(5) = 10
f(10) = 50
f(100) = 100
This means that the points
(x0, y0) = ( 0, 2)
(x1, y1) = ( 5, 10)
(x2, y2) = ( 10, 50)
(x3, y3) = (100, 100)
lie on that curve. You can enter those 4 points into a table
associated with the piecewise linear instruction. The piecewise linear
instruction will look at the value of xvar, and set the value of
yvar. It will set yvar in such a way that the piecewise linear curve
will pass through all of the points that you give it; for example,
if you set xvar = 10, then the instruction will set yvar = 50.
If you give the instruction a value of xvar that lies between two
of the values of x for which you have given it points, then the
instruction will set yvar so that (xvar, yvar) lies on the straight
line connecting those two points in the table. For example, xvar =
55 gives an output of yvar = 75. (The two points in the table are
(10, 50) and (100, 100). 55 is half-way between 10 and 100, and 75
is half-way between 50 and 100, so (55, 75) lies on the line that
connects those two points.)
The points must be specified in ascending order by x coordinate. It
may not be possible to perform mathematical operations required for
certain look-up tables using 16-bit integer math; if this is the
case, then LDmicro will warn you. For example, this look up table
will produce an error:
(x0, y0) = ( 0, 0)
(x1, y1) = (300, 300)
You can fix these errors by making the distance between points in
the table smaller. For example, this table is equivalent to the one
given above, and it does not produce an error:
(x0, y0) = ( 0, 0)
(x1, y1) = (150, 150)
(x2, y2) = (300, 300)
It should hardly ever be necessary to use more than five or six
points. Adding more points makes your code larger and slower to
execute. The behaviour if you pass a value of `xvar' greater than
the greatest x coordinate in the table or less than the smallest x
coordinate in the table is undefined. This instruction must be the
rightmost instruction in its rung.
> A/D CONVERTER READ Aname
--{READ ADC}--
LDmicro can generate code to use the A/D converters built in to
certain microcontrollers. If the input condition to this instruction
is true, then a single sample from the A/D converter is acquired and
stored in the variable `Aname'. This variable can subsequently be
manipulated with general variable operations (less than, greater than,
arithmetic, and so on). Assign a pin to the `Axxx' variable in the
same way that you would assign a pin to a digital input or output,
by double-clicking it in the list at the bottom of the screen. If
the input condition to this rung is false then the variable `Aname'
is left unchanged.
For all currently-supported devices, 0 volts input corresponds to
an ADC reading of 0, and an input equal to Vdd (the supply voltage)
corresponds to an ADC reading of 1023. If you are using an AVR, then
connect AREF to Vdd. You can use arithmetic operations to scale the
reading to more convenient units afterwards, but remember that you
are using integer math. In general not all pins will be available
for use with the A/D converter. The software will not allow you to
assign non-A/D pins to an analog input. This instruction must be
the rightmost instruction in its rung.
> SET PWM DUTY CYCLE duty_cycle
-{PWM 32.8 kHz}-
LDmicro can generate code to use the PWM peripheral built in to
certain microcontrollers. If the input condition to this instruction
is true, then the duty cycle of the PWM peripheral is set to the
value of the variable duty_cycle. The duty cycle must be a number
between 0 and 100; 0 corresponds to always low, and 100 corresponds to
always high. (If you are familiar with how the PWM peripheral works,
then notice that that means that LDmicro automatically scales the
duty cycle variable from percent to PWM clock periods.)
You can specify the target PWM frequency, in Hz. The frequency that
you specify might not be exactly achievable, depending on how it
divides into the microcontroller's clock frequency. LDmicro will
choose the closest achievable frequency; if the error is large then
it will warn you. Faster speeds may sacrifice resolution.
This instruction must be the rightmost instruction in its rung.
The ladder logic runtime consumes one timer to measure the cycle
time. That means that PWM is only available on microcontrollers
with at least two suitable timers. PWM uses pin CCP2 (not CCP1)
on PIC16 chips and OC2 (not OC1A) on AVRs.
> MAKE PERSISTENT saved_var
--{PERSIST}--
When the rung-in condition of this instruction is true, it causes the
specified integer variable to be automatically saved to EEPROM. That
means that its value will persist, even when the micro loses
power. There is no need to explicitly save the variable to EEPROM;
that will happen automatically, whenever the variable changes. The
variable is automatically loaded from EEPROM after power-on reset. If
a variable that changes frequently is made persistent, then the
EEPROM in your micro may wear out very quickly, because it is only
good for a limited (~100 000) number of writes. When the rung-in
condition is false, nothing happens. This instruction must be the
rightmost instruction in its rung.
> UART (SERIAL) RECEIVE var
--{UART RECV}--
LDmicro can generate code to use the UART built in to certain
microcontrollers. On AVRs with multiple UARTs only UART1 (not
UART0) is supported. Configure the baud rate using Settings -> MCU
Parameters. Certain baud rates may not be achievable with certain
crystal frequencies; LDmicro will warn you if this is the case.
If the input condition to this instruction is false, then nothing
happens. If the input condition is true then this instruction tries
to receive a single character from the UART. If no character is read
then the output condition is false. If a character is read then its
ASCII value is stored in `var', and the output condition is true
for a single PLC cycle.
> UART (SERIAL) SEND var
--{UART SEND}--
LDmicro can generate code to use the UARTs built in to certain
microcontrollers. On AVRS with multiple UARTs only UART1 (not
UART0) is supported. Configure the baud rate using Settings -> MCU
Parameters. Certain baud rates may not be achievable with certain
crystal frequencies; LDmicro will warn you if this is the case.
If the input condition to this instruction is false, then nothing
happens. If the input condition is true then this instruction writes
a single character to the UART. The ASCII value of the character to
send must previously have been stored in `var'. The output condition
of the rung is true if the UART is busy (currently transmitting a
character), and false otherwise.
Remember that characters take some time to transmit. Check the output
condition of this instruction to ensure that the first character has
been transmitted before trying to send a second character, or use
a timer to insert a delay between characters. You must only bring
the input condition true (try to send a character) when the output
condition is false (UART is not busy).
Investigate the formatted string instruction (next) before using this
instruction. The formatted string instruction is much easier to use,
and it is almost certainly capable of doing what you want.
> FORMATTED STRING OVER UART var
-{"Pressure: \3\r\n"}-
LDmicro can generate code to use the UARTs built in to certain
microcontrollers. On AVRS with multiple UARTs only UART1 (not
UART0) is supported. Configure the baud rate using Settings -> MCU
Parameters. Certain baud rates may not be achievable with certain
crystal frequencies; LDmicro will warn you if this is the case.
When the rung-in condition for this instruction goes from false to
true, it starts to send an entire string over the serial port. If
the string contains the special sequence `\3', then that sequence
will be replaced with the value of `var', which is automatically
converted into a string. The variable will be formatted to take
exactly 3 characters; for example, if `var' is equal to 35, then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -