There are a few different ways to create a pulse generator (or commonly known in the plc world as a BLINK timer). As a matter of fact many plc programming softwares have this function block built in to their function block libraries. But if they don't or you just want to make your own you can do something like this
VAR
ton1: TON;
ton2: TON;
StartPulse: BOOL;
startPulseTrig: R_TRIG;
LatchPulseInitial: BOOL;
PulseOutput: BOOL;
Timer1Done: BOOL;
Timer2Done: BOOL;
PulseWidth:TIME:=t#500ms;
END_VAR

If you would like to count the number of pulses and store this value to a variable you can use a simple CTU (counter up) block available in all plc languages.

Review of functionality
- The
StartPulse
variable can be anything you want that will start the counter. In my case I just used an internal bool
variable that I turned on. If you want this timer to start automatically when the plc starts then just initialize this variable to true
. Because StartPulse
only works on the rising edge of the signal the LatchPulseInitial
coil will only ever be set once.
- When the
LatchPulseInitial
variable goes true this will start ton1
a Timer On Delay (TON)
function block. This will delay the output of the block from turning on for the time of PT
(in my case I have 500ms).
- After 500ms has expired the
ton1
outputs will turn on. this will turn on the Timer1Done
variable and turn off the Timer2Done
and LatchPulseInitial
. Now that LatchPulseInitial
has been turned off it will never interfere with the program again since it can only be turned on by the rising edge of StartPulse
. Note: once the block has reached PT
the outputs will stay on until the input to the block is removed.

- Since
Timer1Done
is now on ton2
will start counting until PT
is reached. Once PT
is reached the outputs for that block will turn on. This will reset Timer1Done
and set Timer2Done
This will start ton1
again and thus starting the whole process over.
- For the
PulseOutput
, which is the actual pulse output you are looking for, I have this being set to true when Timer2Done
is true. This is because when this variable is true
it is the high state of the pulse generator. When Timer1Done
is true it is the low state of the pulse generator.
- When the
PulseOutput
goes true
it will trigger the input on the CTU
which will increment the count of the variable in CV (current value)
by 1.
If you are going to be using this logic in numerous places in your program or you plan on reusing it in the future I would make this into its own function block so you won't have to repeat this logic everytime you want to make this type of timer.