Jump to content
Sign in to follow this  
Steve_DeGroof

Toggle Pin Faster

Recommended Posts

I've got a piece of code that strobes a pin low N times, where N is a number between 0 and 510.

 

//read bytes without actually retrieving them
void cfSkipBytes(unsigned int count)
{
   unsigned int i;
   cfDataDir = 0xFF;     //data float
   while(cfControlPort&1<<cfRdyBit==0);
   cfAddr = cfRegData;                      //set address
   for (i=count;i>0;i--)
   {
     clear_bit(cfControlPort,cfOeBit);  //strobe read	
     set_bit(cfControlPort,cfOeBit);    
   }
}

The code works fine but I need it to go faster. Right now, the main loop compiles down to about 20-odd instructions. Anyone have any suggestions on how to tighten it up a bit?

Share this post


Link to post
Share on other sites
Guest Pavel

This code should save you a couple of instructions in the main loop:

 

//read bytes without actually retrieving them
void cfSkipBytes(unsigned int count)
{
  cfDataDir = 0xFF;  //data float
  while(cfControlPort&1<<cfRdyBit==0);
  cfAddr = cfRegData;                     //set address
  while (count--)
  {
    clear_bit(cfControlPort,cfOeBit); //strobe read 
    set_bit(cfControlPort,cfOeBit); 
  }
}

 

and even faster code may look like this:

 

//read bytes without actually retrieving them
void cfSkipBytes(unsigned int count)
{
   //This code works only if 'count' is between 0 and 512
   unsigned char i;

  cfDataDir = 0xFF;  //data float
  while(cfControlPort&1<<cfRdyBit==0);
  cfAddr = cfRegData;                     //set address

   //If 'count' is more than 255 do a 256-times loop	
   asm movf param00_cfSkipBytes+1, W
   asm movwf _i_cfSkipBytes

   if( i )
   {
       i = 0;
  
       while( --i )
       {
           clear_bit(cfControlPort,cfOeBit); //strobe read 
           set_bit(cfControlPort,cfOeBit); 
       }
 
   }

   //Loop remaining number of times	
   asm movf param00_cfSkipBytes, W
   asm movwf _i_cfSkipBytes

   while( i-- )
   {
       clear_bit(cfControlPort,cfOeBit); //strobe read 
       set_bit(cfControlPort,cfOeBit); 
   }
}

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Thanks. I tried the second one and it improved things quite a bit. Then I realized that for the one case where speed is critical, the number of bytes is always even. I created a new function called cfSkipWords(unsigned char count). The main loop of this one is only 11 instructions and skips 2 bytes per loop. So an average of 5 1/2 instructions per byte, or about a 4x speed increase.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...