0

I wrote a Code for dc motor speed control in Mikroc But during build it gives error as " undeclared identifier "for all the variables. Can any one help me what is the problem in the code. I used pic18f8722. I have done coding in Java but very less coding in c Is there any problem in declaration of variables and functions?? Below is the code....

float pid(int actualOut){
    error=setPoint-actualOut;
    pOut=error*p;
    iOut=iOut+error*i;
    if(iOut>255)
        iOut=255;
    else if(iOut<0)
        iOut=0;
    dOut=(error-lastError)*d;
    out = pOut+iOut+dOut;
    lastError=error;
    if(out>255)
        out=255;
    else if(out<0)
        out=0;
    return out;
}

void pwm(short duty){
    short dutyCycle=duty;
    PWM1_Init(5000);
    PWM1_Start();
    PWM1_Set_Duty(dutyCycle);
    delay_ms(500);
}

Int feedback(){
    T0CON=0xC2;
    TMR0l=193;
    INTCON=0xA0;
    TMR1l=0;
    TMR1H=0;
    T1CON=0x87;
    s=1;
    while(s==1){}
    actualOut=(60*2*TMR1L)/24;
    return actualOut;
}

void interrupt(){
    T1CON=0x00;
    s=0;
}

void main() {

    float p=100,i=200,d=10;
    int setPoint=100,s=1;
    int actualOut=0;
    float pOut=0,iOut=0,dOut=0;
    float out=16;
    int error=0,lastError=0;
    short duty;

    TRISC = 1;
    TRISG = 0;


    while(1){

        float out = pid(actualOut);
        duty=(short)out;
        pwm(duty);
        int actualOut = feedback();
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user4380889
  • 31
  • 1
  • 4
  • It would seem you're missing a few things. Like a `#include` list and some variable declarations. And you know when you do this: `error=setPoint-actualOut` in function `pid`, that `main()` declares `setPoint` doesn't mean it magically exists as a valid identifier in `pid()`, right? A remedial book on C will explain all of this. – WhozCraig Feb 14 '15 at 19:52
  • This code is written in Mikroc for pic microcontollers. There is no need of #include – user4380889 Feb 14 '15 at 19:54
  • Fantastic. Update the question tags to include the specific platform, and the latter half of my prior comment still stands. – WhozCraig Feb 14 '15 at 19:56
  • I dint understand what is the problem .It gives error for all variables – user4380889 Feb 14 '15 at 20:12
  • 1
    Your problem is that you have not declared all the variables that you are using — that's what the compiler is telling you, and what @WhozCraig is telling you. In the first two lines of the body of `pid()`, there are 4 references to undeclared variables (`error`, `setPoint`, `pOut` and `p`). The compiler can only tell you they are not declared. – Jonathan Leffler Feb 14 '15 at 20:12
  • I did in the main function. Where else I should declare it?? – user4380889 Feb 14 '15 at 20:13
  • They're declared local to the `main()` function in `main()`. You have to decide how they're going to be made accessible to other functions. You could go via the grubby route and make them global variables defined before the first function, or you can arrange to pass them to the functions, possibly packaged in a structure. But they're not declared in `pid()`. – Jonathan Leffler Feb 14 '15 at 20:16
  • Kk I got it Jonathan .thank you!! – user4380889 Feb 14 '15 at 20:18
  • The concept of 'variable' scope' is where a variable is visible. A variable defined within the main() function is only visible within the main() function. suggest either passing the variables as parameters to those functions that use them -or- declare then in the 'file scope' (I.E. outside of any function, perhaps right after the #include statements – user3629249 Feb 14 '15 at 20:22
  • Jonathan I tried making variables global defining them before pid function . But still it gives me error . For some variables it gives me "variable redefined" – user4380889 Feb 14 '15 at 20:24
  • Yes but it doesn't have any #include. So where to declare it – user4380889 Feb 14 '15 at 20:25
  • you might want to look at this answer: where you will notice only two kinds of interrupt functions and within any one of those interrupt functions, the interrupt flag bits need to be checked to determine if an interrupt has occurred. Also note the clearing of the same flag bit before exiting the interrupt. – user3629249 Feb 14 '15 at 21:08

2 Answers2

0

since your working with a PIC processor, you should read: http://www.microcontrollerboard.com/pic_interrupt.html which details how to setup an interrupt handler, etc

http://ww1.microchip.com/downloads/en/AppNotes/00937a.pdf which discusses the concepts of PID software on a PIC

http://www.microchip.com/wwwAppNotes/AppNotes.aspx?appnote=en020434 which contains appropriate source code, and other documentation regarding the pic18f8722

the source code (in assembly) shows that certain .inc files (#include files) need to be used.

The process of converting the assembly files to C files will be an excellent refresher on coding in C

Note: these files are WAY TOO LONG to be inserting here

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

You declare the function just below the #define and pin allocation etc. for the function pwm you can write it as:

void pwm(short);

The above declaration may solve some problems but I am still working on declaring the return type of functions.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
praveen
  • 1
  • 1