This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

For loop...help !

The following is the code to turn motors in the forward direction (for a robot).
The problem is that the motors dont pause for the delay1() which is a 1 sec delay.

Instead it goes one turning without pause.

it never breaks the loop.

Could you people please help as it is urgent.

/*-----------------------------------------------------------------------------
Definitions for P1 (8 bits), P1.0 to P1.7.
-----------------------------------------------------------------------------*/
sfr P1 = 0x90;        /* SFR for P1 */

sbit P1_0 = P1^0;     /* SFR for P1.0 */
sbit P1_1 = P1^1;     /* SFR for P1.1 */
sbit P1_2 = P1^2;     /* SFR for P1.2 */
sbit P1_3 = P1^3;     /* SFR for P1.3 */
sbit P1_4 = P1^4;     /* SFR for P1.4 */
sbit P1_5 = P1^5;     /* SFR for P1.5 */
sbit P1_6 = P1^6;     /* SFR for P1.6 */
sbit P1_7 = P1^7;     /* SFR for P1.7 */
/*------------ Declaration of the various functions ------------------*/

void forward(); 		/* Function to move forward one cell. */
void delay();			/* Function Delay 1 - For Motor Speed */
void delay1();			/* Function Delay 2 - For Pause */
void delay();

/*----Declaration of variables---------------*/

unsigned int i;

/*-----------------------------------------------------------------------------
MAIN C function
-----------------------------------------------------------------------------*/

void main (void)
{

while (1)
  {
	forward();
	delay1();
	delay1();
  }
}

void forward() // Setting Bits individually to move forward
{

for(i=50 ; i>0 ; i--)
	{
 		P1_0 = 0; P1_1 = 0; P1_2 = 0; P1_3 = 1; P1_4 = 1; P1_5 = 0; P1_6 = 0; P1_7 = 0;
	 	delay();

		P1_0 = 0; P1_1 = 0; P1_2 = 1; P1_3 = 0; P1_4 = 0; P1_5 = 1; P1_6 = 0; P1_7 = 0;
 		delay();

 		P1_0 = 0; P1_1 = 1; P1_2 = 0; P1_3 = 0; P1_4 = 0; P1_5 = 0; P1_6 = 1; P1_7 = 0;
 		delay();

 		P1_0 = 1; P1_1 = 0; P1_2 = 0; P1_3 = 0; P1_4 = 0; P1_5 = 0; P1_6 = 0; P1_7 = 1;
 		delay();
	}
 return ;
}

void delay() 			/* Delay function 1  - For Motor Rotation */
{
	#pragma asm
	del: mov r0,#028h
	two: mov r2,#04bh
	one: mov r1,#01h
	again:  djnz r1,again
        	djnz r2,one
        	djnz r0,two
	#pragma endasm

	return;
}

void delay1() 			/* Delay function 2  - For Pause */
{
	#pragma asm
	del1:mov r0,#0e1h
	tw:mov r2,#0eah
	on:mov r1,#08h
	ag:djnz r1,ag
       djnz r2,on
       djnz r0,tw
	#pragma endasm

	return;
}

0