Program Pulses Width Measurer

Top 



The source code below demonstrates the ability of the PLC acts the equipment measurer of electric and electronic quantities.



plc
Pulses_Width_Measurer // Measures the widths of the pulses entered in the input I0.0 (whether the turned on (in 1) or the turned off (in 0) parts)

var
   I0.0    Pulse_Input,
T0
    Edge_Time,
I1.0
    Enab_Interrup,
Q0.0
    Falling,
VM0
    Anterior_Time,
AQW0
Input_in_0_Width,
AQW1
Input_in_1_Width;

network
1 // Initializes the variable Falling, enables the treatment of interrupts and attachs the events to be treated to the handles.

if
(immediate Enab_Interrup) {

if
(not immediate Início) {
enable_interrupt
;
attach
Rising_Falling_I0.0 to_the interrupt rising_edge_I0.0;
attach
Rising_Falling_I0.0 to_the interrupt falling_edge_I0.0; // Note that two events can be treated by the same interrupt handle.

turn_on
Start;

if
(immediate Pulse_Input) {
set
immediate Falling; // Initializes the variable Falling according to value of I0.0 in the beginning of the operation, to, independent of I0.0 to be on or off in this moment, the measurements of the widths of its on and off pulses haven't got interchanged.
}
}

}
else
{
turn_off
immediate Start;
}

network
2 // Handling of the I0.0 rising and falling events, the beginning of counting time at this point remains in high or low level

interrupt_handle
Rising_Falling_I0.0 ( )

if
(Enab_Interrup) {
Anterior_Time
:= Edge_Time;
}

when
(Enab_Interrup) temporize Edge_Time 1 h; // The time 1 h is arbitrary. It's enough that it is the major time than the maximum width to be measured.

if
(not immediate Falling){
Input_in_0_Width
:= Edge_Time - Anterior_Time;
set
immediate Falling;
}
else
{
Input_in_1_Width
:= Edge_Time - Anterior_Time;
reset
immediate Falling;
}

end_interrupt_handle


end




As result of the compilation of the
Pulses_Width_Measurer program above, the SimuPLC 4.1.0 has generated, exactly, the following code, in Instruction List - IL:



// PLC    Pulses_Width_Measurer

// Measures the widths of the pulses entered in the input I0.0 (whether the turned on (in 1) or the turned off (in 0) parts)

//
=VAR   I0.0   Pulse_Input
//
=VAR   T0   Edge_Time
//
=VAR   I1.0   Enab_Interrup
//
=VAR   Q0.0   Falling
//
=VAR   VM0   Anterior_Time
//
=VAR   AQW0   Input_in_0_Width
//
=VAR   AQW1   Input_in_1_Width

NETWORK    1 // Initializes the variable Falling, enables the treatment of interrupts and attachs the events to be treated to the handles.

LDI   I1.0

LDNI    Início
ALD
ENI
ATCH   Rising_Falling_I0.0,   0
ATCH   Rising_Falling_I0.0,   1
// Note that two events can be treated by the same interrupt handle.
S   Start,   1

LDI   I0.0
ALD
SI    Q0.0,   1
// Initializes the variable Falling according to value of I0.0 in the beginning of the operation, to, independent of I0.0 to be on or off in this moment, the measurements of the widths of its on and off pulses haven't got interchanged.

LDI   I1.0
NOT
RI    Start,   1

NETWORK    2 // Handling of the I0.0 rising and falling events, the beginning of counting time at this point remains in high or low level

INT   Rising_Falling_I0.0

LD   I1.0
MOVD   T0,   VM0

LD   I1.0
TON   T0,   3600000
// The time 1 h is arbitrary. It's enough that it is the major time than the maximum width to be measured.

LDNI    Q0.0
MOVD   T0,   Ve1
-D   VM0,   Ve1
MOVD   Ve1,   AQW0
SI    Q0.0,   1
NOT
MOVD   T0,   Ve1
-D   VM0,   Ve1
MOVD   Ve1,   AQW1
RI    Q0.0,   1