My project in MicroC not work in Proteus

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
p3tzata_
Posts: 3
Joined: 18 Aug 2016 14:06

My project in MicroC not work in Proteus

#1 Post by p3tzata_ » 18 Aug 2016 14:35

Hi everyone,
I'm new in mikroC although I'm trying to develop microcontroller for my homemade pellet burner.
I compile my code (with MikroC v.6.0.0) without any errors.
But When I test .hex file in Proteus there is nothing happening in case when I use PIC18F4620.
In another case when I use PIC16F877A there is error: [PIC16 CORE] PC=0x094C. Stack underflow executing RETLW instruction. [U1]

When I comment some random block in my code, simulation pass without any errors.
My code is below.
Am I doing something wrong?
Thank you in advance.

Code: Select all



//************************** LCD variables ********************
sbit LCD_D7 at RB2_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_RS at RB7_bit;

sbit LCD_D7_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_RS_Direction at TRISB7_bit;

//*************************** Relay Coil and Buttons *********************

#define Relay_Fire  PORTD.F4
#define Relay_MC  PORTD.F5
#define Relay_Fan_C PORTD.F6
#define Relay_Fan_N PORTD.F7

#define Button_Setup PORTC.F0
#define Button_Decr PORTC.F1
#define Button_Incr PORTC.F2

//************************temperature variables****************


unsigned int val; //ADC Value
unsigned int t;      //Temperature
char display[16]="";
unsigned int result;
int volt,temp;


int first_loop=1;
int cleaning_sec;
int cleaning_read_pin;


unsigned char* print_mode;
unsigned char* print_lcd;
int count_clean;
char temper[17];
//unsigned char* temper;

char temper1[17];
int i;

int coil_operate=0;
//1 - Normal fan
//2 - Cleaning fan

//5 Volts.
int coil_normal_up=0;
int coil_normal_down=0;
int cnst_coil_normal_up=0;
int cnst_coil_normal_down=0;

//12 Volts.
int coil_clean_up=0;
int cnst_coil_clean_up=0;

//12 Volts. Motor Curve
int mc_coil_operate=0;
int mc_coil_normal_up=0;
int mc_coil_normal_down=0;
int cnst_mc_coil_normal_up=0;
int cnst_mc_coil_normal_down=0;


int status_loop_mc_operate_end=1;

int loop_end_led_operate=0;
int loop_end_led_temp=0;

int tmp_var_eeprom=0;





void operate_EEPROM(int address,unsigned char* text,int start_num,int direction, int min_1, int min_2, int max_1, int max_2)
{
char temper1[10];

             tmp_var_eeprom=EEPROM_Read(address);
             
             if (direction==0) {
             tmp_var_eeprom=tmp_var_eeprom-2;
             }
              
             if (direction==1) {
             tmp_var_eeprom=tmp_var_eeprom+2;
             }

             if (tmp_var_eeprom<=min_1) {tmp_var_eeprom=min_2;} //Min
             if (tmp_var_eeprom>=max_1) {tmp_var_eeprom=max_2;} //Max
            
             EEPROM_Write(address, tmp_var_eeprom);
             tmp_var_eeprom=tmp_var_eeprom/2;
             inttostr(tmp_var_eeprom, temper1) ;
             Lcd_Cmd(_LCD_CLEAR);
             lcd_out(1,1,"Setup MODE:");
             lcd_out(2,1,text);
             lcd_out(2,start_num,Ltrim(temper1));

}




void READ_temp(void)
{

result=adc_read(4);
volt=result*4.88;
temp=volt/10;
//temp=temp+5;
temp = temp + (EEPROM_Read(0x02)/2);

}

void wait_40(void) {

Delay_ms(40);
}

void coil_reset(void)
{
   Relay_Fan_C=0;
   Relay_MC=0;  //NEW entry
   Relay_Fan_N=0;
}


void LCD_display_Normal_Mode(void) {
//char *tempC = "000.0";
//temp = temp%10 ;

//floattostr(temp,display);
inttostr(temp,display);

Lcd_Cmd(_LCD_CLEAR);
Lcd_out(1,1,"Normal Mode:");
lcd_out(2,1,Ltrim(display));
//lcd_out(2,4," ");
Lcd_Chr_Cp(0xdf);
Lcd_Chr_Cp('C');
lcd_out(2,6," ");
//lcd_out(2,7,print_mode);

}


void set_temperature_var_EEPROM(int addr_no,int addr_min_t,int addr_max_t,int addr_fan_on, int addr_fan_off, int addr_fan_clean, int addr_mc) {
int lv_addr_min_t=EEPROM_Read(addr_min_t);
int lv_addr_max_t=EEPROM_Read(addr_max_t);

    if (temp>lv_addr_min_t && temp<=lv_addr_max_t) {

     //Normal Fan
    // print_mode="(6:0)";
     
     coil_normal_up=EEPROM_Read(addr_fan_on)*120;
     if (EEPROM_Read(addr_fan_off)==0){
     coil_normal_down=-1;
     }
     else {
     coil_normal_down=EEPROM_Read(addr_fan_off)*120;
     }

     //Cleaing Fan
     coil_clean_up=EEPROM_Read(addr_fan_clean);
     
     //Motor Curve
     cnst_mc_coil_normal_up=EEPROM_Read(addr_mc);
     cnst_mc_coil_normal_down=120-cnst_mc_coil_normal_up;
      print_mode=print_mode=EEPROM_Read(addr_min_t) ;


}

 //

}



void set_temperature_var(void) {

    //6:0
     if (temp>0 && temp<=60) {
     //Normal Fan
     print_mode="(6:0)";
     coil_normal_up=720;
     coil_normal_down=-1;
     //Cleaing Fan
     //  blink_yellow_led_up=8;
     coil_clean_up=8;
     //Motor Curve
     cnst_mc_coil_normal_up=40;
     cnst_mc_coil_normal_down=80;
     }

    
     //3:3
     if (temp>61 && temp<=63) {
     //Normal
      print_mode="(3:3)";
     coil_normal_up=360;
     coil_normal_down=360;
     //Cleaing

     coil_clean_up=8;
     //Motor Curve
     cnst_mc_coil_normal_up=40;
    cnst_mc_coil_normal_down=80;

     }


    //1:5
    if (temp>64 && temp<=99) {
     //Normal
     print_mode="(1:5)";
     coil_normal_up=120;
     coil_normal_down=600;
     //Cleaing

     coil_clean_up=8;
     //Motor Curve
     cnst_mc_coil_normal_up=40;
     cnst_mc_coil_normal_down=80;
    }

    
    //Reading and Replace varable from EEPROM
    cnst_mc_coil_normal_up=EEPROM_Read(0x00);
    cnst_mc_coil_normal_down=120-cnst_mc_coil_normal_up;

    coil_clean_up=EEPROM_Read(0x01);


    LCD_display_Normal_Mode();


}


void kernel(void) {

    //Read temperature when Normal Fan Loop is finished
    if (coil_normal_up==0 && coil_normal_down==0 )
        {
           coil_reset();

           READ_temp();
           //set_temperature_var();
           //set_temperature_var_EEPROM(int addr_no,int addr_min_t,int addr_max_t,int addr_fan_on, int addr_fan_off, int addr_fan_clean, int addr_mc)

           set_temperature_var_EEPROM(0x10,0x11,0x12,0x13, 0x14,0x15, 0x16);
           set_temperature_var_EEPROM(0x20,0x21,0x22,0x23, 0x24,0x25, 0x26);
           set_temperature_var_EEPROM(0x30,0x31,0x32,0x33, 0x34,0x35, 0x36);
           //Àêî å ïîâå÷å îò 99 êàêâî ñòàâà????
           LCD_display_Normal_Mode();
            
           coil_operate=2;

        }

   //Switch to Normal Fan Loop when Cleaning is finished
   if (coil_clean_up==0 && coil_operate==2) {

          coil_operate=1;

          
          mc_coil_operate=1;
          mc_coil_normal_up=0;
          mc_coil_normal_down=0;
          
          coil_reset();

      }
   
   // Break Motor Curve Loop when Normal Fan is OFF
   if (coil_normal_up==0)
   {
     mc_coil_operate=0;
     mc_coil_normal_up=0;
     mc_coil_normal_down=0;
   }
   



   //Keeping Motor Curve Loop
  if ( mc_coil_operate==1) {
   if (status_loop_mc_operate_end==1 )
           {
                mc_coil_normal_up=cnst_mc_coil_normal_up;
                mc_coil_normal_down=cnst_mc_coil_normal_down;
           }

   }

}














 void operate_coil(void){

       if (coil_operate==1) {
 

                if (coil_normal_up>0){
                    Relay_Fan_N=1;
                    coil_normal_up=coil_normal_up-1;
                    return;
                    }

                if (coil_normal_down>0){
                    Relay_Fan_N=0;
                    coil_normal_down=coil_normal_down-1;
                    return;
                    }
                if  (coil_normal_down==-1) {
                Relay_Fan_N=0;
                coil_normal_down=0;
                return;
                }
                    
                    

       }

        if (coil_operate==2) {

           if (coil_clean_up>0){
              Relay_Fan_C=1;
              coil_clean_up=coil_clean_up-1;
              return;
             }

         }

}

  void mc_operate_coil(void){

       if (mc_coil_operate==1) {


                if (mc_coil_normal_up>0){
                status_loop_mc_operate_end=0;
                    Relay_MC=1;
                    mc_coil_normal_up=mc_coil_normal_up-1;
                    return;
                    }

                if (mc_coil_normal_down>0){
                    Relay_MC=0;
                    mc_coil_normal_down=mc_coil_normal_down-1;
                
                    if (mc_coil_normal_down==0)
                    {status_loop_mc_operate_end=1;}
                    return;
                    }

       }
}

void Button_Setup_Wait_Release(void) {
 while (Button_Setup == 1)
   {
   wait_40();
   }
}

void Button_Decr_Wait_Release(void) {
 while (Button_Decr == 1)
   {
   wait_40();
   }
}

void Button_Incr_Wait_Release(void) {
 while (Button_Incr == 1)
   {
   wait_40();
   }
}

void  operate_EEPROM_Unit(int address,unsigned char* text,int start_num)  {
   Button_Setup_Wait_Release();
    operate_EEPROM(address,text,start_num,999,0,2,118,118);
     while(Button_Setup == 0) //Loop for cofigure Motor Curve Seconds
     {

     if (Button_Decr == 1) { //Button for decrament

         //Decrement paramet
         operate_EEPROM(address,text,start_num,0,0,2,118,118);

         //Wait untill DecrButton is release
         Button_Decr_Wait_Release();
     }

     if (Button_Incr == 1) {//Button for Increment Motor Curve

       //Increment paramet
       operate_EEPROM(address,text,start_num,1,0,2,118,118);

       //Wait untill IncrButton is release
       Button_Incr_Wait_Release();
     }


     wait_40();
     }


}



void configure_params(void){


   if (Button_Setup == 1 ) //SETButton is pushed
   {

     coil_reset();
     operate_EEPROM_Unit(0x15,"(3)CL: ",8);
     operate_EEPROM_Unit(0x16,"(3)MC: ",8);
     operate_EEPROM_Unit(0x25,"(2)CL: ",8);
     operate_EEPROM_Unit(0x26,"(2)MC: ",8);
     operate_EEPROM_Unit(0x35,"(1)CL: ",8);
     operate_EEPROM_Unit(0x36,"(1)MC: ",8);

     operate_EEPROM_Unit(0x02,"Tdelta: ",8);


    /*
   //Wait until SETButton is release
   Button_Setup_Wait_Release();

     //Reset Kernel
      Lcd_Cmd(_LCD_CLEAR);
      lcd_out(1,1,"Must RESTART...");
      wait_40();
      //To continue must restart

     */

  }
///////
}

void main()
{
ADC_Init();
Lcd_Init(); // Initialize LCD
lcd_cmd(_LCD_CURSOR_OFF);

PORTD = 0;      // Initialize PORTB
TRISD = 0;         // Configure PORTB as output

//PORTB = 0;      // Initialize PORTB
//TRISB = 0;         // Configure PORTB as output



TRISA.RA4=1;
TRISC.RA0 = 1;
TRISC.RA1 = 1;
TRISC.RA2 = 1;

Lcd_Cmd(_LCD_CLEAR);
Lcd_out(1,1,"Initialize:");

Relay_Fire=1 ;
Delay_ms(1000);
Relay_Fire=0;
Delay_ms(1000);
Relay_MC=1;
Delay_ms(1000);
Relay_MC=0;
Delay_ms(1000);
Relay_Fan_C=1;
Delay_ms(1000);
Relay_Fan_C=0;
Delay_ms(1000);
Relay_Fan_N=1;
Delay_ms(1000);
Relay_Fan_N=0;
Delay_ms(1000);

Lcd_Cmd(_LCD_CLEAR);
Lcd_out(1,1,"Test LCD:");

while(1)
{

   configure_params();
   kernel();
   operate_coil();
   mc_operate_coil();

   Delay_ms(500);
   
   
}


}

User avatar
lana.arsic
mikroElektronika team
Posts: 1715
Joined: 15 Jan 2016 12:50

Re: My project in MicroC not work in Proteus

#2 Post by lana.arsic » 19 Aug 2016 09:58

Hi p3tzata_,

Welcome to the MikroE forum.

Proteus is not fully certified software by MikroElektronika on which you can test your code,
so it is out of scope of our technical support. Thank you for understanding.

I suggest you, when you are using PIC18F4620, use LAT registers for the output.

Best regards,
Lana

diode_blade
Posts: 172
Joined: 24 Aug 2014 17:55
Location: Sheffield

Re: My project in MicroC not work in Proteus

#3 Post by diode_blade » 19 Aug 2016 11:25

Hi p3tzata,
Proteus might be a very nice software package for PCB and Schematic design but I would certainly not use it for testing PIC code, for one it cannot simulate that actual hardware of the PIC, if you have a developement board, a programmer/ICD debugger then that is all you need to test your software and the PIC you are going to use, I have made before in the past my own PIC boards with the ability to plug in a programmer/debugger to the board so I can test my software and hardware bit by bit and debug it. Now I use my Easypic v7 board and click boards or make my own click boards to do design and testing.

I always test my software and hardware a section at a time to if any nasty little bugs have appeared, I also have another C compiler and seperate ICD debugger/programmer in which I write the same code and then use that to check everything, a sort of double jepordy...If you have got your code already wotking on hardware then why bother with Proteus.

Regards Dave
"When the bugs strike it's no good bashing your head against a brick wall, you might damage the wall"

p3tzata_
Posts: 3
Joined: 18 Aug 2016 14:06

Re: My project in MikroC not work in Proteus

#4 Post by p3tzata_ » 19 Aug 2016 11:44

I sorry for my "c" in the title.
I forgot to mention that I programed my PIC16F877A with this code and it isn't work, doing nothing,
but when I comment some block it work properly according to code.

p3tzata_
Posts: 3
Joined: 18 Aug 2016 14:06

Re: My project in MikroC not work in Proteus

#5 Post by p3tzata_ » 19 Aug 2016 11:45

I sorry for my "c" in the title.
I forgot to mention that I programed my PIC16F877A with this code and it isn't work, doing nothing,
but when I comment some block it work properly according to code.

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: My project in MicroC not work in Proteus

#6 Post by Sparky1039 » 19 Aug 2016 16:51

@diode_blade
As a user of Proteus for about 10 years I can say it is a very nice tool for testing PIC C code. As a +30 year professional engineer I have used this application to simulate the microcontroller operative processes for many commercial product designs. The issue with ANY simulation tool is knowing its limitations and how to work around them, or avoid them completely. Conversely, the schematic and PCB part of ISIS while adequate isn't quite up to the level found with professional tools like Altium, OrCAD, or Mentor Graphics (of which I actively use the first 2). I view the EDA part of ISIS as a very good amateur/ hobbiest development tool set, and in all fairness is improving each year.

The problem Proteus has with mikroC is the generated output doesn't strictly follow the Microchip .cof standards. Because mikroC applies an unique form of compiled memory assignment, allocation, and initialization, it doesn't always port the variable information as the .cof format dictates. This has been a known compatibility issue for quite some time, and mikroE developers have repeatedly stated they do follow the standards. But upon further investigation by the developers of Proteus (whom I have worked with the engineers personally on various issues) they have said just the opposite. The interesting thing is I use many professional compilers for PIC and all of them work just fine with Proteus, but mikroC is the only one I'm familiar with that does not always integrate well. This doesn't mean mikroC will not run on Proteus, rather there are instances where it has problems.

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

Re: My project in MicroC not work in Proteus

#7 Post by p.erasmus » 20 Aug 2016 20:40

Sparky1039 wrote:@diode_blade
As a user of Proteus for about 10 years I can say it is a very nice tool for testing PIC C code. As a +30 year professional engineer I have used this application to simulate the microcontroller operative processes for many commercial product designs. The issue with ANY simulation tool is knowing its limitations and how to work around them, or avoid them completely. Conversely, the schematic and PCB part of ISIS while adequate isn't quite up to the level found with professional tools like Altium, OrCAD, or Mentor Graphics (of which I actively use the first 2). I view the EDA part of ISIS as a very good amateur/ hobbiest development tool set, and in all fairness is improving each year.

The problem Proteus has with mikroC is the generated output doesn't strictly follow the Microchip .cof standards. Because mikroC applies an unique form of compiled memory assignment, allocation, and initialization, it doesn't always port the variable information as the .cof format dictates. This has been a known compatibility issue for quite some time, and mikroE developers have repeatedly stated they do follow the standards. But upon further investigation by the developers of Proteus (whom I have worked with the engineers personally on various issues) they have said just the opposite. The interesting thing is I use many professional compilers for PIC and all of them work just fine with Proteus, but mikroC is the only one I'm familiar with that does not always integrate well. This doesn't mean mikroC will not run on Proteus, rather there are instances where it has problems.
Thank you Sparky ,
I have been using Proteus now for 5 years ( Mince from this forum corrected me I had the same opinion as these guys) ,Proteus works perfectly with the other PIC compilers as you mentioned
it is the way the mE compilers function ,We simulate and do virtual design with Proteus on commercial products and same a lot of time and budget
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

diode_blade
Posts: 172
Joined: 24 Aug 2014 17:55
Location: Sheffield

Re: My project in MicroC not work in Proteus

#8 Post by diode_blade » 21 Aug 2016 11:13

p.erasmus wrote:
Sparky1039 wrote:@diode_blade
As a user of Proteus for about 10 years I can say it is a very nice tool for testing PIC C code. As a +30 year professional engineer I have used this application to simulate the microcontroller operative processes for many commercial product designs. The issue with ANY simulation tool is knowing its limitations and how to work around them, or avoid them completely. Conversely, the schematic and PCB part of ISIS while adequate isn't quite up to the level found with professional tools like Altium, OrCAD, or Mentor Graphics (of which I actively use the first 2). I view the EDA part of ISIS as a very good amateur/ hobbiest development tool set, and in all fairness is improving each year.

The problem Proteus has with mikroC is the generated output doesn't strictly follow the Microchip .cof standards. Because mikroC applies an unique form of compiled memory assignment, allocation, and initialization, it doesn't always port the variable information as the .cof format dictates. This has been a known compatibility issue for quite some time, and mikroE developers have repeatedly stated they do follow the standards. But upon further investigation by the developers of Proteus (whom I have worked with the engineers personally on various issues) they have said just the opposite. The interesting thing is I use many professional compilers for PIC and all of them work just fine with Proteus, but mikroC is the only one I'm familiar with that does not always integrate well. This doesn't mean mikroC will not run on Proteus, rather there are instances where it has problems.
Thank you Sparky ,
I have been using Proteus now for 5 years ( Mince from this forum corrected me I had the same opinion as these guys) ,Proteus works perfectly with the other PIC compilers as you mentioned
it is the way the mE compilers function ,We simulate and do virtual design with Proteus on commercial products and same a lot of time and budget
Hey Guys,
I hold my hands up, I am not knocking Proteus, I just prefer using the IDE whether it be Mikroc or Mplab and my debuggers and hardware to see whats happening..

to quote a friend who once said to me...

"To write perfect code is nonsense..."
"To write faulty code with bugs makes plenty of sense..." :lol: :shock:
"When the bugs strike it's no good bashing your head against a brick wall, you might damage the wall"

Post Reply

Return to “mikroC PRO for PIC General”