for loop issue

This is a discussion on for loop issue within the C Programming forums, part of the General Programming Boards category; So first i would like to say that i am doing this programming inside a microcontroller based complier, but the ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    So first i would like to say that i am doing this programming inside a microcontroller based complier, but the language is still c, so i appreciate the help because i am stuck right now ,

    so here is the issue i have two functions that use two different for loops when i call them in main the first one exucetes and then the second one executes which makes sense, however i need them to execute at the same time and i am not sure how to do that id figure that they would execute at the same time

    so here is the code you can ignore anything with I2C because that is how i am communicating with the microcontroller and the chip i am using

    Code:
     while(1)
    {
             WingMan_CenterOut( );
             WingMan_Left( );
    }
    
    
    
    void WingMan_CenterOut(void)
    {
    
    	int m;
    	LCD_Position(1,0);
    	LCD_PrCString("WingMan_CenterOut");
    	for(m=0; m< 1600; m++)
    	{
    		if(m<400)
    		{
    			I2Caddr=0x21;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x18;
    					
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			}
    		
    		else if((m>400) && (m<800))
    		{
    			I2Caddr=0x21;
    			command[0]=0x00;
    			command[1]=0x24;
    			
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		
    		}
    		else if((m>800) && (m<1200))
    		{
    			I2Caddr=0x21;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x42;
    			
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    
    		}
    		else
    		{
    			I2Caddr=0x21;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x81;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    
    		}
    	}
    } // end of wingMan center out
    void WingMan_Right(void)
    {
    
    	int i;
    	LCD_Position(1,0);
    	LCD_PrCString("WingMan_Right");
    	for(i=0; i<2500 ; i++)
    	{
    		if(i<300)
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x01;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>300) && (i<600))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x03;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>600) && (i<900))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x07;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>900) && (i<1200))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x0F;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>1200) && (i<1500))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x1F;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>1500) && (i<1800))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x3F;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>1800) && (i<2100))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x7F;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xFF;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    	}
    } // end of wingman right
    so what is happaning is the first function executes and then the second but it stops after three itterations the different group two address just means different chips on the same i2c bus

    basically i need both the for loop to execute at the same time when i call them in main

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Assuming you can do something about the LCD_Position / LCD_PrCString occupying the same position, then perhaps something like this.
    Code:
    while(1)
    {
        for ( m = 0 ; m < 2500 ; m++ ) {
             WingMan_CenterOut( m );
             WingMan_Left( m );
        }
    }
    
    // Factorise your copy/paste code!
    void write_and_wait ( unsigned char addr, unsigned char *command, size_t charcount )
    {
        I2C_bWriteBytes(addr, command, charcount, I2C_CompleteXfer);
        while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
        I2C_ClrWrStatus( );
    }
    
    void WingMan_CenterOut(int m)
    {
        if(m<400)
        {
            unsigned char command[] = { 0x00, 0x18 };
            write_and_wait( 0x21, command, sizeof(command) );
        }
        else if((m>400) && (m<800))
        {
            I2Caddr=0x21;
            command[0]=0x00;
            command[1]=0x24;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
    
        }
        else if((m>800) && (m<1200))
        {
            I2Caddr=0x21;
            charcount=2;
            command[0]=0x00;
            command[1]=0x42;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
    
        }
        else if ( m < 1600 )
        {
            I2Caddr=0x21;
            charcount=2;
            command[0]=0x00;
            command[1]=0x81;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
    
        }
    } // end of wingMan center out
    
    void WingMan_Right(int i)
    {
        if(i<300)
        {
            I2Caddr=0x20;
            charcount=2;
            command[0]=0x00;
            command[1]=0x01;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
        }
        else if((i>300) && (i<600))
        {
            I2Caddr=0x20;
            charcount=2;
            command[0]=0x00;
            command[1]=0x03;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
        }
        else if((i>600) && (i<900))
        {
            I2Caddr=0x20;
            charcount=2;
            command[0]=0x00;
            command[1]=0x07;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
        }
        else if((i>900) && (i<1200))
        {
            I2Caddr=0x20;
            charcount=2;
            command[0]=0x00;
            command[1]=0x0F;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
        }
        else if((i>1200) && (i<1500))
        {
            I2Caddr=0x20;
            charcount=2;
            command[0]=0x00;
            command[1]=0x1F;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
        }
        else if((i>1500) && (i<1800))
        {
            I2Caddr=0x20;
            charcount=2;
            command[0]=0x00;
            command[1]=0x3F;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
        }
        else if((i>1800) && (i<2100))
        {
            I2Caddr=0x20;
            charcount=2;
            command[0]=0x00;
            command[1]=0x7F;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
        }
        else if ( i < 2500 )
        {
            I2Caddr=0x20;
            charcount=2;
            command[0]=0x00;
            command[1]=0xFF;
    
    
            I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
            while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
            I2C_ClrWrStatus( );
        }
    } // end of wingman right
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    thank you for you suggestion all though when i tried to implemet your code changing my previous funtion definitions and what not i got a ton of errors, and to be honest i am not sure what your code is actually doing?

    im sorry i spoke to soon i did get ur code to work, however i am not understading how it worked, and whats happaning is both the for loops are executing but they are going really slow
    Last edited by begginer; 03-19-2011 at 11:42 PM.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    43

    two for loops executing toghether

    so i am trying to get two seprete for loops to execute at the same time, i need to give everyone some background information, i am using a microcontroller the cypress PSoC to be exact so some of the code is not normal c, but i will explain it

    not to insult anybody intelligencer but the I2c command is they way i am communicating with the chips, i am using the MCP23016 Digital I/O expander. The chips allows me to add 16 ports to the micro so the command(hex value) is telling the chip which pin should go high

    so with that in mind i would like to have two independed for loop working at one time, my code rigth now make sense, but is not how i want it to work, basically what is happaning is the first foor loop executes then the second one,

    i am not sure on how to have both the foor loops execute at one time

    so here is the code
    Code:
    while(1)
    {
             for(m=0; m<2500; m++)
             {
                   WingMan_CenterOut(m);
                   WingMan_Left(m);
             }
    }
    void WingMan_CenterOut(int m)
    {
    
    
    	LCD_Position(1,0);
    	LCD_PrCString("WingMan_CenterOut");
    	for(m=0; m< 1600; m++)
    	{
    		if(m<400)
    		{
    			I2Caddr=0x21;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x18;
    					
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    			}
    		
    		else if((m>400) && (m<800))
    		{
    			I2Caddr=0x21;
    			command[0]=0x00;
    			command[1]=0x24;
    			
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		
    		}
    		else if((m>800) && (m<1200))
    		{
    			I2Caddr=0x21;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x42;
    			
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    
    		}
    		else
    		{
    			I2Caddr=0x21;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x81;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    
    		}
    	}
    } // end of wingMan center out
    
    void WingMan_Left(void)
    {
    
    	int i;
    	LCD_Position(1,0);
    	LCD_PrCString("WingMan Left");
    	for(i=0; i<4000 ; i++)
    	{
    		if(i<500)
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0x80;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>500) && (i<1000))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xC0;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>1000) && (i<1500))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xE0;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>1500) && (i<2000))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xF0;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>2000) && (i<2500))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xF8;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>2500) && (i<3000))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xFC;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else if((i>3000) && (i<3500))
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xFE;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    		else
    		{
    			I2Caddr=0x20;
    			charcount=2;
    			command[0]=0x00;
    			command[1]=0xFF;
    		
    		
    			I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    			while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    			I2C_ClrWrStatus( );
    		}
    	}
    } // end of wingman right

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you know if it's possible for the two loops to "execute at the same time"? That is, can your micro controller run multiple threads or on multiple cores, at the same time?

    I'm thinking that you need to integrate these functions, or perhaps have symbiotic functions that call each other and return to each other, instead of having them actually "execute at the same time", but that your micro controller will not allow two threads or cores to run at the same time.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Is the time to do this
    Code:
        for ( m = 0 ; m < 2500 ; m++ ) {
             WingMan_CenterOut( m );
    ////////         WingMan_Left( m );
        }
    Plus the time to do this
    Code:
        for ( m = 0 ; m < 2500 ; m++ ) {
    ////////         WingMan_CenterOut( m );
             WingMan_Left( m );
        }
    Slower than doing this
    Code:
        for ( m = 0 ; m < 2500 ; m++ ) {
             WingMan_CenterOut( m );
             WingMan_Left( m );
        }
    What is at the end of your I2C bus on addresses 0x20 and 0x21?

    For example, if this were a robot arm, making the arm go up when it was already going up is probably quite efficient. But if you try up a bit, left a bit (alternately), there may be a lot more involved in the initial setup of preparing to move in a new direction.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Great - another poster starting multiple threads on the same subject.
    for loop issue

    Good luck with that, I lost interest.

    FWIW, I can instantly see how you screwed up, but I'm not interested in saying what it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have an enormous amount of repetition there that you could be getting rid of. For example, if in every case I2Caddr is always going to be 0x20, why is it inside your if-else chain?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Threads merged.

    Well in answer to your question as to why it's all so much slower, is that you left the loops INSIDE the functions.
    BOTH your wingman functions take a parameter indicating the current 'state', and just evaluate the if statements ONCE.

    Look again at my first reply - there is only ONE loop, and that is in the first bit of code.

    Oh, and please refactor your code as I showed you the first time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R Learning Issue
    By TimHarrington in forum C Programming
    Replies: 48
    Last Post: 09-06-2010, 05:33 AM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-06-2010, 12:52 AM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  4. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21