The question to this problem is to add something to the code below. What I need
ID: 2085518 • Letter: T
Question
The question to this problem is to add something to the code below. What I need to add is to make this program light a led 2 times delay than light the led 5 times delay than light the led 7 times. I know I need to add a for loop. But keep getting errors in code composer. I just need help on how to add the for loop to work specifically with those numbers. Would I use for example for lighting twice . for( i=0; i=2;i++), for (i=0; i=5; i++), for (i=0; i=7; i++)?
#include
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction for (;;)
{ volatile unsigned int i;
P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR
i = 50000; //Delay
do (i--);
while (i != 0);
}
}
Explanation / Answer
Answer:- The main() is written below-
---------------------------------------------------------------------------------------------------------------------------------
void delay_loop(void); /* declaration of function for delay */
int main(void) /* main function starts */
{
unsigned int i;
WDTCTL = WDTPW +WDTHOLD ; /* stop the watchdog timer */
P1DIR |= 0x01; /* set p1.0 as output pin */
for(i=0; i<2 ; i++) /* turn on led twice */
{
P1OUT = 0x01; /* Turn led on */
delay_loop(); /* give delay */
P1OUT = 0x00; /* Turn off the led */
delay_loop(); /* call the delay function */
}
for(i=0; i<5 ; i++) /* turn on led 5 times */
{
P1OUT = 0x01; /* Turn led on */
delay_loop(); /* give delay */
P1OUT = 0x00; /* Turn off the led */
delay_loop(); /* call the delay function */
}
for(i=0; i<7 ; i++) /* turn on led seven times */
{
P1OUT = 0x01; /* Turn led on */
delay_loop(); /* give delay */
P1OUT = 0x00; /* Turn off the led */
delay_loop(); /* call the delay function */
}
}
void delay_loop(void) /* function definition for delay */
{
unsigned int i ; /* define one integer */
for( i = 5000; i != 0 ; i--) ; /* delay loop count */
}
--------------------------------------------------------------------------------------------------------------------
Note:- Just add your header file " #include <...>, and then compile and run the code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.