OpenPLC-with-LDmicro/C3/CTU-Counter/English

From Script | Spoken-Tutorial
Revision as of 16:13, 14 July 2021 by Nirmala Venkat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Slide 1: Welcome to the spoken tutorial on CTU Counter
Slide 2: Learning Objectives
  • Count up (CTU) counter
In this tutorial, we’ll learn about the working of Count up counter
Slide 3: System Requirements
  • Ubuntu 18.04 OS
  • LDmicro
  • OpenPLC Mainboard
  • 24V, 2A SMPS
  • USBasp programmer
  • Traffic Light module
  • Switchboard module
To record this tutorial I am using:
  • Ubuntu Linux 18.04 operating system
  • LDmicro
  • OpenPLC Mainboard
  • 24V, 2A SMPS
  • USBasp programmer
  • Traffic Light module and
  • Switchboard module
Slide 4: Pre-requisites
  • To follow this tutorial, you should know the working of
  1. Contact
  2. Coil and
  3. RESET instructions.
  • If not, please refer to the relevant tutorials in this series on this website.
Slide 5: Prerequisites - Hardware setup

hardware-prerequisite.jpg

Connect SMPS and USBasp to the Mainboard as shown in the picture.

Keep these connections throughout this tutorial.

Slide 5: Counters

3 types of counters

  1. Count up (CTU)
  2. Count down(CTD)
  3. Count circular(CTC)


In LDmicro, we have 3 types of counters.

Count up which is represented as CTU.

Count down which is represented as CTD and

Count circular which is represented as CTC.

Here first we will learn about CTU counter.

In this tutorial, we will create a logic to turn on an LED.

The condition is when the switch is pressed for more than or equal to 3 times.

Open the LDmicro from the launcher bar Let us open LDmicro.
Click ‘Instructions -> Insert Contact’ >>

Place the cursor to the right of the contact >>

Click ‘Instructions -> Insert Coil’

Insert a Contact and a Coil as shown.
Double-click on Xnew >> Type switch in name box >> Click OK button Rename the Contact as ‘switch’.
Double click on the Ynew >> Rename it as ‘LED’ >> Click OK button Then rename the Coil as ‘LED’..
Place the cursor to the right of the contact >> Click Instructions -> Counters -> Insert CTU Place the cursor to the right of Xswitch.

Now insert a CTU from Instructions as shown here.

Double-click on Cnew Double-click on the counter.
In the name column, type count We can observe there are two columns.

First one is for the name of the counter.

Type ‘count’ in it.

In the True if >= column type 3

Click the OK button

Second column is the condition column ‘True if >=’.

Here, we have to enter the desired count value.

We will enter 3 as the count value.

Click on the OK button.

Note that the name is prefixed by C by default.

We will now check the working of this logic.
Click Simulate -> Simulation mode >>

Click Simulate -> Real-time simulation

Let us turn on the simulation mode.

For that, click Simulate and then on Simulation mode.

Next, start real-time simulation as shown.

Highlight the state of Xswitch, YLED and Ccount in the I/O list We can observe initially the states of Xswitch, YLED and Ccount are 0.
Double click on Xswitch in the I/O list Double click on the Xswitch to change its state to 1.
Highlight the state of Ccount and YLED We can observe the state of the counter Ccount changes to 1.

But, the state of YLED is still 0.

Double click on Xswitch Now, change the state of Xswitch back to 0.
Highlight the state of Ccount We can observe the state of Ccount is still 1.
That is the counter value updates only with the rising edge of its input.
Double click on Xswitch for 3 times Change the state of Xswitch from 0 to 1 for a couple more times.
Highlight the state of Ccount After 3 low to high transitions of Xswitch, the state of Ccount reaches 3.
Highlight the state of YLED We can now observe the state of YLED changes to 1.
Double click on Xswitch for 2 times Change the state of Xswitch to 0.

Again change it back to 1.

Highlight the state of YLED The state of Ccount changes to 4.

We can observe that the state of YLED is still 1.

Once the counter reaches desired count value, its output will always be 1.

This is because of the condition >= 3 in the CTU instruction.

Click Simulate -> Halt Simulation >> Click Simulate -> Simulation Mode Now, turn off the simulation mode.

For that, click Simulate and then on Halt Simulation.

Then click Simulate and Simulation Mode.

Now let us compile the logic.

The detailed steps on how to compile and save the logic are explained in the earlier tutorials.

Click on Settings >> Click on Microcontroller >> Select AVR ATmega16 40-PDIP

Click on Settings >> Click on MCU parameters >> Change Crystal frequency to 16

Click on Settings and select the microcontroller AVR ATmega16 40-PDIP.

Adjust its parameters as shown here.

Double-click on Xswitch in I/O list >> Select PC0 >> Click OK button

Double-click on YLED in I/O list >> Select PA0 >> Click OK button

Assign pin PC0 to Xswitch and PA0 to YLED.
Click on Compile >> Click on Compile >> Go to Desktop/LDmicro folder >> Rename it as ‘ctu.hex’ >> Click on Save.

Click OK button

Compile the logic as ctu.hex.
Click on File >> Click on Save >> Go to Desktop/LDmicro folder >> Rename it as ‘ctu.ld’ >> Click on Save Then save the ladder diagram as ctu.ld.
We will now see the working of this logic on hardware.
Connect Mainboard to PC using USBasp

laptop-usbasp.jpg

Connect the Mainboard to your laptop using USBasp.

Turn on the power supply.

Open terminal >> Type cd Desktop/LDmicro >> Press ENTER >> type ‘avrdude -c usbasp -p m16 -U flash:w:ctu.hex’ >> Press ENTER Open the Terminal by pressing CTRL+ALT+T keys simultaneously.

Go to the folder where you have saved the hex file.

Type the command as shown to upload the hex file to the Mainboard.

Remove the USBasp connection from the laptop. Turn off the power supply.

Remove the USBasp connection from the laptop.

This will prevent any hardware damage.

ctu.png Let us see the connection details now.
ctu.png to show all the connections as per the narration Connect GND pin of the red LED of Traffic Light module to GND of the Mainboard.

Then connect +5V pin of red LED to PA0 pin of the Mainboard.

ctu.png to show all the connections as per the narration Connect GND and 5V of switchboard to GND and 5V of the Mainboard respectively.

Then connect NO1 of the Switchboard to PC0 of the Mainboard.

Make the connections as shown in the picture.

Turn on the power After making all the connections properly, turn on the power supply.
Red LED doesn't glow Initially the red LED will not glow.
Press switch NO1 thrice It will glow only when the switch NO1 is pressed for at least three times.

So, press the switch NO1 for 3 times.

LED glows We can see the LED will be glowing even after the NO1 is released.
Turn off the power. Turn off the power supply.
Slide 6: Resetting CTU

CTU counters can be reset using RES instruction

In order to turn off the LED we need to reset the counter.

It can be done by using a RESET instruction as learnt in the RTO tutorial.

This brings us to the end of this tutorial.

Let us summarize.

Slide 7: Summary
  • Count up (CTU) counter
In this tutorial we learnt about working of Count up counter.
Slide 8: Assignment
  • Draw a ladder diagram to turn on an LED when the switch is pressed for >= 5 times.
  • Include logic to reset the LED.

Hint: Use another Contact and RES instruction

As an assignment do the following:
  1. Draw a ladder diagram to turn on an LED. The condition is when the switch is pressed for more than or equal to 5 times.
  2. Include logic to reset the LED.

Hint: Use another Contact and RES instruction.

Slide 9:

About Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it

Slide 10:

Spoken Tutorial workshops

The Spoken Tutorial Project team:
  • conducts workshops using spoken tutorials and
  • gives certificates on passing online tests.

For more details, please write to us

Slide 11:

Forum for specific questions:

Please post your timed queries in this forum
Slide 12:

Forum for specific questions:

Do you have any general / technical questions on OpenPLC?

Please visit the FOSSEE forum and post your question.

Slide 13:

Acknowledgement

Spoken Tutorial Project is funded by MHRD, Government of India.
Slide 14:

Thank you slide

This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.

And this is Harsha Priyanka from FOSSEE team, signing off.

Thanks for watching.

Contributors and Content Editors

Nirmala Venkat