+ Post New Thread
Results 1 to 16 of 16
  1. #1
    Full Member level 2
    Points: 1,113, Level: 7

    Join Date
    Dec 2014
    Posts
    147
    Helped
    3 / 3
    Points
    1,113
    Level
    7

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

    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

  2. #2
    Banned
    Points: 5,417, Level: 17

    Join Date
    Jun 2016
    Posts
    1,159
    Helped
    126 / 126
    Points
    5,417
    Level
    17

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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 ?



    •   AltAdvertisment

        
       

  3. #3
    Full Member level 2
    Points: 1,113, Level: 7

    Join Date
    Dec 2014
    Posts
    147
    Helped
    3 / 3
    Points
    1,113
    Level
    7

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    Quote Originally Posted by Okada View Post
    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

    Click image for larger version. 

Name:	1.jpg 
Views:	8 
Size:	167.9 KB 
ID:	133171


    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



  4. #4
    Super Moderator
    Points: 251,556, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,806
    Helped
    13321 / 13321
    Points
    251,556
    Level
    100

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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.



  5. #5
    Banned
    Points: 5,417, Level: 17

    Join Date
    Jun 2016
    Posts
    1,159
    Helped
    126 / 126
    Points
    5,417
    Level
    17

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

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



  6. #6
    Full Member level 2
    Points: 1,113, Level: 7

    Join Date
    Dec 2014
    Posts
    147
    Helped
    3 / 3
    Points
    1,113
    Level
    7

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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..



    •   AltAdvertisment

        
       

  7. #7
    Full Member level 2
    Points: 1,113, Level: 7

    Join Date
    Dec 2014
    Posts
    147
    Helped
    3 / 3
    Points
    1,113
    Level
    7

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    any update??..

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



    •   AltAdvertisment

        
       

  8. #8
    Super Moderator
    Points: 251,556, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,806
    Helped
    13321 / 13321
    Points
    251,556
    Level
    100

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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()



  9. #9
    Full Member level 2
    Points: 1,113, Level: 7

    Join Date
    Dec 2014
    Posts
    147
    Helped
    3 / 3
    Points
    1,113
    Level
    7

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    Quote Originally Posted by FvM View Post
    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



  10. #10
    Banned
    Points: 5,417, Level: 17

    Join Date
    Jun 2016
    Posts
    1,159
    Helped
    126 / 126
    Points
    5,417
    Level
    17

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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.



  11. #11
    Full Member level 2
    Points: 1,113, Level: 7

    Join Date
    Dec 2014
    Posts
    147
    Helped
    3 / 3
    Points
    1,113
    Level
    7

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    Quote Originally Posted by Okada View Post
    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



  12. #12
    Advanced Member level 5
    Points: 8,876, Level: 22
    Achievements:
    7 years registered
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,568
    Helped
    359 / 359
    Points
    8,876
    Level
    22

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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.
    Love me or hate me. All infractions will be ignored.



  13. #13
    Banned
    Points: 5,417, Level: 17

    Join Date
    Jun 2016
    Posts
    1,159
    Helped
    126 / 126
    Points
    5,417
    Level
    17

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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.



  14. #14
    Super Moderator
    Points: 251,556, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    43,806
    Helped
    13321 / 13321
    Points
    251,556
    Level
    100

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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.



    •   AltAdvertisment

        
       

  15. #15
    Banned
    Points: 5,417, Level: 17

    Join Date
    Jun 2016
    Posts
    1,159
    Helped
    126 / 126
    Points
    5,417
    Level
    17

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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.



  16. #16
    Advanced Member level 1
    Points: 5,693, Level: 17

    Join Date
    Jul 2004
    Location
    Hungary
    Posts
    476
    Helped
    189 / 189
    Points
    5,693
    Level
    17

    Re: Mikroc Pic stack overflow, but my program has very less nested calls

    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 ).



--[[ ]]--