Jump to content

Recommended Posts

A beginer programmer, I have written a very simple program to generate a pause using the on-board timer, tmr0. The program compiles succesfully. Using the debugger, the program functions properly except when it executes the main function "return" instruction. A "stack underflow" warning is generated and it restarts from the start. Each time it reaches the end, the warning re-occurs and it re-starts. What am I doing wrong?

 

#include <system.h>

#pragma	DATA 0x2007, _CPD_OFF & _CP_OFF & _BODEN_OFF & _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _PWRTE_OFF


	unsigned short cycle_counter=10;	//counter for no. of clock rollovers

void main(){

	option_reg = 0x07;					//set tmr0, prescale 256 and clock source
	trisc = 0x00;						//configure port C as outputs
	portc.2=1;							//led on
	tmr0 = 0x00;						//start clock
	while(cycle_counter){				//while clock cycle counter > zero
		if(intcon.2){					//clock overflow?
			tmr0 = 0x00;				//yes, reset clock and
			clear_bit(intcon,T0IF);		//reset overflow flag and
			cycle_counter--;			//decrement clock cycle counter
}
}
	portc.2=0;							//done, led off
	return;}

Link to post
Share on other sites

main() occurs in the root of the call tree, ie all functions are processed via PIC call instructions except main because it is the first function. Using the return command causes the compiler to generate a retlw instruction, the PIC debugger tries to execute retlw by extracting the return adrress off the function stack but there is no return address for main.

 

Dont terminate main with return!

 

you would be better stopping main with an endless loop. eg while(1);

 

A beginer programmer, I have written a very simple program to generate a pause using the on-board timer, tmr0.  The program compiles succesfully.  Using the debugger, the program functions properly except when it executes the main function "return" instruction.  A "stack underflow" warning is generated and it restarts from the start.  Each time it reaches the end, the warning re-occurs and it re-starts.  What am I doing wrong?

 

#include <system.h>

#pragma    DATA 0x2007, _CPD_OFF & _CP_OFF & _BODEN_OFF & _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _PWRTE_OFF


        unsigned short cycle_counter=10;    //counter for no. of clock rollovers
        
void main(){
        
        option_reg = 0x07;                    //set tmr0, prescale 256 and clock source
        trisc = 0x00;                        //configure port C as outputs
        portc.2=1;                            //led on
        tmr0 = 0x00;                        //start clock
        while(cycle_counter){                //while clock cycle counter > zero
            if(intcon.2){                    //clock overflow?
                tmr0 = 0x00;                //yes, reset clock and
                clear_bit(intcon,T0IF);        //reset overflow flag and
                cycle_counter--;            //decrement clock cycle counter
}
}
        portc.2=0;                            //done, led off
        return;}

Link to post
Share on other sites

Pixie

 

Many thanks for your very quick reply.

I used "return" because my C reference text "C programming in easy steps" uses it in all the programming examples. Now I note that the author uses the int main function and terminates with a return (0).

Anyway, I took your suggestion and removed the return instruction from my program. Guess what! The compiler adds a return instruction to the assembler code.

So I will go to your suggestion of adding an endless loop to my program.

 

Thanks again

Link to post
Share on other sites

That is because C books assume that your program is going to run on a computer with an operating system, in which case main returns to the operating system. Most OSs expect main to return an error code ie 0 is ok.

In a small embedded system there is no OS, the proceesor jumps to the reset vector on power up (or reset), Boost C puts a command at the reset vector to jump to start up code, when start up code completes it jumps to main.

 

Pixie

 

Many thanks for your very quick reply.

I used "return" because my C reference text "C programming in easy steps" uses it in all the programming examples.  Now I note that the author uses the int main function and terminates with a return (0).

Anyway, I took your suggestion and removed the return instruction from my program.  Guess what!  The compiler adds a return instruction to the assembler code.

So I will go to your suggestion of adding an endless loop to my program.

 

Thanks again

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...