MPLAB
Author |
Message |
molten_rock01
|
Posted: Sat Jun 11, 2005 7:48 pm Post subject: MPLAB |
|
|
Hi. I'm new at C and am doing a project for a class. My project partner just learned to use sub routines but we're having trouble. Could someone suggest what might be wrong?
code: | int a, b, c, x, portaval; // Declare variables for loop
main()
{
PORTA = 0;
PORTC = 0;
CMCON0 = 7; // Turn off comparators
ANSEL = 0; // Turn off ADC
TRISA0 = 0; // Make RA0/RA1/RA2/RA4/RA5/RC0/RC1/RC2/RC3/RC4/RC5 outputs
TRISA1 = 0;
TRISA2 = 0;
TRISA4 = 0;
TRISA5 = 0;
TRISC0 = 0;
TRISC1 = 0;
TRISC2 = 0;
TRISC3 = 0;
TRISC4 = 0;
TRISC5 =0;
TRISA3 = 1; // Make RA3 input
a = 0;
b = 0;
c = 0;
x = 0;
portaval=x;
while (1==1) // Loop forever
{
if (RA3==1) // button is pressed
{
b = b + 1; // Counter
} // fi
if (b==1) // button is pressed for the first time
{
x = showrider();
blinkled(x);
}
else if (b==3) // button is pressed for the third time
{
x = showrider();
blinkled(x);
}
else if (b==5) // button is pressed for the fifth time
{
x = showrider();
blinkled(x);
}
else if (b==7) // button is pressed for the seventh time
{
x = showrider();
blinkled(x);
}
else if (b==9) // button is pressed for the nineth time
{
x = showrider();
blinkled(x);
}
} //elihw
} // end main
int showrider()
/* Knight Rider Effect*/
{
while (RA3==1) // wait for the user to lift finger after pressing button
{
} // elihw
while (RA3==0) //while button is not being pressed
{
PORTA = 0b000001;
for (a=0; a<255; a++); // Simple delay loop
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
return 1;
}
PORTA = 0b000010;
for (a=0; a<255; a++); // Simple delay loop
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
return 2;
}
PORTA = 0b000100;
for (a=0; a<255; a++); // Simple delay loop
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
return 3;
}
PORTA = 0b010000;
for (a=0; a<255; a++); // Simple delay loop
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
return 4;
}
PORTA = 0b100000;
for (a=0; a<255; a++); // Simple delay loop
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
return 5;
}
} //elihw
}
void blinkled(portaval)
{
while (RA3==0) //while button is not being pressed
{
if (portaval==1)
{
while (1==1)
(
PORTA = 0b000001;
simpledelayloop();
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
break;
}
} //fi
}
}
else if (portaval==2)
{
while (1==1)
(
PORTA = 0b000010;
simpledelayloop();
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
break;
}
} //fi
}
}
else if (portaval==3)
{
while (1==1)
(
PORTA = 0b000100;
simpledelayloop();
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
break;
}
} //fi
} //elihw
}
else if (portaval==4)
{
while (1==1)
(
PORTA = 0b010000;
simpledelayloop();
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
break;
}
} //fi
} //elihw
}
else if (portaval==5)
{
while (1==1)
(
PORTA = 0b100000;
simpledelayloop();
if (RA3==1)
{
while (RA3==1)
{
}
while (RA3==0)
{
break;
}
} //fi
} //elihw
}
} //elihw
}
void simpledelayloop()
{
for (a=0; a<255; a++) // Simple Delay Loop
for (c=0; c<127; c++);
PORTA =0b000000;
}
|
When ever we build this program, the code: | void blinkled(portaval) | gives a syntax error. We've looked over the code a thousand times. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
the_short1
|
Posted: Sat Jul 23, 2005 12:57 pm Post subject: (No subject) |
|
|
can i suggest two things: make USER friendly variable names.. TRUST ME it makes it a LOT easier to debug..
and: go like
while (RA3==0){
}
i think you just got a bracket out of place.. hence the syntax error. happened to me all the time until i swithed the the cleaner way.. mkaes it easier to notice , i used MLAB Ide for programming our robot www.instigators1596.com
-kevin |
|
|
|
|
|
wtd
|
Posted: Sat Jul 23, 2005 1:24 pm Post subject: (No subject) |
|
|
Indeed. "a", "b", "c" and "portaval" are not meaningful variable names.
Use spaces.
Bad:
Good:
Don't use meaningless comparisons.
Bad:
Good:
Don't use global variables. Use local variables and pass them via function parameters.
Instead of long "if... else..." constructs that just compare one int, use a switch.
Bad:
code: | if (foo == 1)
{
}
else if (foo == 3)
{
}
else if (foo == 5)
{
}
else
{
} |
Good:
code: | switch (foo)
{
case 1:
break;
case 3:
break;
case 5:
break;
default:
break;
} |
On the other hand, you do the same thing every single time, so reduce your code's length via math.
Bad:
code: | if (b==1) // button is pressed for the first time
{
x = showrider();
blinkled(x);
}
else if (b==3) // button is pressed for the third time
{
x = showrider();
blinkled(x);
}
else if (b==5) // button is pressed for the fifth time
{
x = showrider();
blinkled(x);
}
else if (b==7) // button is pressed for the seventh time
{
x = showrider();
blinkled(x);
}
else if (b==9) // button is pressed for the nineth time
{
x = showrider();
blinkled(x);
} |
Good:
code: | if (b % 2 == 1 && b >= 1 && b <= 9)
{
x = showrider();
blinkled(x);
} |
|
|
|
|
|
|
|
|