Author Topic: LCD update data instantly  (Read 16753 times)

Tito

  • Newbie
  • Posts: 48
  • Think
    • View Profile
LCD update data instantly
« on: August 20, 2012, 10:07:19 AM »
Hi, I am still trying out to get grip on the board.

I am trying out with 8 switch control with direct connection with 8 output and 2 analog input.

What i am trying out is when ever the switch is on or off should update my LCD, let saying Door 1 is open or Door 1 is closed. This is triggered when ever event happens only. Same goes to AI no 1, when data changes should update in row 2 and AI no2 goes to row 3.

Thanks for your support in advance. :)

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:LCD update data instantly
« Reply #1 on: August 20, 2012, 11:30:53 AM »
Your question 1 is fundamental ladder logic. Door1 is connected to an input and trigger an output coil whenever it is ON (or OFF).

For AI, you will need to monitor it periodically. You can use a clock pulse (e.g. Clk:1.0s) to periodically trigger a {dCusF} and then check the value of analog input and then turn ON/OFF the output.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Tito

  • Newbie
  • Posts: 48
  • Think
    • View Profile
Re:LCD update data instantly
« Reply #2 on: August 20, 2012, 10:22:25 PM »
Hi, Manage to sort out as per your advice,

i did use cust function with Clk 1.0s then

if input[1]=1 then
   setlcd 2,1,"Door 1 is open"
else
   setlcd 2,2,"door 1 is close"
endif

if input[1] = 2 then
 setlcd 3,1,"Door 2 is open"
else
   setlcd 3,2,"door 2 is close"
endif

I do use for loop to complete my circle.

Cheers

Tito

  • Newbie
  • Posts: 48
  • Think
    • View Profile
Re:LCD update data instantly
« Reply #3 on: August 23, 2012, 09:00:49 PM »
Hi, is there anything wrong on my coding, when i disable line start with K$ my simulaiton run otherwise hang. I do trigger this functions every 5 sec under differential UP Custom Function.


if input[1] = 1  then DM[1]=1 : else DM[1]=0 : endif   'input #1
if input[1] = 2  then DM[2]=1 : else DM[2]=0 : endif   'input #2
if input[1] = 4  then DM[3]=1 : else DM[3]=0 : endif   'input #3
if input[1] = 8  then DM[4]=1 : else DM[4]=0 : endif   'input #4
if input[1] = 16 then DM[5]=1 : else DM[5]=0 : endif  'input #5

K$= STR$(DM[1]) +","+ STR$(DM[2])+","+STR$(DM[3])+","+STR$(DM[4])+","+STR$(DM[5])
« Last Edit: August 23, 2012, 09:02:49 PM by Tito »

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:LCD update data instantly
« Reply #4 on: August 24, 2012, 08:21:13 AM »
Tito,

Run your code in the simulator.

Look what happens if more than one input is active.  You should observe that K$ will be something like this, "0,0,0,0,0".

Is this what you want?

Gary d

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re:LCD update data instantly
« Reply #5 on: August 24, 2012, 08:30:08 AM »
I don't think your program "hang" (whatever you mean)?

Your code will not work properly if more than one input is ON at the same time since you are only testing the pattern when only one input is ON at any one time.

If you want DM[1] to DM[5] to be a "1" when the corresponding input #1 to #5 is ON, do the following:

FOR I = 0 to 4
  IF TESTBIT(INPUT[1],I)
     DM[I+1] = 1
  ELSE
     DM[I+1] = 0
  ENDIF
NEXT


K$= STR$(DM[1]) +","+ STR$(DM[2])+","+STR$(DM[3])+","+STR$(DM[4])+","+STR$(DM[5])

K$ however will behave exactly as what Gary mentioned.
Email: support@triplc.com
Tel: 1-877-TRI-PLCS

Tito

  • Newbie
  • Posts: 48
  • Think
    • View Profile
Re:LCD update data instantly
« Reply #6 on: August 24, 2012, 06:25:55 PM »
Cheers, I understood the function clearly that

""Your code will not work properly if more than one input is ON at the same time since you are only testing the pattern when only one input is ON at any one time.""

Refer to the statement, if i am CALLing functions on for every sensor triggered, how can i put in queuing system?  

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:LCD update data instantly
« Reply #7 on: August 24, 2012, 07:46:18 PM »
Tito,

I don't know what you mean by " if i am CALLing functions on for every sensor triggered, how can i put in queuing system".

Let's say that you only want to update K$ when any input has changed state.  This can be done in ladder logic, but I don't think you are ready for this.  It can also be done using a custom function.

I've added an IF statement to the previous code snippet.  K$ will only be updated if the state of the INPUT[1] has changed from the last time that this function was called.


' Custom funtion to check if inputs have changed from the last call.
'
' If nothing has changed, then nothing will be done
'
IF ((DM[17] ^ INPUT[1]) & &H1F) = 0
   RETURN               ' Inputs have not changed, bail out.
ENDIF

' Inputs have changed. Save new value and build string in K$

DM[17] = INPUT[1]      ' Store Present Version of INPUT[1] state for future
FOR I = 0 to 4
  IF TESTBIT(INPUT[1],I)
    DM[I+1] = 1
  ELSE
    DM[I+1] = 0
  ENDIF
NEXT

K$= STR$(DM[1]) +","+ STR$(DM[2])+","+STR$(DM[3])+","+STR$(DM[4])+","+STR$(DM[5])
 


Am I getting any closer in guessing what you need?

Gary D

Tito

  • Newbie
  • Posts: 48
  • Think
    • View Profile
Re:LCD update data instantly
« Reply #8 on: August 24, 2012, 08:36:35 PM »
Hi Gary,

Thanks for reply, you get what i meant. But i need to ask something on your coding. On this part :-

IF ((DM[17] ^ INPUT[1]) & &H1F) = 0
   RETURN               ' Inputs have not changed, bail out.
ENDIF

where DM[17] ^ INPUT[1] where EOR to get change from 0 to 1 or 1 to 0.
that's mean initially DM[17] is 0 and EOR with INPUT[1] to capture the changes.

Question is  &H1F = 31 bit. from 0 to 31 which equal to 32 bit for integer. why you & (AND) with &H1F? i didn't get this part. please do elaborate this section.
Thanks in advance.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:LCD update data instantly
« Reply #9 on: August 24, 2012, 09:37:12 PM »
Tito,

Excellent question.  

In your examples you were only using 5 INPUTS.

INPUT[1]  represents the first 16 input bits.  You seemed to be interested in only the first 5 inputs.

The & &h1f is a "mask" that isolates only the first 5 inputs. If you omit the & &h1f term then the all 16 inputs would be evaluated.

I use the &h notation to remind me that this is but mask.  I don't do bits well in decimal.

Gary d

Tito

  • Newbie
  • Posts: 48
  • Think
    • View Profile
Re:LCD update data instantly
« Reply #10 on: August 24, 2012, 10:01:08 PM »
 ;D, i got it. Thank you very much.

Back to my queuing question, what i meant is :-
When ever changes happen in my input[1] i need to send sms.
Let say every second changes happens (talk on worst conditons), on changes i need to send SMS. Every SMS approximates will take 10 sec to go out. I need to que up to send one by one.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:LCD update data instantly
« Reply #11 on: August 25, 2012, 09:25:37 AM »
Tito,

You have a problem. If it takes 10 seconds to send SMS messages for an event and an event happens every second, your system will fail.  

With that set of rules, an infinite queue length is not long enough. Think "tortoise and hare", with the tortoise being SMS  transit time and the hare being the an "event".  Unless the hare stops running, the tortoise never catches up.

The moral of this story is you need to be able to calculate how long your queue needs to be for your worst case bursts of event.

Questions that you need to answer:

1. What is the maximum frequency that an input can change state.  The answer to this determines how often you will have to "poll" for changes.

2. Are there periods of time while no inputs change?  

3. Do the input changes come in bursts of changes followed by long periods of inactivity?

4.  What happens if the queue is full and another event occurs?  Can your system tolerate periodic loss of data?


It is possible to build a queue in DM[] to store a copy of INPUT[1] every time an input chages.  Do you need record the time/date that for each event?

I can show you a "simple" queue or more correctly a FIFO (first in first out) mechanism coded in TBASIC in another post.

Gary d

Tito

  • Newbie
  • Posts: 48
  • Think
    • View Profile
Re:LCD update data instantly
« Reply #12 on: August 25, 2012, 10:02:34 AM »
 :D Hi,

My current situation is, i have 5 sensor to determine either "ON or OFF" situations and 1 Analog input 4-20 mA to determine height of water level.

Refer to my 5 sensor ( on relay with state "ON" / "OFF")
if the sensor trigger and send sms to indicate "SYSTEM SENSOR n  NOT STABLE"  and after 5 min and still "ON" send sms continuously with interval of 5 min. If the changes happen from "ON" to "OFF" send SMS once to indicate "SYSTEM SENSOR n STABLE"  and wait for any changes happen from 0 to 1 again.

Refer to my analog input, display my reading in LCD every 1 sec or depends. if my water level is increasing above 5 meter then check for every 30 sec if the water level is higher or equal to 5 meter then sending sms. If below 5 m send sms once and keep on monitoring.

I am just thinking on worst conditons. Summary of time interval

For sensor if 5 sensor trigger at once:
then need 50 secs to send SMS and next after 5 mins.

For analog input if trigger above 5 meter
then  very 30 sec's.

FIFO method would be good for this applications.

Thanks  

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:LCD update data instantly
« Reply #13 on: August 25, 2012, 09:25:55 PM »
Tito,

I have a ".pc6" program that demonstrate a simple FIFO mechanism that you may need to deal with the issue of input changes that may happen at a rate that exceeds the rate that SMS messages can be generated and sent.

Please send me your email address (you can find mine in my user profile) and I will send you the ".pc6" file.

The FIFO uses DM[] memory to hold the data.  Data is enqueued (written into DM[]) based on the index, ENQ_Index.  Data is dequeued (read from DM[] based on the index DEQ_Index.

Once data is placed into DM[] it is not moved.  The indices are adjusted.  It is faster and simpler to not move the data.

Please note that this code uses the #Define mechanism to make the code a bit more readable.  These are the items that use the #Define mechanism:

#   #Define Name   Value
1   ENQ_Index   DM[1]  
2   DEQ_Index   DM[2]
3   FIFO_Cnt           DM[3]
4   FIFO_Size           4
5   FIFO_StartIndex   11


ENQ_Index determines where data is stored
DEQ_Index determines where data is read
FIFO_Cnt is the quantity of data stored in the FIFO
FIFO_Size is the size of the FIFO.  In this example the FIFO can
hold 4 items.  Change this value to change the size of the FIFO and rerun the code.
FIFO_StartIndex is the index into DM[] for start of the actual data storage.  In this example I use DM[11.15] to store the 4 data items.


There are 3 custom functions involved in this example:

1. InitFIFO - this must be called once at the start of the PLC program to make the FIFO appear to be empty.

' InitFIFO - Initialize FIFO indices and flags
'
ENQ_Index   = 0
DEQ_Index    = 0
FIFO_Cnt    = 0
SetIO FIFO_EMPTY
ClrIO FIFO_FULL

2. ENQ_A - this function writes a value to the FIFO:

' ENQ_A - function to enqueue the least significant 16 bits of A

IF TestIO(FIFO_FULL)
   ' the FIFO is full and cannot accept data
   ' this is and error condition
   '
   RETURN
ENDIF


DM[FIFO_StartIndex + ENQ_Index] = A      ' Add a value to the FIFO

' Update FIFO index, counter and flags
'
ENQ_Index = (ENQ_Index + 1) MOD FIFO_Size
FIFO_Cnt = FIFO_Cnt + 1

ClrIO FIFO_EMPTY         ' FIFO cannot be empty

IF FIFO_Cnt = FIFO_Size
   SetIO FIFO_FULL         ' the FIFO is full and cannot accept more data
ENDIF


3. DEQ_A function to read a value from the FIFO

' DEQ_A - function to dequeue a 16 bit value from the FIFO and load it into A
'

IF TestIO(FIFO_Empty)
   ' the FIFO is empty and there are no values to remove from the FIFO
   ' this is and error condition
   '
   RETURN
ENDIF


A = DM[FIFO_StartIndex + DEQ_Index]      ' remove one value from the FIFO

' Update index, FIFO count and flags
'
DEQ_Index = (DEQ_Index + 1) MOD FIFO_Size
FIFO_Cnt = FIFO_Cnt - 1

ClrIO FIFO_FULL         ' FIFO cannot be full

IF FIFO_Cnt = 0
   SetIO FIFO_EMPTY   ' the FIFO is empty and there is no more data to remove
ENDIF



I know that you may me overwhelmed by the detail of this code, but this is the simplest way that I know to get the job done with TBASIC.  The sample code sets, clears and tests the following RELAYS: FIFO_FULL and FIFO_EMPTY.  This was done to allow ladder logic to "Know" the current state of the FIFO.

Best regards,

Gary D.

Tito

  • Newbie
  • Posts: 48
  • Think
    • View Profile
Re:LCD update data instantly
« Reply #14 on: August 25, 2012, 10:06:31 PM »
Hi, i did drop an email to you.
thank you.