0
\$\begingroup\$

I used PIC18F452 microcontroller for my project. And I'm using mikroc pro software. My code compiled successfully. But in proteus, it will give the error "Stack overflow is forcing device reset". And I don't get the LCD output.

I have attached the code here:

          // LCD module connections
          sbit LCD_RS at RD4_bit;
          sbit LCD_EN at RD5_bit;
          sbit LCD_D4 at RD0_bit;
          sbit LCD_D5 at RD1_bit;
          sbit LCD_D6 at RD2_bit;
          sbit LCD_D7 at RD3_bit;
          sbit LCD_RS_Direction at TRISD4_bit;
          sbit LCD_EN_Direction at TRISD5_bit;
          sbit LCD_D4_Direction at TRISD0_bit;
          sbit LCD_D5_Direction at TRISD1_bit;
          sbit LCD_D6_Direction at TRISD2_bit;
          sbit LCD_D7_Direction at TRISD3_bit;
          // End LCD module connections

void main() {

          double m,a,voltage,level,noise_level;
          //char *text;
          int l;
          char ch,ch1,ch2,ch3;
          //long tlong;                          //
          int pot_value;

          TRISB = 0x00; // PORTB is output
          TRISC = 0x00; // PORTC is output
          TRISD = 0x00; // PORTD is output

          TRISA = 0xFF;              // All port A pins are configured as inputs
          ADCON0 = 0x81;         //AN0 for analogue input
          ADCON1 = 0xC4;         //voltage at AN3 as Vref

 Lcd_Init(); // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_Out(1,1,"Noise Level");//Write text'Hello World' in first row

 while(1)
          {
              pot_value = ADC_Read(0);
              voltage = pot_value * 5.0/1023.0;
              a=voltage/1.573;

              if(1<=a<10)
              {
                  l=0;
                  ch=a;
              }
             if (0.1<=a<1)
              {
                  l=-1;
                  ch=a*10;
              }
              if (0.01<=a<0.1)
              {
                  l=-2;
                  ch=a*100;
              }
              if (0.001<=a<0.01)
              {
                  l=-3;
                  ch=a*1000;
              }
             if (0.0001<=a<0.001)
              {
                  l=-4;
                  ch=a*10000;
              }

             switch (ch)
              {
  case 1: m=0.0;  break;
  case 2: m=0.39; break;
  case 3: m=0.544; break;
  case 4: m=0.65; break;
  case 5: m=0.74; break;
  case 6: m=0.813; break;
  case 7: m=0.875; break;
  case 8: m=0.929; break;
  case 9: m=0.977; break;
  default: m=0.977;
              }

              level=40*(l+m);
              noise_level=94.0+level;


              ch1=noise_level/10;
              Lcd_Chr(2,2,48+ch1);
              m = (a*10);
              ch2=m-ch1*10;
              Lcd_Chr_CP(48+ch2);
              Lcd_Chr_CP('.');
              m = (a*100);
              ch3=m-(ch1*10+ch2)*10;
              Lcd_Chr_CP(48+ch3);
              Lcd_Out_Cp("dBA");
              Delay_ms(1);
          }

}

And the schematic:

proteus schematic

The error messages:

Error messages

HEX FILE OF PROJECT

And the assembly:

_main:

;MyProject.c,17 ::      void main() {
;MyProject.c,26 ::      TRISB = 0x00; // PORTB is output
    CLRF        TRISB+0 
;MyProject.c,27 ::      TRISC = 0x00; // PORTC is output
    CLRF        TRISC+0 
;MyProject.c,28 ::      TRISD = 0x00; // PORTD is output
    CLRF        TRISD+0 
;MyProject.c,30 ::      TRISA = 0xFF;              // All port A pins are configured as inputs
    MOVLW       255
    MOVWF       TRISA+0 
;MyProject.c,31 ::      ADCON0 = 0x81;         //AN0 for analogue input
    MOVLW       129
    MOVWF       ADCON0+0 
;MyProject.c,32 ::      ADCON1 = 0xC4;         //voltage at AN3 as Vref
    MOVLW       196
    MOVWF       ADCON1+0 
;MyProject.c,34 ::      Lcd_Init(); // Initialize LCD
    CALL        _Lcd_Init+0, 0
;MyProject.c,35 ::      Lcd_Cmd(_LCD_CLEAR); // Clear display
    MOVLW       1
    MOVWF       FARG_Lcd_Cmd_out_char+0 
    CALL        _Lcd_Cmd+0, 0
;MyProject.c,36 ::      Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
    MOVLW       12
    MOVWF       FARG_Lcd_Cmd_out_char+0 
    CALL        _Lcd_Cmd+0, 0
;MyProject.c,37 ::      Lcd_Out(1,1,"Noise Level");//Write text'Hello World' in first row
    MOVLW       1
    MOVWF       FARG_Lcd_Out_row+0 
    MOVLW       1
    MOVWF       FARG_Lcd_Out_column+0 
    MOVLW       ?lstr1_MyProject+0
    MOVWF       FARG_Lcd_Out_text+0 
    MOVLW       hi_addr(?lstr1_MyProject+0)
    MOVWF       FARG_Lcd_Out_text+1 
    CALL        _Lcd_Out+0, 0
;MyProject.c,39 ::      while(1)
L_main0:
;MyProject.c,41 ::      pot_value = ADC_Read(0);
    CLRF        FARG_ADC_Read_channel+0 
    CALL        _ADC_Read+0, 0
;MyProject.c,42 ::      voltage = pot_value * 5.0/1023.0;
    CALL        _Int2Double+0, 0
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       32
    MOVWF       R6 
    MOVLW       129
    MOVWF       R7 
    CALL        _Mul_32x32_FP+0, 0
    MOVLW       0
    MOVWF       R4 
    MOVLW       192
    MOVWF       R5 
    MOVLW       127
    MOVWF       R6 
    MOVLW       136
    MOVWF       R7 
    CALL        _Div_32x32_FP+0, 0
;MyProject.c,43 ::      a=voltage/1.573;
    MOVLW       16
    MOVWF       R4 
    MOVLW       88
    MOVWF       R5 
    MOVLW       73
    MOVWF       R6 
    MOVLW       127
    MOVWF       R7 
    CALL        _Div_32x32_FP+0, 0
    MOVF        R0, 0 
    MOVWF       main_a_L0+0 
    MOVF        R1, 0 
    MOVWF       main_a_L0+1 
    MOVF        R2, 0 
    MOVWF       main_a_L0+2 
    MOVF        R3, 0 
    MOVWF       main_a_L0+3 
;MyProject.c,45 ::      if(1<=a<10)
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       0
    MOVWF       R6 
    MOVLW       127
    MOVWF       R7 
    CALL        _Compare_Double+0, 0
    MOVLW       0
    BTFSC       STATUS+0, 0 
    MOVLW       1
    MOVWF       R0 
    MOVLW       10
    SUBWF       R0, 0 
    BTFSC       STATUS+0, 0 
    GOTO        L_main2
;MyProject.c,47 ::      l=0;
    CLRF        main_l_L0+0 
    CLRF        main_l_L0+1 
;MyProject.c,48 ::      ch=a;
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    CALL        _Double2Byte+0, 0
    MOVF        R0, 0 
    MOVWF       main_ch_L0+0 
;MyProject.c,49 ::      }
L_main2:
;MyProject.c,50 ::      if (0.1<=a<1)
    MOVLW       205
    MOVWF       R4 
    MOVLW       204
    MOVWF       R5 
    MOVLW       76
    MOVWF       R6 
    MOVLW       123
    MOVWF       R7 
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    CALL        _Compare_Double+0, 0
    MOVLW       0
    BTFSC       STATUS+0, 0 
    MOVLW       1
    MOVWF       R0 
    MOVLW       1
    SUBWF       R0, 0 
    BTFSC       STATUS+0, 0 
    GOTO        L_main3
;MyProject.c,52 ::      l=-1;
    MOVLW       255
    MOVWF       main_l_L0+0 
    MOVLW       255
    MOVWF       main_l_L0+1 
;MyProject.c,53 ::      ch=a*10;
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       32
    MOVWF       R6 
    MOVLW       130
    MOVWF       R7 
    CALL        _Mul_32x32_FP+0, 0
    CALL        _Double2Byte+0, 0
    MOVF        R0, 0 
    MOVWF       main_ch_L0+0 
;MyProject.c,54 ::      }
L_main3:
;MyProject.c,55 ::      if (0.01<=a<0.1)
    MOVLW       10
    MOVWF       R4 
    MOVLW       215
    MOVWF       R5 
    MOVLW       35
    MOVWF       R6 
    MOVLW       120
    MOVWF       R7 
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    CALL        _Compare_Double+0, 0
    MOVLW       0
    BTFSC       STATUS+0, 0 
    MOVLW       1
    MOVWF       R0 
    CALL        _Byte2Double+0, 0
    MOVLW       205
    MOVWF       R4 
    MOVLW       204
    MOVWF       R5 
    MOVLW       76
    MOVWF       R6 
    MOVLW       123
    MOVWF       R7 
    CALL        _Compare_Double+0, 0
    MOVLW       1
    BTFSC       STATUS+0, 0 
    MOVLW       0
    MOVWF       R0 
    MOVF        R0, 1 
    BTFSC       STATUS+0, 2 
    GOTO        L_main4
;MyProject.c,57 ::      l=-2;
    MOVLW       254
    MOVWF       main_l_L0+0 
    MOVLW       255
    MOVWF       main_l_L0+1 
;MyProject.c,58 ::      ch=a*100;
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       72
    MOVWF       R6 
    MOVLW       133
    MOVWF       R7 
    CALL        _Mul_32x32_FP+0, 0
    CALL        _Double2Byte+0, 0
    MOVF        R0, 0 
    MOVWF       main_ch_L0+0 
;MyProject.c,59 ::      }
L_main4:
;MyProject.c,60 ::      if (0.001<=a<0.01)
    MOVLW       111
    MOVWF       R4 
    MOVLW       18
    MOVWF       R5 
    MOVLW       3
    MOVWF       R6 
    MOVLW       117
    MOVWF       R7 
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    CALL        _Compare_Double+0, 0
    MOVLW       0
    BTFSC       STATUS+0, 0 
    MOVLW       1
    MOVWF       R0 
    CALL        _Byte2Double+0, 0
    MOVLW       10
    MOVWF       R4 
    MOVLW       215
    MOVWF       R5 
    MOVLW       35
    MOVWF       R6 
    MOVLW       120
    MOVWF       R7 
    CALL        _Compare_Double+0, 0
    MOVLW       1
    BTFSC       STATUS+0, 0 
    MOVLW       0
    MOVWF       R0 
    MOVF        R0, 1 
    BTFSC       STATUS+0, 2 
    GOTO        L_main5
;MyProject.c,62 ::      l=-3;
    MOVLW       253
    MOVWF       main_l_L0+0 
    MOVLW       255
    MOVWF       main_l_L0+1 
;MyProject.c,63 ::      ch=a*1000;
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       122
    MOVWF       R6 
    MOVLW       136
    MOVWF       R7 
    CALL        _Mul_32x32_FP+0, 0
    CALL        _Double2Byte+0, 0
    MOVF        R0, 0 
    MOVWF       main_ch_L0+0 
;MyProject.c,64 ::      }
L_main5:
;MyProject.c,65 ::      if (0.0001<=a<0.001)
    MOVLW       23
    MOVWF       R4 
    MOVLW       183
    MOVWF       R5 
    MOVLW       81
    MOVWF       R6 
    MOVLW       113
    MOVWF       R7 
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    CALL        _Compare_Double+0, 0
    MOVLW       0
    BTFSC       STATUS+0, 0 
    MOVLW       1
    MOVWF       R0 
    CALL        _Byte2Double+0, 0
    MOVLW       111
    MOVWF       R4 
    MOVLW       18
    MOVWF       R5 
    MOVLW       3
    MOVWF       R6 
    MOVLW       117
    MOVWF       R7 
    CALL        _Compare_Double+0, 0
    MOVLW       1
    BTFSC       STATUS+0, 0 
    MOVLW       0
    MOVWF       R0 
    MOVF        R0, 1 
    BTFSC       STATUS+0, 2 
    GOTO        L_main6
;MyProject.c,67 ::      l=-4;
    MOVLW       252
    MOVWF       main_l_L0+0 
    MOVLW       255
    MOVWF       main_l_L0+1 
;MyProject.c,68 ::      ch=a*10000;
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    MOVLW       0
    MOVWF       R4 
    MOVLW       64
    MOVWF       R5 
    MOVLW       28
    MOVWF       R6 
    MOVLW       140
    MOVWF       R7 
    CALL        _Mul_32x32_FP+0, 0
    CALL        _Double2Byte+0, 0
    MOVF        R0, 0 
    MOVWF       main_ch_L0+0 
;MyProject.c,69 ::      }
L_main6:
;MyProject.c,71 ::      switch (ch)
    GOTO        L_main7
;MyProject.c,73 ::      case 1: m=0.0;  break;
L_main9:
    CLRF        main_m_L0+0 
    CLRF        main_m_L0+1 
    CLRF        main_m_L0+2 
    CLRF        main_m_L0+3 
    GOTO        L_main8
;MyProject.c,74 ::      case 2: m=0.39; break;
L_main10:
    MOVLW       20
    MOVWF       main_m_L0+0 
    MOVLW       174
    MOVWF       main_m_L0+1 
    MOVLW       71
    MOVWF       main_m_L0+2 
    MOVLW       125
    MOVWF       main_m_L0+3 
    GOTO        L_main8
;MyProject.c,75 ::      case 3: m=0.544; break;
L_main11:
    MOVLW       150
    MOVWF       main_m_L0+0 
    MOVLW       67
    MOVWF       main_m_L0+1 
    MOVLW       11
    MOVWF       main_m_L0+2 
    MOVLW       126
    MOVWF       main_m_L0+3 
    GOTO        L_main8
;MyProject.c,76 ::      case 4: m=0.65; break;
L_main12:
    MOVLW       102
    MOVWF       main_m_L0+0 
    MOVLW       102
    MOVWF       main_m_L0+1 
    MOVLW       38
    MOVWF       main_m_L0+2 
    MOVLW       126
    MOVWF       main_m_L0+3 
    GOTO        L_main8
;MyProject.c,77 ::      case 5: m=0.74; break;
L_main13:
    MOVLW       164
    MOVWF       main_m_L0+0 
    MOVLW       112
    MOVWF       main_m_L0+1 
    MOVLW       61
    MOVWF       main_m_L0+2 
    MOVLW       126
    MOVWF       main_m_L0+3 
    GOTO        L_main8
;MyProject.c,78 ::      case 6: m=0.813; break;
L_main14:
    MOVLW       197
    MOVWF       main_m_L0+0 
    MOVLW       32
    MOVWF       main_m_L0+1 
    MOVLW       80
    MOVWF       main_m_L0+2 
    MOVLW       126
    MOVWF       main_m_L0+3 
    GOTO        L_main8
;MyProject.c,79 ::      case 7: m=0.875; break;
L_main15:
    MOVLW       0
    MOVWF       main_m_L0+0 
    MOVLW       0
    MOVWF       main_m_L0+1 
    MOVLW       96
    MOVWF       main_m_L0+2 
    MOVLW       126
    MOVWF       main_m_L0+3 
    GOTO        L_main8
;MyProject.c,80 ::      case 8: m=0.929; break;
L_main16:
    MOVLW       242
    MOVWF       main_m_L0+0 
    MOVLW       210
    MOVWF       main_m_L0+1 
    MOVLW       109
    MOVWF       main_m_L0+2 
    MOVLW       126
    MOVWF       main_m_L0+3 
    GOTO        L_main8
;MyProject.c,81 ::      case 9: m=0.977; break;
L_main17:
    MOVLW       172
    MOVWF       main_m_L0+0 
    MOVLW       28
    MOVWF       main_m_L0+1 
    MOVLW       122
    MOVWF       main_m_L0+2 
    MOVLW       126
    MOVWF       main_m_L0+3 
    GOTO        L_main8
;MyProject.c,82 ::      default: m=0.977;
L_main18:
    MOVLW       172
    MOVWF       main_m_L0+0 
    MOVLW       28
    MOVWF       main_m_L0+1 
    MOVLW       122
    MOVWF       main_m_L0+2 
    MOVLW       126
    MOVWF       main_m_L0+3 
;MyProject.c,83 ::      }
    GOTO        L_main8
L_main7:
    MOVF        main_ch_L0+0, 0 
    XORLW       1
    BTFSC       STATUS+0, 2 
    GOTO        L_main9
    MOVF        main_ch_L0+0, 0 
    XORLW       2
    BTFSC       STATUS+0, 2 
    GOTO        L_main10
    MOVF        main_ch_L0+0, 0 
    XORLW       3
    BTFSC       STATUS+0, 2 
    GOTO        L_main11
    MOVF        main_ch_L0+0, 0 
    XORLW       4
    BTFSC       STATUS+0, 2 
    GOTO        L_main12
    MOVF        main_ch_L0+0, 0 
    XORLW       5
    BTFSC       STATUS+0, 2 
    GOTO        L_main13
    MOVF        main_ch_L0+0, 0 
    XORLW       6
    BTFSC       STATUS+0, 2 
    GOTO        L_main14
    MOVF        main_ch_L0+0, 0 
    XORLW       7
    BTFSC       STATUS+0, 2 
    GOTO        L_main15
    MOVF        main_ch_L0+0, 0 
    XORLW       8
    BTFSC       STATUS+0, 2 
    GOTO        L_main16
    MOVF        main_ch_L0+0, 0 
    XORLW       9
    BTFSC       STATUS+0, 2 
    GOTO        L_main17
    GOTO        L_main18
L_main8:
;MyProject.c,85 ::      level=40*(l+m);
    MOVF        main_l_L0+0, 0 
    MOVWF       R0 
    MOVF        main_l_L0+1, 0 
    MOVWF       R1 
    CALL        _Int2Double+0, 0
    MOVF        main_m_L0+0, 0 
    MOVWF       R4 
    MOVF        main_m_L0+1, 0 
    MOVWF       R5 
    MOVF        main_m_L0+2, 0 
    MOVWF       R6 
    MOVF        main_m_L0+3, 0 
    MOVWF       R7 
    CALL        _Add_32x32_FP+0, 0
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       32
    MOVWF       R6 
    MOVLW       132
    MOVWF       R7 
    CALL        _Mul_32x32_FP+0, 0
;MyProject.c,86 ::      noise_level=94.0+level;
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       60
    MOVWF       R6 
    MOVLW       133
    MOVWF       R7 
    CALL        _Add_32x32_FP+0, 0
;MyProject.c,89 ::      ch1=noise_level/10;
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       32
    MOVWF       R6 
    MOVLW       130
    MOVWF       R7 
    CALL        _Div_32x32_FP+0, 0
    CALL        _Double2Byte+0, 0
    MOVF        R0, 0 
    MOVWF       main_ch1_L0+0 
;MyProject.c,90 ::      Lcd_Chr(2,2,48+ch1);
    MOVLW       2
    MOVWF       FARG_Lcd_Chr_row+0 
    MOVLW       2
    MOVWF       FARG_Lcd_Chr_column+0 
    MOVF        R0, 0 
    ADDLW       48
    MOVWF       FARG_Lcd_Chr_out_char+0 
    CALL        _Lcd_Chr+0, 0
;MyProject.c,91 ::      m = (a*10);
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       32
    MOVWF       R6 
    MOVLW       130
    MOVWF       R7 
    CALL        _Mul_32x32_FP+0, 0
    MOVF        R0, 0 
    MOVWF       main_m_L0+0 
    MOVF        R1, 0 
    MOVWF       main_m_L0+1 
    MOVF        R2, 0 
    MOVWF       main_m_L0+2 
    MOVF        R3, 0 
    MOVWF       main_m_L0+3 
;MyProject.c,92 ::      ch2=m-ch1*10;
    MOVLW       10
    MULWF       main_ch1_L0+0 
    MOVF        PRODL+0, 0 
    MOVWF       R0 
    MOVF        PRODH+0, 0 
    MOVWF       R1 
    CALL        _Int2Double+0, 0
    MOVF        R0, 0 
    MOVWF       R4 
    MOVF        R1, 0 
    MOVWF       R5 
    MOVF        R2, 0 
    MOVWF       R6 
    MOVF        R3, 0 
    MOVWF       R7 
    MOVF        main_m_L0+0, 0 
    MOVWF       R0 
    MOVF        main_m_L0+1, 0 
    MOVWF       R1 
    MOVF        main_m_L0+2, 0 
    MOVWF       R2 
    MOVF        main_m_L0+3, 0 
    MOVWF       R3 
    CALL        _Sub_32x32_FP+0, 0
    CALL        _Double2Byte+0, 0
    MOVF        R0, 0 
    MOVWF       main_ch2_L0+0 
;MyProject.c,93 ::      Lcd_Chr_CP(48+ch2);
    MOVF        R0, 0 
    ADDLW       48
    MOVWF       FARG_Lcd_Chr_CP_out_char+0 
    CALL        _Lcd_Chr_CP+0, 0
;MyProject.c,94 ::      Lcd_Chr_CP('.');
    MOVLW       46
    MOVWF       FARG_Lcd_Chr_CP_out_char+0 
    CALL        _Lcd_Chr_CP+0, 0
;MyProject.c,95 ::      m = (a*100);
    MOVF        main_a_L0+0, 0 
    MOVWF       R0 
    MOVF        main_a_L0+1, 0 
    MOVWF       R1 
    MOVF        main_a_L0+2, 0 
    MOVWF       R2 
    MOVF        main_a_L0+3, 0 
    MOVWF       R3 
    MOVLW       0
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    MOVLW       72
    MOVWF       R6 
    MOVLW       133
    MOVWF       R7 
    CALL        _Mul_32x32_FP+0, 0
    MOVF        R0, 0 
    MOVWF       main_m_L0+0 
    MOVF        R1, 0 
    MOVWF       main_m_L0+1 
    MOVF        R2, 0 
    MOVWF       main_m_L0+2 
    MOVF        R3, 0 
    MOVWF       main_m_L0+3 
;MyProject.c,96 ::      ch3=m-(ch1*10+ch2)*10;
    MOVLW       10
    MULWF       main_ch1_L0+0 
    MOVF        PRODL+0, 0 
    MOVWF       R0 
    MOVF        PRODH+0, 0 
    MOVWF       R1 
    MOVF        main_ch2_L0+0, 0 
    ADDWF       R0, 1 
    MOVLW       0
    ADDWFC      R1, 1 
    MOVLW       10
    MOVWF       R4 
    MOVLW       0
    MOVWF       R5 
    CALL        _Mul_16x16_U+0, 0
    CALL        _Int2Double+0, 0
    MOVF        R0, 0 
    MOVWF       R4 
    MOVF        R1, 0 
    MOVWF       R5 
    MOVF        R2, 0 
    MOVWF       R6 
    MOVF        R3, 0 
    MOVWF       R7 
    MOVF        main_m_L0+0, 0 
    MOVWF       R0 
    MOVF        main_m_L0+1, 0 
    MOVWF       R1 
    MOVF        main_m_L0+2, 0 
    MOVWF       R2 
    MOVF        main_m_L0+3, 0 
    MOVWF       R3 
    CALL        _Sub_32x32_FP+0, 0
    CALL        _Double2Byte+0, 0
;MyProject.c,97 ::      Lcd_Chr_CP(48+ch3);
    MOVF        R0, 0 
    ADDLW       48
    MOVWF       FARG_Lcd_Chr_CP_out_char+0 
    CALL        _Lcd_Chr_CP+0, 0
;MyProject.c,98 ::      Lcd_Out_Cp("dBA");
    MOVLW       ?lstr2_MyProject+0
    MOVWF       FARG_Lcd_Out_CP_text+0 
    MOVLW       hi_addr(?lstr2_MyProject+0)
    MOVWF       FARG_Lcd_Out_CP_text+1 
    CALL        _Lcd_Out_CP+0, 0
;MyProject.c,99 ::      Delay_ms(1);
    MOVLW       3
    MOVWF       R12, 0
    MOVLW       151
    MOVWF       R13, 0
L_main19:
    DECFSZ      R13, 1, 1
    BRA         L_main19
    DECFSZ      R12, 1, 1
    BRA         L_main19
    NOP
    NOP
;MyProject.c,100 ::         }
    GOTO        L_main0
;MyProject.c,102 ::         }
L_end_main:
    GOTO        $+0
; end of _main
\$\endgroup\$
  • \$\begingroup\$ Could you also post the generated assembly code? And please post it inline (indented with a tab or 4 spaces). Thank you! \$\endgroup\$ – user17592 Sep 12 '14 at 7:24
  • \$\begingroup\$ Thank you sir..I have attached the hex file link in yhe bottom of my post. \$\endgroup\$ – channa Sep 12 '14 at 8:00
  • \$\begingroup\$ Could you please post the assembly code? Normally, the C compiler generates assembly in an .asm file, and then from that makes the hex. The .asm file is plain-text, so please post it inline and do not put it on mediafire or something like that. Thank you. \$\endgroup\$ – user17592 Sep 12 '14 at 8:02
  • \$\begingroup\$ Sir, there are lot more lines in .asm file.So it get very difficult to post manually line by line. So I will attached in mediafire. \$\endgroup\$ – channa Sep 12 '14 at 8:21
  • 2
    \$\begingroup\$ Not sure if it's related but those bits of code like if (0.01<=a<0.1) won't do what you're thinking they will. It should be something like if (0.01 <= a && a < 0.1) \$\endgroup\$ – PeterJ Sep 12 '14 at 8:22
2
\$\begingroup\$

There is certainly something "up" with that hex file.

Running in the MPLAB-X simulator, step by step, the sequence I am getting is:

0x0000 GOTO 0xE5C
0x0E5C CALL 0x1376,0
0x1376 NOP
0x1378 NOP
0x137A NOP
... etc to the end of the chip ...
... PC loops at 0xFFFE to 0x0000
0x0000 GOTO 0xE5C
0x0E5C CALL 0x1376,0
0x1376 NOP
0x1378 NOP
0x137A NOP
... etc to the end of the chip ...
... PC loops at 0xFFFE to 0x0000
0x0000 GOTO 0xE5C
0x0E5C CALL 0x1376,0
0x1376 NOP
0x1378 NOP
0x137A NOP
... etc to the end of the chip ...
... PC loops at 0xFFFE to 0x0000

So yes, the result is a stack overflow as there is a repeated CALL with no RETURN.

What is causing it? No idea. The compiler is telling the program to start from the wrong address, or to call the crt0 routines at the wrong address.

So why is it saying the overflow happened at 0xE60 not 0xE5C? Simply this:

  1. The call is attempted. Whether it succeeds or not the PC advances to the next instruction at the end of the call.
  2. The exception happens after it has failed.
  3. The call instruction is a 2 word instruction. It occupies addresses 0xE5C and 0xE5E, so the PC will have advanced to 0xE60 by the time the exception is raised.

If you think about it:

  1. PC = 0xE5C. Read the instruction, advance PC (now 0xE5E).
  2. PC = 0xE5E. Read the parameter (0), advance PC (now 0xE60).
  3. Push PC onto the stack
  4. Stack push fails - stack full.
  5. Report exception. PC is 0xE60.

In normal operation it would be:

  1. PC = 0xE5C. Read the instruction, advance PC (now 0xE5E).
  2. PC = 0xE5E. Read the parameter (0), advance PC (now 0xE60).
  3. Push PC onto the stack
  4. Set PC to target address
  5. Execute target code
  6. Pop PC from stack
  7. Continue from after CALL instruction

So 0xE60 is what the PC was when it had found out that it had failed, which isn't the address of the instruction that caused it, but the address of the instruction after the one that caused it. That is, the address of the instruction it would have continued at after the CALL had returned.

So now we know what is happening, we have to find out why it's happening.

There are two basic reasons:

  1. The compiler is not linking in the crt0 code (the code that initializes everything like variables etc).
  2. The compiler is linking the crt0 into the wrong location, or is providing the wrong location for where it should be calling.

The most likely culprit (if you are using one) is the linker script that defines what gets put where in memory. The default compiler linker scripts should be all that is required. You would only really want to fiddle with linker scripts if you're working a special environment, such as a chip with a bootloader that requires code to be placed in a different location.

However, there could be many other reasons behind it. To help you out we need a little more information.

  1. Please post your entire program so we can compile it ourselves. Also include any links to third party libraries or include files you may be using.
  2. If possible please post a trace of the commands and their parameters executed during compilation. This will enable us to determine if something out of the ordinary is going on during the compilation process.
\$\endgroup\$
  • \$\begingroup\$ Why the downvote? \$\endgroup\$ – Majenko Sep 12 '14 at 18:43
  • \$\begingroup\$ Because this could've been a comment. But also, if you look at the errors, the PC is 0xE60 when the overflow occurs. According to this answer, the PC should be 0xE5C. I still think we didn't get the right hex or the right error message from the OP, and you are right that the error is here; but anyway this post doesn't provide an explanation for the question of the OP. I'd prefer to get the right facts from the OP first, and then start writing answers. \$\endgroup\$ – user17592 Sep 12 '14 at 19:20
  • \$\begingroup\$ a: try and fit all that in a comment. b: the overflow happens after the call fails. The PC advances as part of the call failing, and the trap then happens. Once the PC has advanced it is at 0xE60. \$\endgroup\$ – Majenko Sep 12 '14 at 19:53
  • \$\begingroup\$ The PC advances two positions? If you say so, I never heard of this before. But this needs to be written in the post in my opinion. \$\endgroup\$ – user17592 Sep 12 '14 at 19:55
  • \$\begingroup\$ No, the PC advances to the next instruction. Call is a 2 word instruction - it occupies 0xE5C and 0xE5E. I have elaborated in the answer. \$\endgroup\$ – Majenko Sep 12 '14 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.