Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MPLAB xc8 error in programing

Status
Not open for further replies.

julian403

Full Member level 5
Full Member level 5
Joined
Feb 28, 2014
Messages
254
Helped
1
Reputation
2
Reaction score
1
Trophy points
18
Location
Argentina
Activity points
2,105
Why MPLAB give :
make[2]: *** [dist/default/production/ELQUEVAPA.X.production.hex] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 1s)

In the next code:

Code:
#include <xc.h>


// CONFIG
#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF       // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF         //

extern int aux=0; 
extern int aux2=0; 

void show (int var)
{ 
var++; 
show(aux2);

}

void main(void) {
     
    INTCON=0b10010000;
    TRISB=0x02;
    TRISA=0x01; 
       
    while (!PORTAbits.RA1) 
    {
        show(aux);
    }
    if (PORTAbits.RA1) 
    { 
        INTCONbits.GIE=0;
    }

    return;
}

I think that the error it's in the function show. But why?
 
Last edited:

Not sure about the meaning of the above error, but your code has a wrong declaration. You are instantiating the function show() within itself.
 
Not sure about the meaning of the above error, but your code has a wrong declaration. You are instantiating the function show() within itself.

Yes. that's a recursivity and I used it on Qt but maybe mplad do not use that.

Why can I do so
 

In my early days of programming, not all compilers supported recursive calls. Check the specification of this compiler if this is the case. Anyway you are comparing compilers for C(XC8) and C++(QT), which are a kind "different" languages, and in general have more resources d.
 
thanks. Other one.

If I want to define a globar var, it's like this:

Code:
extern int var =0 

void show (int aux) 
{ 
aux++; 

return; 

} 

void main (void) 
{ 
show (var) ;
show (var);
return; 
}

So there, var must be 2?
 

First, the advice: get yourself a good book on C programming and learn the language from the ground up (your questions are very basic).

your first code example generates an infinite recursion, i.e. your function calls itself endlessly (more specific: until you get a stack overflow).
your second code example increments a local variable when you expect it to increment a global one.

.
No, it should be '1'
You are post incrementing with aux++.

No. What's incremented is the local copy of the global variable that gets destroyed on function return.

var never changes at all.
 
You're absolutelly right. The function would work in that code as supposed expected only if the variable inside function show() was var instead of aux, but it is in fact not a good programming style; to update global variables without implementing specific functions for that ( e.g SetVal, GetVal ).
 

Referring to post #1. You are only showing make errors, no actual compiler errors messages. Either you omitted the relevant part of the error log or there's no compiler or linker error at all. Rather an incorrect project setup.

Regarding recursive calls, even if they are supported (I guess they are in xc8), you have an endless recursion with no termination condition. The code may compile but will never run.
 

How Can I make a variable which do not destroy in the return? But do not using pointer. I think there is a way but I do not remember.
 

How Can I make a variable which do not destroy in the return?

You can declare the variable within any function as static. This way the variable stay local, but with the ability to do not loose its content after leaving out of the function.
 
You can declare the variable within any function as static. This way the variable stay local, but with the ability to do not loose its content after leaving out of the function.
I did that but I'm not sure where to declare static int var. If I have to declare it in each funtion or global
 

"static" means that the variable will be declared in the main memory and NOT on the stack (or some other temporary memory depending on the actual device). However the visibility of the name will still be limited to just the surrounding function (if declared inside of one) or the the file.
Declaring a static variable inside a function is is a common technique for havign a variabel that retains its value between function calls but is only visible within the function.
Susan
 
Thanks to all. The interrup function is (by microchip)

Code:
void interrupt   tc_int  (void)        // interrupt function 
 
{
        if(INTCONbits.T0IF && INTCONbits.T0IE) 
    {                                     // if timer flag is set & interrupt enabled
                TMR0 -= 250;               // reload the timer - 250uS per interrupt
                INTCONbits.T0IF = 0;                  // clear the interrupt flag 
                PORTB = 0x40;             // toggle a bit to say we're alive
        }
 
}

But how Can I pass a variable to this funtion?
 

But how Can I pass a variable to this funtion?

You can't. Because you never call it from your program. An interrupt function is called by the processor itself in case of a specific event (in your case apparently on the expiration of a timer).

If you need a specific value inside the interrupt function, you need to compute it separately and make it available to the interrupt function using a global variable.
 
If you need a specific value inside the interrupt function, you need to compute it separately and make it available to the interrupt function using a global variable.

That's I want to use. Can you give me an example? a simple code where it's define the variable and the interrupt funtion.
 

Code:
volatile int myVar;
void interrupt myISR(void)
{
    myVar++;
}
Your compiler syntax might be a little different and you will probably want more in the ISR to reset the interrupt flag etc. but the principle is all there.
Susan
 
Probably too late now but I want to go back to the comments about the recursive function.
The way you have written the 'show' function in the first post will certainly cause problems at run time as there is no conditional statement to stop the recursive calling of the function.
I don't think you have anywhere said exactly which device you are using but some have a special 'call stack' that only contains a few entries and other devices create a stack in memory. In either case you will have problems.
The first time you call "show" is OK but it will then call itself again pushing another return address onto the stack. It will then call itself yet again and push yet another return address on the stack. AS there is no way out of this loop you will continue to push the return addresses onto the stack until it overflows which generally results in a 'crash' of you program.
Recursive calls are OK (if allowed by the compiler and hardware etc.) but you need to make sure that ther is a way for them to unwind before you get a stakc overflow.
Susan
 

Probably too late now but I want to go back to the comments about the recursive function.
The way you have written the 'show' function in the first post will certainly cause problems at run time as there is no conditional statement to stop the recursive calling of the function.
I don't think you have anywhere said exactly which device you are using but some have a special 'call stack' that only contains a few entries and other devices create a stack in memory. In either case you will have problems.
I already mentioned the endless recursion in post #9. In this case (missing termination condition), program execution will always fail, independent of the call stack model. We are just talking about how it fails.

The original question was however about a suspected compilation rather than runtime error. Some embedded compilers reject recursive function calls generally, but they won't analyze termination conditions. Many will allow it, I'm not completely sure about XC8. But if it does, proper termination is on user's responsibility.
 

FvM - Missed that; sorry for the extra post.
Susan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top