can i define a constant like this ? using mikroC

Discussion in 'Embedded Systems and Microcontrollers' started by rocker123uk, Feb 6, 2016.

  1. rocker123uk

    Thread Starter New Member

    Dec 6, 2015
    10
    0
    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)
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    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.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    You can also string a lot of instructions together like this.

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

    Expert

    Feb 24, 2006
    10,145
    1,791
    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.
     
  5. rocker123uk

    Thread Starter New Member

    Dec 6, 2015
    10
    0
    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
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,145
    1,791
    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.
     
  7. rocker123uk

    Thread Starter New Member

    Dec 6, 2015
    10
    0
    ok and if i want to use it in the main function. do i just write it as stepone(); ?
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,012
    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.
     
  9. rocker123uk

    Thread Starter New Member

    Dec 6, 2015
    10
    0
    #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?
     
  10. Papabravo

    Expert

    Feb 24, 2006
    10,145
    1,791
    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.
     
  11. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    Try

    Code (Text):
    1. int8_t step[] = {1,4,2,8};
    2.  
    3. ...
    4. void main() {
    5. do {
    6.   PORTA = step[state];
    7.    ...
    8.  
     
    Papabravo likes this.
  12. Papabravo

    Expert

    Feb 24, 2006
    10,145
    1,791
    state = ++state % 4 ;
    or
    ++state ; state %= 4 ;
    or
    Code (Text):
    1.  
    2. if(direction == CW)
    3.   { ++state ; state &= 3 ; }
    4. else  // direction == CCW
    5.   { --state ; state &= 3 ; }
    6.  
     
    Last edited: Feb 7, 2016
Loading...