I am using a PIC to control a 28BYJ-48 stepper motor using a ULN2003 driver board. I have found a code that works perfectly but trying to understand what one line of code is doing.
Having searched I have not come across this being raised in the past. I am a new comer to C programming and learning from other peoples code. I came across this in a code which works as I would expect it to.
if(Wait > 0) /* Wait is set in the void main void which calls this in the function*/
{
delay = Wait; /*delay takes the value of wait*/
do {
while(INTCONbits.T0IF == 0) {}
INTCONbits.T0IF = 0;
} while (--delay > 0);
}
The while (--delay > 0);
.
Has me stumped as the delay is taken from a know value and passed into a function call so what does the --
have to do with it.
I know is some languages its used to decrement the variable either after it has been read but as far as I am aware the value is not being lowered.
Any help or pointer would be appreciated.
Edited to show more of the code.
void StepMotor(int16_t Count, uint8_t Wait)
{
static uint8_t state = 0;
/* values for half step 0001, 0011, 0010, 0110, 0100, 1100, 1000, 1001*/
static const uint8_t HalfSteps[8] = {0x01, 0x03, 0x02, 0x06, 0x04, 0x0C, 0x08, 0x09};
uint8_t delay;
do
{
if (Count > 0)
{
PORTC = HalfSteps[state]; /* drive stepper to select state */
state++; /* step one state clockwise */
state &= 0x07; /* keep state within HalfStep table */
Count--; /* update step count */
}
else if (Count < 0)
{
PORTC = HalfSteps[state]; /* drive stepper to select state */
state--; /* step one state counterclockwise */
state &= 0x07; /* keep state within HalfStep table */
Count++; /* update step count */
}
/* Wait between steps */
if(Wait > 0) /* Wait is set in the void main void which calls this in the function*/
{
delay = Wait; /*delay takes the value of wait*/
do {
while(INTCONbits.T0IF == 0) {}
INTCONbits.T0IF = 0;
} while (--delay > 0);
}
} while (Count != 0);
}
void main(void)
{
/*
* Application initialization
*/
Init_PIC();
/*
* Application process loop
*/
while(1)
{
StepMotor(4076, 4); /* step about 1/2 a revolution clockwise at 4.096 milliseconds per step (about 8 seconds) */
__delay_ms(1000);
StepMotor(-2038, 2); /* step about 1/2 a revolution counterclockwise at 2.048 milliseconds per step (about 4 seconds) */
__delay_ms(1000);
StepMotor(-2038, 2); /* step about 1/2 a revolution counterclockwise at 2.048 milliseconds per step (about 4 seconds) */
__delay_ms(1000);
}
}
Maybe this is more helpful to get an answer to my question what is the "while (--delay > 0); "
doing.