Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

help to optimize code!!

Not open for further replies.


Member level 2
May 31, 2012
Reaction score
Trophy points
Mumbai India
Activity points

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. (using switch case)

Please suggest any alternative to reduce the code size.


Hard to imagine how 120 register values are controlling just 8 bits?

There must be an algorithm relating both, and it's nature decides how operations can be optimized. As long as you don't experience code space limitations, the problem might be considered academic, however.

If the relation of process values to output bits is somehow symmetrical, it may be decoded in the same lines of code using (a) ROM table(s).

E.g. check 120 temperatures against limit values: One ROM or RAM table contains the actual limit values, another the respective alarm codes.
The 120 holding registers contains process values and that are in between 0-100.

I have to compare these values with some constant values i.e say 20,40 and >100.

I have connected 8 port pins of micro controller to latch IC which are common to all 15 latch IC's and 15 pins are connected as a clock to enable the latch.

So according to result of comparison the data is sent over the 8 pins that are connected to all latch IC's and by providing clock the expected latch is enabled.

I am doing this using Switch case and by doing this way it increases the number of cases.

Can you please explain how to do it with ROM table?


You didn't say this clearly, but I assume that each latch bit is outputting the compare result for one process value. In this case the latch and bit number can be simply calculated from the register index and everything can be processed in a loop.

Because the latch registers can't be read back, you'll have a an array of bytes where the bit values are manipulated and then send out at once.
  • Like
Reactions: tpetar


    Points: 2
    Helpful Answer Positive Rating
Not open for further replies.

Part and Inventory Search

Welcome to