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.

[PIC] Mikroc Pic stack overflow, but my program has very less nested calls

Status
Not open for further replies.

burrow

Full Member level 2
Joined
Dec 15, 2014
Messages
147
Helped
3
Reputation
6
Reaction score
3
Trophy points
18
Activity points
1,148
Hai.. i was writing a program for pic18f4520, i just finished few lines, iam not using too much Nested functions. I guess pic18f supports upto 31 nested calls or so.. But mine seems far less still iam getting stack overflow resets. I have no idea whats happening.. Yeah i tried changing to different version of Mikroc too but got same error.

Code:
sbit LCD_RS at LATD0_bit;
sbit LCD_EN at  LATA2_bit;
sbit LCD_D4 at  LATA1_bit;
sbit LCD_D5 at  LATC5_bit;
sbit LCD_D6 at  LATC2_bit;
sbit LCD_D7 at  LATA0_bit;


  short garbage;
    sbit LCD_RS_Direction at TRISD0_bit;
       sbit LCD_EN_Direction at TRISA2_bit;
          sbit LCD_D4_Direction at TRISA1_bit;
              sbit LCD_D5_Direction at TRISC5_bit;
                  sbit LCD_D6_Direction at TRISC2_bit;
                     sbit LCD_D7_Direction at TRISA0_bit;



int datas=0;
char output[100];
short menu=0;
short submenu=0;
short refresh=0;


char *string[100]="Hello";
const char *son="Turning on";
const char *shello="Hello...";
const char *sinit="Inititializing....";

const char *swifi="...WiFi";
const char *smux="Multiplexers";
const char *suart="...Uart.....";
const char *sready="Ready.....:)";
const char *shoip="Hdd";







short A0,A1,A2,DATA,WE1,WE2;      // Used to control multiplex;
short quotient;
short binarystore[4];
short binarypointer=0;
bit bin1,bin2,bin3,bin0;
short pointerr3;





//I am using CD4099 ic to multiplex my output pins. AND Function multiplexer Does the multiplexing



void multiplexer(int addres,int dat)   //Parameters are the pin number ( from 0 to 15, 2 cd 4099 used and the DATA to be placed)
  {

  LATA.B3 =1;                   //Write enable pin of mux 1 is kept high (to turn it off)
  LATE.B1=1;                      //Write enable pin of mux 2 is kept high (to turn it off)

             delay_ms(03);
   if(dat==0)                         //Data placed in Data pin
      LATE.B2=0;                                             //Data pin is connected to PORT E PIN2
    else
      LATE.B2=1;

quotient=addres;
binarypointer=0;
binarystore[0]=9;
binarystore[1]=9;
binarystore[2]=9;
binarystore[3]=9;




      while(quotient>0)           {
       //The passed pin number is split down to binary form to place address is A0 A1 and A2 of mux
        binarystore[binarypointer]=quotient%2;
           binarypointer++;
           quotient=quotient/2;
      }



//Appropriate bits are placed in address pins
      if(binarystore[3]!=9)
          bin3=binarystore[3] ;
          else
          bin3=0;

  if(binarystore[2]!=9)
          bin2=binarystore[2] ;
          else
          bin2=0;


  if(binarystore[1]!=9)
          bin1=binarystore[1] ;
          else
          bin1=0;


  if(binarystore[0]!=9)
          bin0=binarystore[0] ;
          else
          bin0=0;

          if(bin0==1)
              LATE.B0=1   ;
           else
              LATE.B0=0   ;


          if(bin1==1)
              LATA.B5=1   ;
           else
              LATA.B5=0   ;




          if(bin2==1)
               LATA.B4=1  ;
           else
               LATA.B4=0   ;



               //Msb decides which MUX is to be turned on
            if(bin3==1)
           {
                 LATA.B3=1 ;
                 LATE.B1=0 ;

           }
             else
             {
                  LATA.B3=0;
                 LATE.B1=1 ;

             }
                      delay_ms(03);

        return  ;

}


//HT12E is connected to output of the mux and function sendrf takes 5 parameters, 4 DATA bits and 1 address bit

                  short rfbinarystore[6];
                  short rfbinarypointer=0;
                  short rfquotient;
                 bit  rfbin0,rfbin1,rfbin2,rfbin3,rfbin4,rfbin5;

                 void strConstCpy(char *dest, const char *source) {
while(*source)
*dest++ = *source++ ;

*dest = 0 ;
}

//Function to send RF commands
void sendrf(short addres, short d1,short d2,short d3,short d4)
{




rfquotient=addres;
rfbinarypointer=0;
rfbinarystore[0]=9;
rfbinarystore[1]=9;
rfbinarystore[2]=9;
rfbinarystore[3]=9;
rfbinarystore[4]=9;
rfbinarystore[5]=9;
                    rfbinarypointer=0;
                    //address  is converted to bit form
      while(rfquotient>0)
      {
           rfbinarystore[rfbinarypointer]=rfquotient%2;
           rfbinarypointer++;
           rfquotient=rfquotient/2;
      }
                              //each bit is found out

             if(rfbinarystore[5]!=9)
          rfbin5=rfbinarystore[5] ;
          else
          rfbin5=0;

           if(rfbinarystore[4]!=9)
          rfbin4=rfbinarystore[4] ;
          else
          rfbin4=0;


          if(rfbinarystore[3]!=9)
          rfbin3=rfbinarystore[3] ;
          else
          rfbin3=0;

  if(rfbinarystore[2]!=9)
          rfbin2=rfbinarystore[2] ;
          else
          rfbin2=0;


  if(rfbinarystore[1]!=9)
          rfbin1=rfbinarystore[1] ;
          else
          rfbin1=0;


  if(rfbinarystore[0]!=9)
          rfbin0=rfbinarystore[0] ;
          else
          rfbin0=0;



    multiplexer(11,1);    //Turn of Transmission Enable which is connected to  mux out 11



                                                     //depending on the bits mux is called to turn on that particular pin

 if(rfbin0==1)               //Set address bit of HT12E
   multiplexer(0,1)  ;
  else
    multiplexer(0,0)  ;

if(rfbin1==1)                 //Set address bit of HT12E
     multiplexer(1,1)   ;
  else
     multiplexer(1,0)   ;

 if(rfbin2==1)               //Set address bit of HT12E
    multiplexer(2,1)    ;
  else
    multiplexer(2,0)    ;

 if(rfbin3==1)                //Set address bit of HT12E
   multiplexer(3,1)   ;
  else
    multiplexer(3,0)    ;

  if(rfbin4==1)             //Set address bit of HT12E
   multiplexer(4,1)    ;
  else
    multiplexer(4,0)    ;

 if(rfbin5==1)                 //Set address bit of HT12E
   multiplexer(5,1)    ;
 else
    multiplexer(5,0)  ;



           //Place Data
 if(d1==1)
  multiplexer(9,1);
 else
  multiplexer(9,0);

 if(d2==1)
  multiplexer(8,1);
 else
  multiplexer(8,0);

 if(d3==1)
  multiplexer(10,1);
 else
  multiplexer(10,0);

 if(d4==1)
  multiplexer(6,1);
 else
  multiplexer(6,0);



                     //Enable transmission
  multiplexer(11,0);

        // transmission enable pin is tied to 11th pin of mux
        Delay_ms(03);

        return ;

;


}

               void interrupt(void)
{




if(INTCON.INT0IF)
{
INTCON.INT0IF = 0;          // clear the interrupt flag
}

if(INTCON.RBIF==1)
   {
   if(PORTB.f7 == 1){
         datas=datas+10;
             }
          garbage=PORTB;
      INTCON.RBIF = 0;
      }
                        return;
 }




void main()
{
TRISA=0b00000000;
ADCON0.ADON=0;
TRISE=0;
TRISE=0b00000000;
TRISC=0;   //Seeting Port c as output
PORTC=0;      //Setting PORT C as zero
TRISD=0;
PORTD.B7=1;


INTCON.TMR0IE=0;
T0CON.TMR0ON=0;
T0CON.T08BIT=1;
T0CON.T0CS=0;
T0CON.PSA=0;
T0CON.T0CS=0;



  CMCON.B3=0;
    CMCON.B2=1;   //mAKING All pins Digital by disabling comparators
    CMCON.B1=1;
    CMCON.B0=1;


 TRISB=0b11111111;               //setting B as input
PORTB=0;
 INTCON.RBIE = 0;                 //Port b interrutpt enable
             //Global Interrupt enable
 //All interrupt turned off
 INTCON.INT0IE= 1;               //enable Interrupt on timer zero
 INTCON.PEIE = 1;                   //Periphral interup enable
 INTCON.GIEL=1;
 INTCON.GIEH=1;
 INTCON.IPEN = 1;

        INTCON.GIE = 1;
 INTCON2.INTEDG0 = 0;              //Interrupt on rising edge of timer2 Clock
 INTCON2.RBIP = 1;
 TRISD=0b00000000;                        //Setting port d as output
 TRISC=0b11000000  ;

     ADCON1.pcfg3=1;
      ADCON1.pcfg2=1;
       ADCON1.pcfg1=1;
        ADCON1.pcfg0=1;



   PORTD.B6 =0;


UART1_Init(9600);
 Delay_ms(100);






    Lcd_Init();
                                    //Some leds are connected to following pins,turning them off and on

                    multiplexer(13,0);
                    multiplexer(12,0);
                    LATD.B7=1;
                    LATD.B4=0;
                    LATD.B5=0;


Lcd_Cmd(_LCD_CLEAR); // Clear display
strConstCpy(string,shoip);
Lcd_Out(1,7,string);

strConstCpy(string,shello);
 Lcd_Out(2,1,string);
 delay_ms(1000);

Lcd_Cmd(_LCD_CLEAR); // Clear display
strConstCpy(string,sinit);
 Lcd_Out(1,1,string);


        // i need some leds to blink in a fashion thats why using off and on
            multiplexer(13,1);
            delay_ms(100);           delay_ms(1000);    refresh=0;
             multiplexer(13,0);

             strConstCpy(string,swifi);
             Lcd_Out(2,1,string);
             multiplexer(12,1);
             delay_ms(1000)                              ;
             refresh=0;
             LATD.B5=1;      //pOWER led
              delay_ms(1000);                   refresh=0;

             strConstCpy(string,smux);
             Lcd_Out(2,1,string);     refresh=0;

             multiplexer(13,1);    //HOME    led
             delay_ms(500);                   refresh=0;
              LATD.B4=1;       //WARNING led
              refresh=0;

              strConstCpy(string,suart);
              Lcd_Out(2,1,string);
              refresh=0;
              delay_ms(1000);
              delay_ms(1000);         refresh=0;
              multiplexer(13,0);    //HOME
              delay_ms(1000);             refresh=0;
               strConstCpy(string,sready);
               Lcd_Out(2,1,string);      refresh=0;
               multiplexer(13,1);    //HOME
               delay_ms(1000);                    refresh=0;

               multiplexer(12,0);       //led1
                    delay_ms(1000);                     refresh=0;
                    LATD.B4=0;       //WARNING

                       while(1)
   //
   {



       sendrf(66,1,1,1,1);

  sendrf(64,1,1,1,1);
    sendrf(65,1,1,1,1);


   sendrf(65,0,0,0,0);
    sendrf(64,0,0,0,0);





                    UART1_Write_Text("H;");


UART1_Read_Text(output, "end", 19);    // reads text until 'OK' is found
        // if data isreceived

                                            refresh=0;

                    multiplexer(12,1);
                      refresh=0;
                          delay_ms(10);
                    multiplexer(12,0);
                                      refresh=0;

                        menu=0;
                        submenu=0;
       datas=datas+1;
   if(menu==0 && submenu==0)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
  strConstCpy(string,shoip);
 Lcd_Out(1,7,string);

 Lcd_Out(2,1,output);
    }
    else
    if(menu==1 && submenu==0)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Out(1,1,"s");
 InttoStr(datas,string);
 Lcd_Out(2,3,string);
    }
        else
     if(menu==1 && submenu==1)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Out(1,1,"s");
 InttoStr(datas,string);
 Lcd_Out(2,3,string);
    }
            else
    if(menu==2 && submenu==0)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Out(1,1,"T");
 InttoStr(datas,string);
 Lcd_Out(2,3,string);
    }
                else
      if(menu==3 && submenu==0)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Out(1,1,"Fo");
 InttoStr(datas,string);
 Lcd_Out(2,3,string);
    }





                  //i AM JUST pasting above codes again to show you the error.. irrespective of the code, when i cross certain limits in program error starts appearing
                        if(menu==2 && submenu==0)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
  strConstCpy(string,shoip);
 Lcd_Out(1,7,string);

 Lcd_Out(2,1,output);
    }
    else
    if(menu==4 && submenu==0)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Out(1,1,"s");
 InttoStr(datas,string);
 Lcd_Out(2,3,string);
    }
        else
     if(menu==6 && submenu==1)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Out(1,1,"s");
 InttoStr(datas,string);
 Lcd_Out(2,3,string);
    }
            else
    if(menu==8 && submenu==0)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Out(1,1,"T");
 InttoStr(datas,string);
 Lcd_Out(2,3,string);
    }
                else
      if(menu==3 && submenu==0)
   {
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Out(1,1,"Fo");
 InttoStr(datas,string);
 Lcd_Out(2,3,string);
    }



      }
   }



I tried in proteus and real hardware too but its giving the same result
 

What is this ?

Code:
char *string[100]="Hello";

Is the above a pointer to array or a string ? Why such large array or array of pointers ? How much RAM your device has ?
 

What is this ?

Code:
char *string[100]="Hello";

Is the above a pointer to array or a string ? Why such large array or array of pointers ? How much RAM your device has ?




Iam just using 27% of My available RAM, 406bytes out of around 1500 bytes

..and FYI,, i removed that line and compiled again.. still the same..

I checked Stack trace in proteus

It goes like this

1.jpg


and i checked asm file in mikroc to the lines corresponding to those addresses

_main:
0x09BA 0xF009EC08 CALL 4624, 0
;MyProject.c,329 :: void main()
;MyProject.c,334 :: TRISA=0b00000000;
0x09BE 0x6A92 CLRF TRISA
;MyProject.c,335 :: ADCON0.ADON=0;
0x09C0 0x90C2 BCF ADCON0, 0
 

char *string[100] is wrong syntax, means array of char pointers rather than array of char (respectively c string), as intended. No idea if it causes problems for mikroC.

How stack overflow occurs can be best checked with debugger.
 

Zip and post your complete mikroC project files and also your proteus file. I will debug it and see what is happening.
 

I removed that line..

And about using debugger, sorry i dont own a genuine microchip Flasher..
But i did check stack trace with proteus.. You can see the image in my previous reply to Okada

- - - Updated - - -

Haii. i have attatched Rar file.. Please check it..

Dont get confused seeing proteus file.. :).. at present i have removed buttons and all from program just to sort out this Overflow bug..

- - - Updated - - -

I tried running debug mode on Mikroc, It stops at main() saying Pc out of scope..
 

Attachments

  • eda new.rar
    92.1 KB · Views: 89

any update??..

Iam actually stuck, i cant figure out whats wrong..
 

The binary code is flawed, don't know if it's a MikroC bug or a wrong compiler configuration.

The first instruction in main which would normally call the initialization of variables, stack setup etc. is jumping to nowhere.
Code:
_main:
 0x09BA 0xF009EC08 CALL 4624, 0 ; 0x1210, no code at this place
 ;MyProject.c,329 :: void main()
 

The binary code is flawed, don't know if it's a MikroC bug or a wrong compiler configuration.

The first instruction in main which would normally call the initialization of variables, stack setup etc. is jumping to nowhere.
Code:
_main:
 0x09BA 0xF009EC08 CALL 4624, 0 ; 0x1210, no code at this place
 ;MyProject.c,329 :: void main()


How can i solve this ?? I tried pasting code to new project.. i tried modifying functions etc.. But its still the same
 

Does other mikroC projects work fine ? If not maybe your mikroC installation is corrupted. Try reinstalling the compiler and then compiling the project after cleaning the project.
 

Does other mikroC projects work fine ? If not maybe your mikroC installation is corrupted. Try reinstalling the compiler and then compiling the project after cleaning the project.



I think when the size crosses certain limit it starts crashing.. Yea i tried with another version too.. 5.6 .. Dont know if my setup file is crashed.. Sorry iam using cracked versions
 

No, the compilier is ok. Code is crap. This is just an example of dirty programming. Impossible to comment or fix anyhow. Just get more experience and write again from zero.
 

Why not buy legit version of mikroC. mikroE is providing 13% discount on all their products till tomorrow. Buy one license for the Compiler or use XC8 free version Compiler.
 

No, the compilier is ok. Code is crap. This is just an example of dirty programming. Impossible to comment or fix anyhow. Just get more experience and write again from zero.
You are probably right regarding code quality. Each time I look into it, I see other severe faults, e.g. GIE=1 inside interrupt function.

But this doesn't explain why the compiler inserts a call to non-existing code location for RTL initialization.
 

I have compiled your project and I see difference in the generated .hex files. Test my .hex file. If it works then Compiler is the problem and solution will be to buy a license.
 

Attachments

  • My Project.rar
    73.7 KB · Views: 76
  • compiler messages.png
    compiler messages.png
    31.6 KB · Views: 83

Which compiler version are you using?

Recompiled, using my good old v.6.0.1 (case sensitive), now the project is working in Proteus, without any stack error.
Attached the whole project and a Proteus screenshot (with a welcome message, created by the USART virtual terminal, on the LCD :)).
 

Attachments

  • eda new_running.rar
    120.8 KB · Views: 70
  • Eda_new_running.gif
    Eda_new_running.gif
    16.2 KB · Views: 79

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top