We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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; }
Your code seems prety straightforward. I cannot see anything obvious, but then I guess you would not have posted here otherwise. Is it the case that the function delay() is working, but delay1() is not? Are you building with minimum optimisation and global register colouring turned off? Do you have any interrupts running? Observations: the declaration of delay() appears twice and your comments are a little confusing.
The obvious questions would be: 1) what speed is your micro running at, in other words: what's your plan that delay1() should burn 1 second based on? 2) the delay loops look slightly odd. Did you really want to wait for roughly 0x28 * 0x4b * 0x02 = 6000 and 0xe1 * 0xea * 0x08 = 421200 djnz times in delay() and delay1(), respectively, rather than 0x284b01 and 0xe1ea08? 3) Did you try moving the delay functions to a separate, real asm module, instead of diving into the perilous waters of inline assembly? 4) Did you inspect the actual machine code? 5) Did you try it in the simulator? And if so: was it configured correctly?
Pay attention to the linker error.You may need to add followings: INIT.A51 and STARTUP.A51. BTW,why not using a function like this: void Delay() { uchar ii,kk,jj; for (ii==0x28;ii>0;ii--) for (jj=0x4b;jj>0;jj--) for (kk=2;kk>0;kk--) {}; }