CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

a simple task - but how can this be done with fewer clocks?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

a simple task - but how can this be done with fewer clocks?
PostPosted: Sat Feb 07, 2009 2:57 pm     Reply with quote

A relay controller maps 'ON' relays to a '1' in 16 bits of a word - for 16 relays - each relay draws a pretty exact 1 amp from the main 24V PSU. Different routines control different bits of controlvar.

To report the total current estimate - we need to find how many of the 16 bits are ON at any give time to get the current demand.

Question is - is the following the most efficient way to sum the bits in a 16 bit word ??
Code:

unsigned int16 controlvar;   //global   

unsigned int8 total_bits=0; //
For (i=0;i<16;i++)
  total_bits+=bit_test(controlvar,i);
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Feb 07, 2009 5:10 pm     Reply with quote

The original loop on a PIC18 takes 999 cycles.

Unrolling the loop is a common performance boost, for example:
Code:
int16 controlvar;   //global   

int8 total_bits=0;

  if (bit_test(controlvar,0)) total_bits++;
  if (bit_test(controlvar,1)) total_bits++;
  if (bit_test(controlvar,2)) total_bits++;
  if (bit_test(controlvar,3)) total_bits++;
  ...
  if (bit_test(controlvar,14)) total_bits++;
  if (bit_test(controlvar,15)) total_bits++;
This reduces the loop to 32 cycles (independent of the value in controlvar).
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

trading code space for execution speed
PostPosted: Sun Feb 08, 2009 10:40 pm     Reply with quote

most excellent reminder - i admit i forgot that i should look at trading a bit of code space for execution speed. THANK YOU so much.
Ttelmah
Guest







PostPosted: Mon Feb 09, 2009 4:34 am     Reply with quote

It'd be interesting to actually measure the code space as well.
I suspect it may not be much if any larger, and may actually be smaller!.....
The 'reason', is that the bit tests for fixed bit values, can all be translated into simple BTFS instructions, while the one using a variable, involves significant pre-calculation.
Remember also, that macros can be your friend here, so:
Code:

#define t_bit(x) if (bit_test(controlvar,x)) total_bits++

t_bit(0);
t_bit(1);
t_bit(2);
....

Makes tidier looking code, and reduces the typing.

Best Wishes
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Mon Feb 09, 2009 10:13 am     Reply with quote

If you're really desperate for fast execution speed, use a lookup table. You'd have 256 bytes stored with every possible combination of bits representing the addresses, and the numbers would be the number of bits which were set in any particular byte. But I'm not sure that it actually saves time versus checking each bit in turn, and it uses a lot of ROM space.
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

PostPosted: Mon Feb 09, 2009 10:18 am     Reply with quote

Considering that the bits you are representing the ON and OFF states of some mechanical relays, it seems pointless to be so concerned about the number of clocks it takes to add them up. Mechanical relays aren't normally changed that quickly.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

about speed of 'relay change'
PostPosted: Mon Feb 09, 2009 11:22 am     Reply with quote

while the time it takes for a relay or solenoid to close or open is in fact much longer than any of these count methods require - measured in tens or even hundreds of milliseconds -
the CURRENT draw requirements are much more sharply defined and happen in the time domain without much regard for how long it takes the mechanical result to occur.
the goal is track the total bits on - to determine NET ( average ) flow from multiple solenoid fluid valves - BUT the immediate total CURRENT demand is not so related to the actuator time- but IS tightly coupled to that pesky bit count I'm after .
Ttelmah
Guest







PostPosted: Mon Feb 09, 2009 1:36 pm     Reply with quote

The current involved will also change slowly.
Remember a relay coil is an inductor, so the current lags the actual switching change significantly.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group