can i define a constant like this ? using mikroC

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
hey guys, thanks for viewing

is it possible to define a port constant like this:

#define stepone PORTC=0b00000001;
#define steptwo PORTC = 0b00000100;
#define stepthree PORTC = 0b00000010;
#define stepfour PORTC = 0b00001000;

i am creating a MikroC program where each of the steps would be used few times in the code and i dont want to go through the hassle of changing the bits in case there would need to be an adjustment.
is it fine like the above? if there are any better suggestions then please share. Thank you (using pic16f877a)
 

spinnaker

Joined Oct 29, 2009
7,830
Again, please use capital letters and periods in your sentences. Posts are hard to read without them. You should do what you can to make it easier for others to help you. That is really not asking too much for free help.

You can do constants similar to what you are doing expect leave out the semicolon. Also what is stepone? Is it a typedef?

If what you want to do is to set PORTC to some value? That is not a constant. Rather more of an inline function. Then you can do something like this.

#define stepone() PORTC=0b00000001
#define steptwo() PORTC = 0b00000100
#define stepthree() PORTC = 0b00000010
#define stepfour() PORTC = 0b00001000

When you call stepone(); (for example). It replaces stepone() with PORTC=0b00000001.

But check the MikroC documentation. Some C compilers have an inline keyword that can be appiled to functions. It will automatically place the code inline as opposed to a function call. Microchip does not have it but it is worth checking the Mikro documentation.
 

spinnaker

Joined Oct 29, 2009
7,830
You can also string a lot of instructions together like this.

#define stepfour() PORTA = 0b0000100; PORTB = 0b00001000; PORTC = 0b00001000
 

Papabravo

Joined Feb 24, 2006
21,158
You are NOT defining constants. Let me repeat that; you are NOT defining constants. You are defining text substitution macros. The difference is subtle, and very important. Don't write another line of code until you understand the difference.

You can do it that way, but it makes your code awfully hard to read. Don't believe me? Put it down for six months and then try to come back to it and figure out what is going on.
 

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
spinnaker, stepone is for the first step for the stepper motor. so #define stepone() PORTC=0b00000001 is the right way of doing i persume because its a function. AWESOME! Thanks guys!! will try it now
 

Papabravo

Joined Feb 24, 2006
21,158
GAAAAH!!! Are you paying attention or what? As a text sustitution macro, all occurrences of stepone() will be replaced by PORTC=0b00000001 There won't be any function involved -- just straight up TEXT SUBSTITUTION.
 

spinnaker

Joined Oct 29, 2009
7,830
I should have mentioned a macro is not really a constant.

A true constant is like this.

const int c = 1;

It is only for variables and not functions. The advantage over substitution is the variable to typed.

Just call stepone(); and it will replace stepone() with PORTC=0b00000001 during compile exactly as I wrote above.
 

Thread Starter

rocker123uk

Joined Dec 6, 2015
33
#define stepone() PORTC = 0b00000001

void main (){
do{
switch (state){
case 0:
stepone ();

does the above make sense..its just an example? i mean is it right yeah?
 

Papabravo

Joined Feb 24, 2006
21,158
Yes, that is technically correct, but ask yourself if there might be a more intuitive way to do it. The answer might be yes or no, and it might change when you come back to the code in 6 months or a year. Try imagining a junior programmer in your enterprise coming to you and asking WTF is going on. Then imagine trying to explain.
 

Papabravo

Joined Feb 24, 2006
21,158
Try

Code:
int8_t step[] = {1,4,2,8};

...
void main() {
do {
  PORTA = step[state];
   ...
state = ++state % 4 ;
or
++state ; state %= 4 ;
or
Code:
if(direction == CW)
  { ++state ; state &= 3 ; }
else  // direction == CCW
  { --state ; state &= 3 ; }
 
Last edited:
Top