Thread: Need to run 2 for loops simultaneously in C. Microprocessor Class. MSP430 TI

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    8

    Need to run 2 for loops simultaneously in C. Microprocessor Class. MSP430 TI

    In my Microprocessors class we have been tasked with creating 2 different displays with a string of 8 LED lights depending on different buttons presses. The first display was simple on the second display 2 lights must start at the center and bounce to the outside and return to the center. Basically 0001_1000 then 0010_0100 so on until 1000_0001, then return to the original 0001_1000 in the same fashion.
    To accomplish this I have tried 2 for loops, but the problem is having them run simultaneously.

    If it helps any the Hardware is TI MSP430 Launchpad.
    Also, the for loops in question are in bold and italics.
    Code:
    #include <MSP430G2553.h>
    #define DELAY 250000
    #define DELAY2 150000
    void main()
    {
        WDTCTL = WDTPW + WDTHOLD;
        
        P1DIR = 0x00;
        
        P2REN |= (BIT3|BIT4);
        P2OUT &= (~(BIT3&BIT4));
        
        unsigned int i, j;
    
        while(1)
        {     
          if(P2IN&BIT3)
          {     
        
            P1DIR = 0xFF;
    
            while(1)
            {
    
              P1OUT = 0xA0;
              for(i=0;i<6;i++)
              {         
               _delay_cycles(DELAY);
                
               P1OUT = P1OUT>>1;
              }
    
              P1OUT = 0x05;
              for(i=0;i<6;i++)
              { 
               _delay_cycles(DELAY);
                
               P1OUT = P1OUT<<1;
             }
         
            }
         }
    
    
         if(P2IN&BIT4)
         {
          P1DIR = 0xFF;
        
          while(1)
          {
            P1OUT = 0x10;
              for(i=0;i<1;i++)
             {
              _delay_cycles(DELAY2);
     
              P1OUT = P1OUT>>1;
             }
    
              P1OUT = 0x08;
              for(j=0;j<3;j++)
              {
               _delay_cycles(DELAY2);
    
              P1OUT = P1OUT<<1;
              }
          }
        }
      }
     }     
    }
    Thank You,
    Taylor Hamilton
    Last edited by TaylorHamilton; 02-18-2012 at 11:19 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I don't know about that microprocessor therefore I have to ask: By "simultaneously" you mean concurrently or at exactly the same time?
    If the first, is that microprocessor interrupt driven? If yes, learn how to use/set/call those interrupts. If no, sorry but you can't do it.

    If the second, do you have multiple cores/processors? If yes, learn how to arrange/assign them different code to run. If no, sorry but you can't do it.

    Sorry for being too generic...
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    GReaper, yes I was meaning at the same time. Through a quick GOOGLE search I have discovered that there is only core/processor. However, it is possible to do interrupts and I have found a tutorial to do so. Though in the tutorial it says it more of an advanced topic. I do not mind being ahead of the game as far as the class goes but we are just learning how to use these as recent as a week or so ago are you sure there is no simpler way? I am only asking because he believe simplicity is the best route when available. If not then I shall start using the tutorial I found. Tbh, I didn't even know what and interrupt was until you asked lol.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I see what you mean, but there's no "simplicity" in raw CPU programming. That's why libraries like e.g "ncurses" exist. Actually, that's why operating systems exist! Unless your teacher finds a library for you, which is next to fantasy because few libraries exist for embedded systems, you'll have to do all the dirty work yourself. Anyway, good luck with your studies!

    PS: You can't run two or more pieces of code at the same time if your architecture isn't a multi-core/multi-processor one. Concurrent execution "appears" to run at the same time, while actually the system manages how the available time of the processor is divided, behind the scenes. Assuming there's no system, you will have to make that code yourself. Be warned though that concurrency involves many advanced concepts like multithreading, context switching( saving the state of thread and loading another one ), timing( you'll need a system timer that will generate interrupts in regular intervals ) and much, much more!
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Yeah I looked into the whole Concurrent execution aspect and looked at multithreading and heard mention of the others you stated. Maybe multithreading is the way he wishes us to go lol. Thank you for your help btw.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Actually, maybe you could help me with another aspect of this program. When I press a different button how would I get the program to switch to the other if statement. I have tried breaking in the while statements but in order for it to work I have to hit the button between for loops. Which, is not the intended result. Ex. if(P2IN&BIT4)
    break;
    That was for a test reason of course but if I would figure that out all that would be left is the multithreading lol.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by TaylorHamilton View Post
    When I press a different button how would I get the program to switch to the other if statement.
    You could use "goto". Although it's considered "black art", it'll be the easiest/fastest solution for this. The other way is to add some extra flags that will make the other and/or next loop to exit or not loop at all.
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Yes, I considered the goto but as you stated its the "black art". My teacher said using goto is bad programming but it seems more and more tempting lol. :P

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Ok, then change the condition of your inner while loops from "1" to e.g "flag", set that flag to one at the start of your inner while loops( before entering them ), and set it to zero when you want to "swap" loops( before the "break" ). You will probably want to add an extra condition on every second for loop in order not to enter it at all, or you could do an "if (!flag) break;" before entering these loops, whichever appeals to you better.
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Thank you once again GReaper. As for my first problem I just wrote the code in that section to basically come out on the LEDs as 0001_1000 (0x18) then goes to 0001_1000 (0x24) and such then reversed it back all the way to (0x18). To meet the time requirement for the display (which I clocked at 6 times the amount required) I just lowered the delay. Im suprised it took me this long to think of that I guess just because of the length of code for it is why I did not think that way about it lol. I am going to shoot my Professor an email to check if he would prefer the Multiple Threads though.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Trying to do multiprocessing on your micro-controller is probably overkill. I really think you need to re-examine what is required. I think your problem has more to do with all the while(1) loops. I understand the outer one, but the others seem like trouble. Once you enter one of these loops you don't give yourself any way to exit them. Don't forget that you can use functions to make your logic clearer. Also your processor has interrupt capability for some of the external pins, you may want to investigate.

    Is it possible for both switches to be pressed at the same time?

    Jim

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    Jim, Yes it is possible I have the processor rigged to a Protoboard and honestly I dont need the Processor hooked into it. It's just so I dont have to keep removing the chip from the board then onto the Micro and vice versa. Need to run 2 for loops simultaneously in C. Microprocessor Class. MSP430 TI-protoboard-jpg © CARLOS CASTILLO (ATU ELECTRICAL ENGINEERING DEPT.)
    Sorry if that came across mean. I just reread that and was like wow that sounds harsh lol. :P
    Last edited by TaylorHamilton; 02-19-2012 at 01:08 AM. Reason: Copyright

  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Jee, so that's how much embedded this system is!... :-x

    EDIT: I wish we did such things in my college too... But no, we're still stuck with theoretical concepts, 90% of which are completely useless!... I lost the initial excitement long ago. Now it's just a struggle for the degree( which won't be of much use either ).
    Last edited by GReaper; 02-19-2012 at 01:07 AM.
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    lol yeah honestly its wasnt that bad to rig up haha. Thats about all I walked out of my Digital Logic class knowing in full how to do lol.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You don't have two loops, you have 4 states
    Finite-state machine - Wikipedia, the free encyclopedia

    Namely
    Button1_Up
    Button1_Down
    Button2_Up
    Button2_Down

    The two up states don't do anything.

    The two down states each have an associated counter (button 1 has 8 values)

    So you press button 1, you advance to the next button1 counter, and output the LED pattern that corresponds to that counter.

    All this you can do in a single while loop.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Microprocessor project in c
    By chila in forum C Programming
    Replies: 6
    Last Post: 08-08-2011, 01:16 PM
  2. Need Advice on C Programming for MicroProcessor ??
    By Gaurav Singh in forum Tech Board
    Replies: 5
    Last Post: 04-04-2011, 11:17 AM
  3. microprocessor
    By BEN10 in forum General Discussions
    Replies: 16
    Last Post: 09-15-2009, 03:17 PM
  4. assembly for M8 microprocessor series
    By GanglyLamb in forum Tech Board
    Replies: 3
    Last Post: 03-15-2006, 05:34 PM
  5. a microprocessor thing!
    By badhri in forum C Programming
    Replies: 8
    Last Post: 05-29-2002, 10:58 AM

Tags for this Thread