help to optimize code!!

Status
Not open for further replies.

paddy_p

Member level 2
Joined
May 31, 2012
Messages
48
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Mumbai India
Visit site
Activity points
1,680
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. (using switch case)

Please suggest any alternative to reduce the code size.

thanks,
paddy
 

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?

thanks
 

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.
 
Reactions: tpetar

    tpetar

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

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…