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

Multiplying INT vs LONG cycle time

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



Joined: 16 Aug 2010
Posts: 12

View user's profile Send private message

Multiplying INT vs LONG cycle time
PostPosted: Tue Jun 03, 2014 1:46 am     Reply with quote

Hello,

I have two methods making the same function but written in different algorithms. I want to understand which one will give the least cycle time.

The first one is using long variable and the other with int. i know long needs more memory than int, but when using int I will multiply the number by a factor to result slightly the same result when using long. so will this differ in anything ?

this function is repeated six times in a loop and used to control six Triacs.

The first code:

Code:

int d[0] = {127};

#int_ext
void ext_isr()
{
    if(d[0] > 126)
    {
        output_low(pin_e0);
    }
    else
    {
        delay_us(d[0] * 55);
        output_high(pin_e0);
        delay_us(3);
        output_low(pin_e0);
    }
}

void main()
{
   while(1)
   {
       if(input(PIN_B7) && d[0] <127)
       {
          d[0] = d[0] + 1;
       }
       else if(input(PIN_B6) && d[0] > 0)
       {
          d[0] = d[0] - 1;
       }
   }
}


The other code with long:


Code:

long d[0] = {7000};

#int_ext
void ext_isr()
{
    if(d[0] > 7000)
    {
        output_low(pin_e0);
    }
    else
    {
        delay_us(d[0]);
        output_high(pin_e0);
        delay_us(3);
        output_low(pin_e0);
    }
}

void main()
{
   while(1)
   {
       if(input(PIN_B7) && d[0] <7000)
       {
          d[0] = d[0] + 100;
       }
       else if(input(PIN_B6) && d[0] > 0)
       {
          d[0] = d[0] - 100;
       }
   }
}
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Tue Jun 03, 2014 3:30 am     Reply with quote

I can't see the point of your code, but anyway.
This declaration
Quote:
int d[0] = {127};
initializes array of integers with zero elements.

Just a hint. Good way to reduce your program time when you are using I/O functions is to use FAST IO which will not produce pin direction code (as default does), but just the logic state. It will make
Code:
     
        output_high(pin_e0);
        output_low(pin_e0);

much faster.

1. Look at the .lst file in your project directory.
You are able to see the assembler instructions code.

2. Make a google search:
Quote:
pic assembler instruction set pdf
.

3. Compare the two patterns.

I hope it would help.
_________________
A person who never made a mistake never tried anything new.
ckielstra



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

View user's profile Send private message

PostPosted: Tue Jun 03, 2014 5:24 am     Reply with quote

You did not mention the PIC you are using. The PIC18 has a hardware multiply unit which can do 8x8 multiplications in 1 cycle where the PIC16 has to do a lot more work.

You can measure the timing differences yourself when you use Microchip's free MPLAB IDE, there you find a simulator tool with stopwatch. Run part of your program and read the time it took to execute.

Your program has a design flaw when you want to scale up to use more than 1 TRIAC. For example, when 1 TRIAC has a delay of up to 7000us all other TRIACs can not be controlled during that time.
Much better is to have a system that is triggers an interrupt every xxx us. When a zero crossing is detected you set timers for every TRIAC you have. In the interrupt routine you count down every timer by 1, when a timer reaches zero you fire the action for that TRIAC.
The interrupt frequency timing is dependent on how fast your processor is clocked because there is some overhead involved for saving and restoring registers. Choosing a 50us interrupt rate will give you 140 'steps' in your 7ms time frame. Because of the overhead I then suggest you use a clock frequency of 16MHz or higher.
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