OpenPLC-with-LDmicro/C3/Compare-Instructions/English
|
|
Slide 1: | Welcome to the spoken tutorial on Compare instructions. |
Slide 2: Learning Objectives
|
In this tutorial, we will learn about working of Compare instructions |
Slide 3: System Requirements
|
To record this tutorial I am using:
|
Slide 4: Pre-requisites
|
|
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 6a: Compare instructions
Six types of compare instructions
|
In LDmicro we have six types of compare instructions.
They are
|
Slide 6b: Compare instructions
|
These instructions can be used to compare a variable to another variable.
Also when we want to compare a variable to a 16-bit signed constant. Working of these compare instructions will be similar. But the functioning will be according to their names. |
In this tutorial we’ll learn about compare for equals instruction in detail. | |
Open LDmicro | Let us open LDmicro. |
We will create a virtual circular counter using a compare for equals instruction. | |
Click Instructions -> Insert Contact >> Place the cursor to the right of the contact >> Click instructions -> Insert OSR >> Place the cursor to the right of OSR >> Click Instructions -> Arithmetic instructions -> Insert ADD | Insert a Contact.
To the right of it insert an OSR To the right of OSR insert an ADD instruction as shown here. |
Double-click on the contact >> Type switch in the name box >> Click OK button | Rename the Contact as ‘switch’. |
Double click on ADD | Double-click on the ADD instruction. |
Type in the destination and ‘is set as := ‘ columns ‘vcounter’ | Set the destination and first operand to ‘vcounter’ meaning virtual counter. |
Click the OK button | Second operand is by default 1.
Don’t change that. Click the OK button |
Click Edit -> Insert Rung after | Insert a rung below as shown. |
We will insert Compare for equals and Move instruction in the new rung. | |
Place the cursor in the new rung >> Click Instructions -> Comparison operators -> Insert EQU >> place the cursor to the right of the contact >> Click Instructions -> Insert MOV | Place the cursor in the new rung.
Click on Instructions and then Comparison operators. Select Insert EQU. Next place the cursor to the right of the compare for equals instruction. Click on Instructions and then select Insert MOV. |
Double click on Compare for equals >> Set the first column to vcounter >> Set the second column to 0 >> Click the OK button | Double-click on Compare for equals.
Compare the ‘vcounter’ variable with 4. That is, set the first column to ‘vcounter’ and the second one to 4. Click on the OK button. |
Double-click on the MOV instruction >> Type vcounter in the destination column >> Type 0 in the source column >> Click the OK button | Double-click on the Move instruction.
Set the Destination variable as ‘vcounter’ and Source variable as 0. |
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 and counter in the I/O list | Initially, the state of Xswitch and vcounter are 0. |
Double-click on ‘Xswitch’ | Change the state of Xswitch to 1. |
Highlight the state of vcounter | We can observe the state of vcounter changes to 1.
The state of vcounter increases by 1 every time there is a high to low transition in Xswitch. |
Double-click on ‘Xswitch’ for 5 more times
Highlight the state of vcounter. |
Keep changing the state of Xswitch from 0 to 1 and back to 0.
We can observe vcounter takes values 0, 1, 2, 3 and then goes back to 0. That is like a circular counter with count 3. |
For visualization purpose we will glow a LED whenever the vcounter value is 0. | |
Click on Simulate >> Click Halt Simulation >> Click Simulation Mode | Now, turn OFF the simulation mode.
For that, click Simulate and then on Halt Simulation. Then click Simulate and Simulation Mode. |
Visual cue
Click Edit -> Insert rung after |
Insert a rung below the second rung. |
Place the cursor in the new rung >> Click on Instructions -> Comparison Operators -> Insert EQU >> Place the cursor to the right of EQU >> Click on Instructions -> Insert Coil | Place a Compare for equals and a Coil in the new rung. |
Double-click on EQU instruction >> Type vcounter in the first box >> Type 0 in the second box >> Click OK button | Double-click on Compare for equals.
Compare the ‘vcounter’ variable with 0. That is, set the first column to ‘vcounter’ and the second one to 0. Click on the OK button. |
Double-click on Ynew >> Type LED in the name box >> Select SET-only option >> Click OK button | Rename the Coil as LED. |
Highlight the third rung | Thus this will energise the YLED whenever the vcounter value is 0. |
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 ‘compareequals.hex’ >> Click on Save.
Click OK button |
Compile the logic as ‘compareequals.hex’. |
Click on File >> Click on Save >> Go to Desktop/LDmicro folder >> Rename it as ‘compareequals.ld’ >> Click on Save | Then save the ladder diagram as ‘compareequals.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:compareequals.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. |
compareequals.png | Let us see the connection details now. |
compareequals.png | Connect GND pin of the red LED of Traffic Light module to GND of the Mainboard.
Then connect +5V pin of the red LED to PA0 pin of the Mainboard. |
compareequals.png | 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. |
After making all the connections properly, turn ON the power supply. | |
We can observe that the LED will be glowing initially. | |
It will turn OFF once the NO1 is pressed. | |
The red LED will glow again after pressing NO1 for 3 more times. | |
Thus LED glow indicates that the count value of the counter is 0. | |
Turn OFF the power supply. | |
Try out the remaining Compare instructions on your own. | |
This brings us to the end of this tutorial.
Let us summarize. | |
Slide 7: Summary
In this tutorial, we learnt about
|
In this tutorial, we learnt about Compare instructions |
Slide 8: Assignment
Try to
Hint: Use ‘greater than’ compare instruction to turn ON the LED. |
As an assignment, try to
Hint: Use ‘greater than’ compare instruction to turn ON the LED. |
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:
For more details, please write to us |
Slide 11:
Forum for specific questions: |
|
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. |