"Integral constant expected"

Discussion in 'Programmer's Corner' started by peck68, Apr 24, 2010.

  1. peck68

    Thread Starter Member

    Nov 27, 2009
    73
    0
    For some reason i can't use a variable to use with delay_us() with Mikro for a PIC, I am making like a homemade PWM since my pic doesn't support it

    Code ( (Unknown Language)):
    1.  
    2.  
    3. unsigned int delay;
    4. void main(){
    5.     TRISB = 0;
    6.     PORTB = 0;
    7.     delay = 0;
    8.     while(1){
    9.         PORTB = 0xff;
    10.         delay_us(1);
    11.         PORTB = 0;
    12.         delay_us(delay);
    13.        
    14.         if(delay < 50000){
    15.             delay++;
    16.         }
    17.         else{
    18.             delay--;
    19.         }
    20.     }
    21. }
    22.  
    That brings up the error of "Integral constant expected" for "delay_us(delay);"

    However if i used say delay_us(5); it works completely fine?

    thanks for any help :)
     
  2. rjenkins

    AAC Fanatic!

    Nov 6, 2005
    1,015
    69
    You'll have to write you own delay loop function, just a for( ; ; ) with the input value multiplied by whatever you need to get uS.
     
    Last edited: Apr 24, 2010
  3. mik3

    Senior Member

    Feb 4, 2008
    4,846
    63
    The delay_us() function does not support variables, according to your problem. Therefore, as rjenkins said, you can use a for loop and set the delay_us() inside the loop to 1 us. Then use your variable delay to execute the loop as many times you want the total delay.
     
  4. peck68

    Thread Starter Member

    Nov 27, 2009
    73
    0
    Ahh sounds reasonable - will try that out, thanks :)
     
Loading...