For loop counting to ten

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
I started messing around with my MSP430 after a few months hiatus, and now I need some help with my for() loop.

I need a loop to blink my LED1 in a specific way. Like this, blink(1) and a very short delay, then next blink. At the end of each blinking line, there should be a bit longer delay to indicate "new line".

1
11
111
1111
11111
111111
1111111
11111111
111111111
1111111111

At the very end, some fast flash is supposed to occur. But I have no idea on how to create this for loop.

And while writing this post, I came to think of just doing 10 separate for() loops, so I can get that longer delay in. But any better suggestions?
 

WBahn

Joined Mar 31, 2012
32,836
Use nested loops. The outer loop does a complete line. The inner loop has two loops, one after the other. The first one flashes the LED for the desired number of times and the second creates the delay at the end of the line. You use the loop counter for the outer loop to control how many times to flash and how long to delay.
 

bug13

Joined Feb 13, 2012
2,002
or this:

Code:
#include <stdio.h>
#include <stdint-gcc.h>


void blink(unsigned int num);

int main()
{
    unsigned int i = 0;
    for(i = 0; i < 10; i++){
        blink(i + 1);
    }
    return 0;
}

void blink(unsigned int num){
    unsigned int i = 0;
    for(i = 0; i < num; i++){
        /* replace this with your short blink */
        printf("1 ");
    }
    /* replace this with your longer delay*/
    printf("\n");
}
 

Thread Starter

StrongPenguin

Joined Jun 9, 2018
307
I did this. Not 100% finished, but it does the job.

Code:
for(rows = 0; rows <= 10; rows++)
        {
            //Inner loop
            for(pause = 0; pause <= rows; pause++)  //If lines is less than rows, increase line
            {
                for(line = 0; line <= pause; line++)
                    {
                    __delay_cycles(ENDLINE);
                    }
            P1OUT = BIT0;
            __delay_cycles(BLINK);
            P1OUT = 0x00;
            }

            if(rows == 10)      //Check to see if rows is 10
            {
            P1OUT |= BIT6;
            __delay_cycles(FINISH);     //Should only come here after counting to 10
            P1OUT = 0x00;
            }
         }
 
Top