Constant Types and Representation Methods
1. Decimal Constants (K)
Representation: K + Number
Range:
·16-bit:K-32768 ~ K32767
·32-bit:K-2,147,483,648 ~ K2,147,483,647
Example:
K100 // Decimal 100
K–500 // Decimal -500
K12345 // Decimal 12345
2. Hexadecimal Constants (H)
Representation: H + Hexadecimal Number
Range:
·16-bit:H0000 ~ HFFFF
·32-bit:H00000000 ~ HFFFFFFFF
Example:
H64 // Hexadecimal 64, equal to Decimal 100
HFF // Hexadecimal FF, equal to Decimal 255
H1234 // Hexadecimal 1234, equal to Decimal 4660
3. Floating Point/Real Constants (E)
Representation: E + Floating Point Number
Range:
·Negative:-3.402823e+38 ~ -1.175495e-38
·Zero:0
·Positive:+1.175495e-38 ~ +3.402823e+38
Precision Note:
·Single precision floating point has a maximum of 7 decimal significant digits
·Excess will be automatically rounded
Example:
E3.14 // Floating point 3.14
E–100.5 // Floating point -100.5
E0.001 // Floating point 0.001
4. Character/String Constants
Representation: Characters or strings enclosed in single quotes
Example:
‘A’ // Single character A
‘Hello’ // String Hello
‘123’ // String 123 (not a number)
‘Temperature:25.5℃’ // Mixed Chinese and English string
5. Boolean Constants
Representation:
·TRUE or ON – True/On
·FALSE or OFF – False/Off
Example:
TRUE // Boolean true value
FALSE // Boolean false value
ON // Equivalent to TRUE
OFF // Equivalent to FALSE
Special Soft Element Constants
The manual also defines some special system constants, mainly used for specific function flags:
|
Constant Name |
Data Type |
Description |
|
M8000 |
BOOL |
Set to ON when the user program is running |
|
M8001 |
BOOL |
Inverted state of M8000 |
|
M8002 |
BOOL |
ON for the first cycle when the user program starts running |
|
M8003 |
BOOL |
Inverted state of M8002 |
|
M8011 |
BOOL |
Oscillating clock with a 10ms cycle |
|
M8012 |
BOOL |
Oscillating clock with a 100ms cycle |
|
M8013 |
BOOL |
Oscillating clock with a 1s cycle |
|
M8014 |
BOOL |
Oscillating clock with a 1min cycle |
Common M8000 Series Special Soft Elements
Constant Usage Examples
Using Constants in Ladder Diagram (LD)
// Numeric constant usage
[ MOV K100 D0 ] // Transfer Decimal 100 to D0
[ MOV H64 D1 ] // Transfer Hexadecimal 64 (Decimal 100) to D1
[ DEMOV E3.14 D2 ] // Transfer Floating point 3.14 to D2, D3
// Clock pulse application
M8013 // 1 second clock pulse
|—[ MOV K1 Y0 ] // Trigger Y0 output once per second
// Boolean constant usage
M8000 // Always ON during runtime
|—[ MOV K500 D10 ] // Continuous transfer during runtime
Using Constants in Structured Text (ST)
// Assigning numeric constants
VAR
speedSet : INT;
temperature : REAL;
statusFlag : BOOL;
message:STRING;
END_VAR // Assigning various constants
speedSet := K1500; // Set speed to 1500
temperature := E25.5; // Set temperature to 25.5℃
statusFlag :=TRUE; // Set status flag to true
message:=‘Device is running normally’; // Set status message
// Using constants in condition A judgment
IF speedSet > K1000 THEN // High-speed processing logic
END_IF; // Using clock constants for timing operations
IF M8013 THEN
// Operations executed once per second
END_IF;
Using Constants in Function Block Parameters
// Using constants as parameters when calling function blocks
PID_Control(
SetPoint := E75.0, // Setpoint 75.0
Kp := E2.5, // Proportional coefficient 2.5
Ki := E0.1, // Integral coefficient 0.1
Kd := E1.0 // Derivative coefficient 1.0
);
Important Notes
1. Floating Point Precision Limit // Note: Floating point has a maximum of 7 significant digits
E1234567.89 // Actual storage may be 1234567.9 (rounded)
2. Data Type Matching
// Correct usage
MOV K100 D0 // Integer to Integer
DEMOV E100.5 D2 // Floating point to Floating point
// Avoid mixing usage
MOV E100.5 D0 // May produce unexpected results
3. String Length Limit
·String variables default to a length of 128 bytes
·Maximum support for 256 bytes
·The last byte is defaulted as a terminator
4. Read-Only Nature of Special Soft Elements
Most M8000 series special soft elements are read-only (R), and cannot be assigned in the program:
M8000 :=TRUE; // Error! M8000 is read-only
Practical Application Scenarios
1. Device Parameter Settings
// Fixed parameters for the device
CONST
MAX_SPEED := K3000; // Maximum speed
MIN_TEMP := E–10.0; // Minimum temperature
ALARM_DELAY := K5000; // Alarm delay (ms)
END_CONST
2. Status Monitoring
// Using clock constants for status monitoring
IF M8012 THEN // Execute every 100ms
IF currentSpeed > MAX_SPEED THEN
alarmFlag :=TRUE;
END_IF;
END_IF;
3. Debug Information Output
// Using string constants to output debug information
debugMessage :=‘Speed:’+ INT_TO_STRING(currentSpeed)+‘ RPM’;
By properly using various constants, PLC programs can be made clearer, more readable, and easier to maintain.