Let’s create a fast counter that will count pulses coming to fast input 1. As you might remember, if you create a counter in the regular PLC cycle, you would hit 1ms limit rather soon. That is you would fail to catch pulses that last less than 1ms (that is typical PLC cycle length). Of course you can configure the fast input in “counter” mode, however let’s create a PRU program for fast counter so you can learn PRU programming.
PRU cycle can be as low as 1 µs, so it can catch very short pulses.
If you have Hardella IDE opened, you can either open
code sample in the IDE, or you can create a new project with this code (
Let’s set PRU cycle length to be 1 µs.
That means PRU program will check inputs once every 1 µs. That means you can have a reliable counter at speeds up to 250 kHz.
The counter program is trivial, however let’s note the following:
R_TRIGcall, however there’s no variable declared for the trigger. How’s that possible? That’s easy: Hardella can use implicit functional blocks. It is very convenient for one-time blocks like
countervariable is placed in PRU memory, however a data exchange is configured. That is how the results of the fast counter will be passed to the main CoDeSys program.