Maker Pro
Maker Pro

help to optimize code!!

Hello,

I have to interface PIC micro controller with PLC over modbus.

I have to read 120 number of holding registers of PLC and according to values received the port pin of micro controller is set or cleared.

I have used 15 74HCT574 latch IC and all are connected to one port of micro controller.

But in programming i am bit confused.

I am using switch case. But as I mentioned I have got 120 process values that I need to compare, it increases the size of code.

Please suggest any alternative to reduce the code size.

thanks,
paddy
 
If you have to compare to 120 values you have to compare to 120 values...

But, if the values are known (and have a pattern or sequence) you might be able to group or narrow down the comparing routine vs a long linear brute force compare...

For example say your data is a binary value 4 digits... You can check the first number if that is zero only compare it to other values that start with zero, if it's one only compare it to values that start with one... This will in theory half the compare loop... Or you can go further, if it starts with 00 jump to those compares, 01 jump to those, 10 jump to those, and 11 jump to those... Now you have quartered the compare loop...

Of course this all depends upon the data you are comparing patterns might or might not help reduce compare routines, and at the end of the day if you need more code space get more code space, 120 compares is hardly that extreme...
 

Harald Kapp

Moderator
Moderator
I have used 15 74HCT574 latch IC and all are connected to one port of micro controller
How are these registers connected? I assume the data bits are connected to one port (bits 0...7). But you will need a way of addressing the registers individually. Probably a second port and an address decoder (e.g. 2*74HCT138).

In addition to CC's response:
I have got 120 process values that I need to compare
Compare in what way? Do you have to compare each register value against a reference value? If so, are the reference values different for each register? Or do you have to compare the register values against each other? Or each register against the previous/next one? Can the reference values for comparison be generated on the fly by an an algorithm instead of storing each value?
Some more information could help us understanding the problem.
 
The values which I am getting are the process values and that are random in between 0-100.

I need to compare it with two set points say 20 & 40 and according to result i have to set/clear port pin to turn on/off LED indication
 
How are these registers connected? I assume the data bits are connected to one port (bits 0...7). But you will need a way of addressing the registers individually. Probably a second port and an address decoder (e.g. 2*74HCT138).



In addition to CC's response:

Compare in what way? Do you have to compare each register value against a reference value? If so, are the reference values different for each register? Or do you have to compare the register values against each other? Or each register against the previous/next one? Can the reference values for comparison be generated on the fly by an an algorithm instead of storing each value?
Some more information could help us understanding the problem.

I have used one port as a data port which is common to all 15 latch IC's and another 15 port pins are used as a clock to enable the latch.

I have three set points say 20,40 and >100 and i have to compare group of 40 registers to these set points.
 
Last edited:

Harald Kapp

Moderator
Moderator
I am using switch case.
You don't mean you have a switch statement with 120 cases, do you?

Put the comparison into a loop, better two nested loops. Without knowing more about the problem at hand, I can offer this pseudo-code. You'll have to translate it to your specific requirements (hardware connections, process values):
Code:
main_loop
   process_register_address = 0   /* strating with 0 for this example */
   for (register_no = 0;register_no++;register_no <15)    /* 0...14 are the 15 lateches 74HCT574 */
      tmp_register = 0x00
      for (bit=0;bit++; bit<8)                           /* each register has bits 0...7 */
         tmp = value(process_register_address)
         if tmp > reference
            tmp_register(bit) = 1                       /* use bitwise OR to set a bit of temp register. No need to explicitely clear a bit, this is done by tmp_register = 0x00 above */
         end if
      process_register_address += 1                      /* prepare for reading next process register */
      next bit
      output_register (register_no, tmp_register) /* write contents of tmp_register to the register number register_no */
   next register_no
end_main_loop

Note that this is pseudo code in a syntax similar to C but not real C. You will have to formalize the code and add your own routines e.g. for reading a process value and for adressing an output latch and writing data to it.
 
I appreciate for your kind support. Still i have some doubts.

I explain you what i exactly want to do.

I want a indication device which gives LED indication if limit condition occurs.

I have to connect 5 remote devices with each device having 24 outputs to this indication device.

The output of remote device is the value in between 0 to 100 which i am getting in micro controller over modbus protocol from PLC.

I have three limit conditions i.e 20,40 and >100.

So now i have 40 register where i have to check for first limit condition, next 40 registers for second limit condition and remaining 40 registers for third limit condition
and blink the particular LED if condition exist.

I have connected 120 LED's through latch IC's and another 15 pins of micro controller are used as a clock input to every latch.

So every time i have to provide the clock to latch IC to get the output.

Please suggest what i need to do.
 
I have three limit conditions i.e 20,40 and >100.

Small loop...

If value >100 do this
If value >40 do this
If value >20 do this

You can use a look up table to set the 'do this variable' based on what register is triggering the fault, especially easy if you use an LED driver like I suggest bellow...

As I said if you need to compare 120 values you need to compare 120 values...

I have connected 120 LED's through latch IC's and another 15 pins of micro controller are used as a clock input to every latch.
Us a single LED driver chip like the HT1632C instead...
 
Top