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);
}
}