All pastes #178435 Raw Edit

Someone

public text v1 · immutable
#178435 ·published 2006-09-21 09:30 UTC
rendered paste body
/************************************************************************************
*     Name:				                                                            *
*     Written by:     	Jama Yusuf and Steffen Rogne Automasjonsutvikling AS, 2006  *                 
*     Parameters:       .               			        	                    * 
*     Description:      this software controls the Pellet Brenner and it has a      *
*                       multible functions that runs the flow of the pellet         * 
*                       feeding & the speed of the fan that blows air to the cup    *
*                       it also reads a temperatures sensers                        *
*************************************************************************************/

/*This is NOT the final program*/
/*I havent done a lot on the looks on the source*/

#define RS_LCD              PORTB.2
#define RW_LCD	     		PORTB.1
#define E_LCD	     		PORTB.0
#define Select	     		PORTB.4
#define RI		     		PORTB.5
#define Ignite	     		PORTC.0 
#define M2_		     		PORTC.1
#define M1_		     		PORTC.2
#define CTS		     		PORTC.3
#define RST		    		PORTC.4
#define SD		     		PORTC.5
#define TXD		     		PORTC.6
#define RXD		     		PORTC.7
#define LED		     		PORTA.5 
#define leftup              PORTA.0
#define leftdown            PORTA.1
#define rightup             PORTA.2
#define rightdown           PORTA.3
#define GO_DONE             ADCON0.2
#pragma char AD_LO     @        0x20
#pragma char AD_HI     @        0x21
#define FEED_LO             CCPR2L
#define FEED_HI             CCPR2H 
#define FAN_LO              CCPR1L
#define FAN_HI              CCPR1H 

#define ViTre 0
#define Bio   1  

void PICint(void);
void SetUp_Menu(void); 
void read_EEPROM (int adr_read, int *value_return);
void write_EEPROM(int *adr_write, int *data2write);   
void recovery_menu(void);
void last_check (int *GO_GO);
void default_start (void);
void default_speed (void);
void recovery_start(void);
void active_state (void);
void igniter(void);
void temperature_reader (int temp_check,int *temp_true_false);
void default_feed_speed (void);
void default_fan_speed (void);
void start_fan_speed (void);
void start_feed_speed(void);
void restart (void);
void rightDpressed(void);
void rightUpressed(void);
void STOP_FEED(void);
void finishing_state (void);
void STOP_FAN(void);

void lcd_disp(char t, char p);
void lcd_prints(const char *p);
void delay2(int lc);
void delay1(int lc);
void delay_s(int sec);
void bin2dec(int valHI, int valLO);
void bincount(void);  // 5+1 array...
void math(unsigned int varHi,unsigned int varLo,unsigned int *varHiOut,unsigned int *varLoOut);
void system_error(int ER_AD,int ER_COD);


/*******************************************************************************************************************/
/* Globals                                                                                                         */
/*******************************************************************************************************************/
char dig[6]; // a global verable that is used to controll the display.
int  lastx;// a global verable that is used to control the count of the Bin to DEC	
int  pos; // A global verable that is used to show the possion of the number
int STOP_flag; // a global verable that is used to sett a flag for the system stop
int state1;
int state2;
int vendor;


//int stat;
//int speed;
//int unloop;


//----------------------------------------------------------
//	ISR
//-----------------------------------------------------------
#pragma origin 4

/*interrupt interruptISR(void)
{
  //	int_save_registers    // W, STATUS (and PCLATH)
  // 	INTF = 0;
 // 	T0IF = 0;
 //   int_restore_registers // W, STATUS (and PCLATH)
}


/*******************************************************************************************************************/
/*  Main program                                                                                                   */
/*******************************************************************************************************************/
void main ()             

{

//int T1;
//int T2;
int true_false;
vendor = Bio;



//vendor = ViTre;
	
		PICint ();	//calling for the PIC initialization
        delay_s(1);

		lcd_disp(1,0x38); 
		lcd_disp(1,0x0C); 
		lcd_disp(1,0x01); // clear screen

	if(leftup ==0)
		SetUp_Menu ();

			lcd_disp(1,0x01); // clear screen
			lcd_prints("    Welcome");
			lcd_disp(1,0xC0); // Second row
			lcd_prints("Demo V1.2");
			delay_s(2);	

			
		//read_EEPROM(1,&true_false);//calling for the EEPROM reading the check the last information stored...specially if it 
                                   // was stopped and pellets burn out succesfull or not...

	//if(true_false == 1)// checking the resolt from the EEPROM
		default_start();// If it was stopped it would call for the default start which would start the system opprotion

	//else 
		//recovery_menu();// if it was not stopped then it would call for the recovery menu to run some "recovery"

/*******************************************************************************************************************/
// when the call for the default start or the recovery menu returns whitout ERRORS 
// the system rests here in stopped or active state
/*******************************************************************************************************************/
	do{
		active_state();// calling for the active state AND staying there until it's stopped and then it would return here
		default_start();//calling for start when the system is stopped and returns here by itselv to active state
	
	}while (1);
}

/*******************************************************************************************************************/
/*  this function is the default menu where the system normaly starts if it has been stopped correctly             */
/*  and the Message of that action has been correctly writen to the EEPROM                                         */
/*******************************************************************************************************************/
void default_start(void)
{

int temp_check3;// a variable that is used to read back the informaion of the temperature senser
int temp_check4; // a variable that is used to read back the informaion of the temperature senser
int loop_check;// a variable that is used to read back the informaion of the temperature senser

		lcd_disp(1,0x01); // clear screen  
		lcd_prints ("   Welcome");
		lcd_disp(1,0xC0); // Line two
		lcd_prints("  Push Start");
		delay_s(1);

     while(leftup != 0);

	//if(leftup == 0){// if start is been pressed

		start_feed_speed();// calling the feed a head of everything to get the pellets in to the cup
	    start_fan_speed(); // calling the fan to be blowed in to full speed to help the burn 
		igniter();// calling the igniter to start the burn
			
		temperature_reader (1,&temp_check4);// calling the temperature reader to see if the temperature is rising
	
	if(temp_check4 == 0)// checking if the temperature is rising or not if it's not rising it would call for restart
			restart ();// calling for restart if the temperature didn't rise

			default_speed (); // calling for the default speed
	
}
/*******************************************************************************************************************/
/*  this function alows the value of the speed to be entered here and also keep                                    */
/*  the system running at this area until it's stopped it also writes the stopp message to the EEPROM              */
/*******************************************************************************************************************/
#define MAXSTATE1 11
#define MAXSTATE2 11
void active_state (void)
{
		
int x;	
state1 = 0;
state2 = 0;            

	while(leftdown != 0){
             if (vendor == Bio){
				switch(state1){
					case 0:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   1000 Watt ");
					      	delay2(255);
							 FEED_LO = 0x50;
							 FAN_LO  = 0x70; 
					break;
			
					case 1:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   2000 Watt ");
					      	delay2(255);
							 FEED_LO = 0x53;
							 FAN_LO  = 0x70; 

					break;
		
					case 2:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   3000 Watt ");
					      	delay2(255);
							 FEED_LO = 0x60;
							 FAN_LO  = 0x70; 

					break;
			
					case 3:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   4000 Watt ");
					      	delay2(255);
							 FEED_LO = 0x70;
							 FAN_LO  = 0x70; 

					break;
		
					case 4:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   5000 Watt ");
					      	delay2(255);
							 FEED_LO = 0x80;
							 FAN_LO  = 0x80; 

					break;
			
					case 5:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   6000 Watt ");
					      	delay2(255);
							 FEED_LO = 0x90;
							 FAN_LO  = 0x90; 

					break;
		
					case 6:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   7000 Watt ");
					      	delay2(255);
							 FEED_LO = 0xa0;
							 FAN_LO  = 0xa0; 

					break;
			
					case 7:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   8000 Watt ");
							 FEED_LO = 0xb0;
							 FAN_LO  = 0xb0; 
					      	delay2(255);
					break;
		
					case 8:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   9000 Watt ");
							 FEED_LO = 0xc0;
							 FAN_LO  = 0xc0; 
					      	delay2(255);
					break;
			
					case 9:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("  10,000 Watt ");
							 FEED_LO = 0xd0;
							 FAN_LO  = 0xd0; 
					      	delay2(255);
					break;
		
					case 10:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("  11,000 Watt ");
							 FEED_LO = 0xe0;
							 FAN_LO  = 0xe0; 

					      	delay2(255);
					break;
			
					case 11:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("Fuel: Biobrensel");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("  12,000 Watt ");
							 FEED_LO = 0xff;
							 FAN_LO  = 0xff; 

					      	delay2(255);
					break;

				 }
			
			while(rightup && rightdown&&leftdown);
		
				if(rightup == 0){
					if(state1 != MAXSTATE1) 
		               state1 = state1 +1;
			    }
			
				if(rightdown == 0){
					if(state1 != 0)
		               state1 = state1-1;
		        } 
          }
         else{
				switch(state2){
					case 0:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   1000 Watt ");
							 FEED_LO = 0x26;
							 FAN_LO  = 0x01; 

					      	delay2(255);
					break;
			
					case 1:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   2000 Watt ");
							 FEED_LO = 0x2e;
							 FAN_LO  = 0x01; 

					      	delay2(255);
					break;
		
					case 2:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   3000 Watt ");
							 FEED_LO = 0x37;
							 FAN_LO  = 0x04; 

					      	delay2(255);
					break;
			
					case 3:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   4000 Watt ");
							 FEED_LO = 0x40;
							 FAN_LO  = 0x0a; 

					      	delay2(255);
					break;
		
					case 4:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   5000 Watt ");
							 FEED_LO = 0x48;
							 FAN_LO  = 0x15; 

					      	delay2(255);
					break;
			
					case 5:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   6000 Watt ");
							 FEED_LO = 0x51;
							 FAN_LO  = 0x23; 

					      	delay2(255);
					break;
		
					case 6:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   7000 Watt ");
							 FEED_LO = 0x5a;
							 FAN_LO  = 0x35; 

					      	delay2(255);
					break;
			
					case 7:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   8000 Watt ");
							 FEED_LO = 0x63;
							 FAN_LO  = 0x4b; 

					      	delay2(255);
					break;
		
					case 8:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("   9000 Watt ");
							 FEED_LO = 0x6b;
							 FAN_LO  = 0x65; 

					      	delay2(255);
					break;
			
					case 9:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("  10,000 Watt ");
							 FEED_LO = 0x74;
							 FAN_LO  = 0x82; 

					      	delay2(255);
					break;
		
					case 10:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("  11,000 Watt ");
							 FEED_LO = 0x7d;
							 FAN_LO  = 0xa3; 

					      	delay2(255);
					break;
			
					case 11:
							lcd_disp(1,0x01); // clear screen
							lcd_prints("  Fuel: ViTre");
							lcd_disp(1,0xC0); // Line two
							lcd_prints("  12,000 Watt ");
							 FEED_LO = 0x85;
							 FAN_LO  = 0xc8; 

					      	delay2(255);
					break;

				 }
			
			while(rightup && rightdown&&leftdown);
		
				if(rightup == 0){
					if(state2 != MAXSTATE2) 
		               state2 = state2 +1;
			    }
			
				if(rightdown == 0){
					if(state2 != 0)
		               state2 = state2-1;
		        } 
          }
	}  
	if(leftdown == 0)

		lcd_disp(1,0x01); // clear screen
		lcd_prints("BPS System ble");
		lcd_disp(1,0xC0); // Line two
		lcd_prints("   stoppet");
        delay_s(2);

		finishing_state();			
}

/*******************************************************************************************************************/
/* this function setts the fan and the feed to the  default speed which is 50 by default but can be changed by user*/   
/*******************************************************************************************************************/
#pragma codepage 1
void default_speed (void)

{


		default_feed_speed();// call for the default feed speed. this is 50 by default but it can be set to higher
                             // or lower by the user and the information is saved to EEPROM

		default_fan_speed();// call for the default fan speed. this is 50 by default but it can be set to higher
                             // or lower by the user and the information is saved to EEPROM

}

/**************************************************************************************************************************/
/*  this is a recovery function that is connected to the recovery menu and starts in different way then the default start */
/**************************************************************************************************************************/
void restart (void)
{
int temp_check2;
//int x;
int loop;
//int ERROR;
int error_adr;     

	STOP_FEED ();
	
	loop = 0;
	do {
		lcd_disp(1,0x01); // clear screen
		lcd_prints("  System Being");
		lcd_disp(1,0xC0); // Line two  
		lcd_prints("    Restarted");
		delay_s(10);
		
		igniter();
		
		temperature_reader (0,&temp_check2);
	
		if(temp_check2 == 1) break;
	
		loop = loop +1;
		
	}while(loop < 2);
	
	
	if (temp_check2 == 0)
		system_error(0,0); 
				    
}

/*******************************************************************************************************************/
/*  this function is the start feed fuction...it's connected to the default start                                  */
/*******************************************************************************************************************/
void start_feed_speed(void)

{
int x;
		FEED_LO = 0xFF;
		
		lcd_disp(1,0x01); // clear screen
		lcd_prints(" Starting Feed");
		lcd_disp(1,0xC0); // Line two
		lcd_prints(" For 15 sec   ");
	
	for(x=1; x < 15; x++){
		delay_s(1);																						
		bin2dec(0,x); 
		lcd_disp(1,0xCC); // clear screen	
		lcd_prints("   ");
		lcd_disp(1,0xCC); // clear screen	
		lcd_prints(&dig[pos]);
	}
	FEED_LO = 0x00;
}

/*******************************************************************************************************************/
/* this function keeps the fan run until the temperature starts to go down which means no more pellets to burn     */   
/*******************************************************************************************************************/
void finishing_state (void)
{
int x;
int pellets_left;

		STOP_FEED();
		
	do{
	
		lcd_disp(1,0x01); // clear screen
		lcd_prints((char*)" Pre-STOP State");
		lcd_disp(1,0xC0); // Line two
		lcd_prints((char*)"   Please wait");
		delay_s(2);
		
	
		if(pellets_left != 4)
			pellets_left = pellets_left + 1;
			
		if(leftup == 0)
		break;
	
	}while(pellets_left < 3);
	
		STOP_FAN();

}
/*******************************************************************************************************************/
/*  this function Stops the feed:                                                               */
/*******************************************************************************************************************/
void STOP_FAN(void)
{
int x;
		FAN_LO = 0x00;

		lcd_disp(1,0x01); // clear screen
		lcd_prints((char*)"   Stopping");
		lcd_disp(1,0xC0); // Line two
		lcd_prints((char*)"    The FAN");
		delay_s(2);

}

/*******************************************************************************************************************/
/*  this function Stops the feed:                                                               */
/*******************************************************************************************************************/
void STOP_FEED(void)
{
int x;
		FEED_LO = 0x00; 

		lcd_disp(1,0x01); // clear screen
		lcd_prints("    Stopping");
		lcd_disp(1,0xC0); // Line two
		lcd_prints("  the Feeding");
		delay_s(2);

}

/*******************************************************************************************************************/
/*  this function is the default feed speed fuction:                                                               */
/*******************************************************************************************************************/
void default_feed_speed (void)
{
int x;
		FEED_LO = 0x32; 

		lcd_disp(1,0x01); // clear screen
		lcd_prints("  Setting FEED");
		lcd_disp(1,0xC0); // Line two
		lcd_prints("to Default speed");
		delay_s(2);
}

/*******************************************************************************************************************/
/*  this function is the start fan fuction:                                                                        */
/*******************************************************************************************************************/
void start_fan_speed (void)
{
int x;
		FAN_LO = 0x80;

		lcd_disp(1,0x01); // clear screen
		lcd_prints("    Starting");
		lcd_disp(1,0xC0); // Line two
		lcd_prints("    the FAN");
		delay_s(2);

}
/*******************************************************************************************************************/
/*  this is the default fan speed  fuction:                                                                        */
/*******************************************************************************************************************/
void default_fan_speed (void)
{
int x;
		FAN_LO = 0x32;
		
		lcd_disp(1,0x01); // clear screen
		lcd_prints("  Setting FAN");
		lcd_disp(1,0xC0); // Line two
		lcd_prints("To Default speed");
		delay_s(2);
}

/*******************************************************************************************************************/
/*  this function is the start igniter fuction...it's connected to the default start and restart functions         */
/*******************************************************************************************************************/
void igniter (void)

{
int z;   													    
int x;
																		 
	Ignite = 1;
	
	lcd_disp(1,0x01); // clear screen
	lcd_prints(" Setting Igniter");
	lcd_disp(1,0xC0); // Line two
	lcd_prints("  ON   ");

	for(x=1; x < 25; x++){
		delay_s(1);
		z = 25-x; 																						
		bin2dec(0,z); 
		lcd_disp(1,0xCC); // clear screen																			                    
		lcd_prints("   ");
		lcd_disp(1,0xCC); // clear screen																			                    
		lcd_prints(&dig[pos]);
	}

	lcd_disp(1,0x01); // clear screen
	lcd_prints("    Turning");
	lcd_disp(1,0xC0); // Line two
	lcd_prints("The Igniter OFF");	
	delay_s(2);
	
	
	Ignite = 0;

}
/*******************************************************************************************************************/
/*  this function reads the temperature and it's connected to the default start, restart, system check, recovery menu */
/*******************************************************************************************************************/
#pragma codepage 0

void temperature_reader (int temp_check, int *temp_true_false)

{
int T1, T2;
int a;
int x;
uns16 z;
uns24 av;

	a = 1;
	av = 0;
	
//	PORTA  = 0x00;
	ADCON1 = 0x80;
	ADCON0 = 0x39;

	lcd_disp(1,0x01); // clear screen
	lcd_prints("   Reading");
	lcd_disp(1,0xC0); // Line two  
	lcd_prints(" Temperature");
	delay_s(2);

	lcd_disp(1,0x01); // clear screen
	lcd_prints("   Please wait");
	lcd_disp(1,0xC0); // Line two 

	
	for(x = 0; x < 4; x++)
	{
		GO_DONE = 1;
		while (GO_DONE);

		GO_DONE = 1;
		while (GO_DONE);

		AD_LO = ADRESL;
		AD_HI = ADRESH;

		z = AD_HI;
		z = z << 8;
		z = z & 0xFF00;
		z = z | AD_LO;
		av = av + z;

	}
	av = av >> 2;
	z = av;
	z = z-233;

	ADCON1= 0x06;

	AD_LO   = z & 0xFF;
	z 		= z >> 8;
	AD_HI   = z;

	lcd_prints ("     ");
	lcd_disp(1,0xC0); 
	math(AD_HI,AD_LO,&T1, &T2);
	bin2dec(T1,T2); 
	lcd_prints("  is ");
	lcd_prints(&dig[pos]);
	lcd_prints(" ");
	lcd_disp(0,0xDF);
	lcd_prints("C");
	delay_s(4);	

	if(AD_HI > 1){
		lcd_disp(1,0x01); // clear screen
		lcd_prints("  Temperature");
		lcd_disp(1,0xC0); // Line two  
		lcd_prints("rise succesfully");
		delay_s(2);
		*temp_true_false =1;
    }
	else{	
		lcd_disp(1,0x01); // clear screen
		lcd_prints("Temperature is");
		lcd_disp(1,0xC0); // Line two  
		lcd_prints(" not rising");
		delay_s(2);	
	    *temp_true_false = 0;
	}

}


void delay1(int lc)
{
   int lc2 = 0x20;

	lc = lc << 1;
 
	while(lc)

      {
		while(lc2) 
            lc2 = lc2 - 1;

			lc = lc - 1;
          
	
        	lc2 = 0x20;
	  }
}

void delay2(int lc)
{
   int lc2 = 0xFF;
   int lc3 = 0xFF;
 
	while(lc)
    {
		while(lc2) 
        { 
        	while(lc3){
				lc3 = lc3 - 1;
			}
	    	lc2 = lc2 - 1;
		}
	    lc = lc - 1;                                       
    	lc2 = 0xFF;
	}


}

void delay_s(int sec)
{
	int x;

	for(x=0; x < sec; x++){
        delay2(255);
        delay2(255);		        
        delay2(255);
        delay2(255);		        
	}

}

void lcd_disp(char t, char p)
{
  if(t==1)
   	RS_LCD = 0;
  else	
   	RS_LCD = 1;

	RW_LCD = 0;
	PORTD  = p;
	E_LCD = 0;
	delay1 (5);
	E_LCD = 1;

	if(p == 0x01)
		delay1(70);
	else
		delay1(10);
}   	

void lcd_prints(const char *p)
{
	char x;
	while(*p)
	{
		x = *p;
		lcd_disp(0,x);
		p= p + 1;
	}
}


void bincount(void)  // 5+1 array...
{

	dig[4]= dig[4]+1;
	if(dig[4] == 0x3A){
		dig[4] = 0x30;
		dig[3]= dig[3]+1;
		if(dig[3] == 0x3A){
			dig[3] = 0x30;
			dig[2]= dig[2]+1;
			if(dig[2] == 0x3A){
				dig[2] = 0x30;
				dig[1]= dig[1]+1;
				if(dig[1] == 0x3A){
					dig[1] = 0x30;
					dig[0]= dig[0]+1;
				}
			}
		}
	}
}

void bin2dec(int valHI, int valLO)
{
	dig[0] = '0';
	dig[1] = '0';
	dig[2] = '0';
	dig[3] = '0';
	dig[4] = '0';
	dig[5] = 0;

	while(valLO != 0){
		bincount();
		valLO=valLO-1;
	}

	while(valHI != 0){
		valLO = valLO-1;
		bincount();
		while(valLO != 0){
			bincount();
			valLO = valLO -1;
		}
        valHI=valHI-1;	
	}

	pos= 0;
	if(dig[0] == '0'){
		dig[0] = ' ';
		pos= 1;
		if(dig[1] == '0'){
			dig[1] = ' ';
			pos= 2;
			if(dig[2] == '0'){
				dig[2] = ' ';
				pos= 3;
				if(dig[3] == '0'){
					dig[3] = ' ';
					pos= 4;
				}
			}
		}
	}
}




void math(unsigned int varHi,unsigned int varLo,unsigned int *varHiOut,unsigned int *varLoOut)
{
	unsigned int temp1;

	temp1 = varLo;
	temp1=temp1- 0;
	
	if( varHi > 3){
		*varHiOut = 3;
		*varLoOut = 0xFF;
	}
	else{
		if(temp1 > varLo){
			if(varHi == 0){
				*varHiOut = 0;
				*varLoOut = 0;
			}
			*varHiOut = varHi-1;
			*varLoOut = temp1;		
		}
		else{
			*varHiOut = varHi;
			*varLoOut = temp1;
		}
	}
}
#pragma codepage 1

/*******************************************************************************************************************/
/*  this function reads the EEPROM and directs the program to the right start system                               */
/*******************************************************************************************************************/

void SetUp_Menu(void)
{
int x;
 
	lcd_disp(1,0x01); // clear screen
	lcd_prints("Set-Up Menu");
	lcd_disp(1,0xC0); // Second row
	lcd_prints("set vendor pls");

	while(leftup != 1);

while(rightup && rightdown);	
	if(rightup == 0){
			lcd_disp(1,0x01); // clear screen
			lcd_prints("Vender is set to");
			lcd_disp(1,0xC0); // Second row
			lcd_prints("    Vi-Tre");
	  vendor = ViTre;
	}
	if(rightdown == 0){
			lcd_disp(1,0x01); // clear screen
			lcd_prints("Vender is set to");
			lcd_disp(1,0xC0); // Second row
			lcd_prints(" Biobrensel AS");
	   vendor = Bio;
	 }  
             
	delay_s(3);	             
}
/*********************************************************************************************************************/
/*  this function is a recovery function. the program gets here if the system suddently gets shut-down or lost power */
/*  it reads the temperature senser and the EEPROM in order to stor the system to it's original routine.             */
/*********************************************************************************************************************/

void recovery_menu(void)
{
int x;
int temp_check4;


		lcd_disp(1,0x01); // clear screen  
		lcd_prints ("Press Start to");
		lcd_disp(1,0xC0); // Line two
		lcd_prints("   Recover");

	while(leftup);


	if(leftup == 0)

		lcd_disp(1,0x01); // clear screen  
		lcd_prints ("  recovering");
		lcd_disp(1,0xC0); // Line two
		lcd_prints(" Please Wait...");

		delay_s(2);



		temperature_reader (0,&temp_check4);

	if (temp_check4 == 0)
		recovery_start();

		temperature_reader (1,&temp_check4);

	if (temp_check4 == 0)
		restart ();
         
}

/**************************************************************************************************************************/
/*  this is a recovery function that is connected to the recovery menu and starts in different way then the default start */
/**************************************************************************************************************************/
void recovery_start(void)
{

		start_fan_speed();//call for the start fan speed to set the fan to full speed	
		igniter();	//call for the igniter while the fan is in full speed to help the burn speed
		default_feed_speed();//call for the default feed speed to reduce the speed of the feeding


}

#pragma codepage 0
void system_error(int ER_AD,int ER_COD)
{

int x;

FAN_LO = 0X00;
FEED_LO = 0X00;
	do{
		lcd_disp(1,0x01); // clear screen
		lcd_prints(" ERROR Code 01");
		lcd_disp(1,0xC0); // Line two  
		lcd_prints("   Was found");
		delay_s(3);
		
		lcd_disp(1,0x01); // clear screen
		lcd_prints("If Problem Fixed");
		lcd_disp(1,0xC0); // Line two  
		lcd_prints(" REBOOT System");
		delay_s(1);
	
	
	}while(1);
}

/*******************************************************************************************************************/
/*  this function reads the EEPROM                                                                                 */
/*******************************************************************************************************************/
void read_EEPROM (int adr_read, int *value_return)
{
//this is not really a function: it  does not read the EEPROM....It just reads the value that it has been given to
// adr_read and moves to value return...the real function here is not yet been 

   	lcd_disp(1,0x01); // clear screen
    lcd_prints((char*)"Reading EEPROM");
    lcd_disp(1,0xC0); // Line two  
    lcd_prints((char*)"Please wait...");
		delay_s(1);

 if(adr_read == 1) // checking the value of adr to read and setting it flag if it's = 1
    *value_return = 1; // moving the value or adr_read to value return
 else // checking the value of adr to read and setting it flag if it's = 0
  *value_return = 0;//moving the value of adr_read to value return if it = 0

/*
EEADR = *adr_read ;
EECON1 = 0x01;
*true_false= EEDATA;
*/

/*
MOVF DATA_EE_ADDR,W ; Data Memory
MOVWF EEADR ; Address to read
BSF STATUS,RP0 ; Bank 3
BCF EECON1,EEPGD ; Point to Data
; memory
BSF EECON1,RD ; EE Read
BCF STATUS,RP0 ; Bank 2
MOVF EEDATA,W ; W = EEDATA
*/

}
/*******************************************************************************************************************/
/*  this function write the EEPROM                                                                                 */
/*******************************************************************************************************************/
void write_EEPROM(int *adr_write, int *data2write)
{
// NOTE: This is not the real function it's not yet been writen or proved


EEADR = *adr_write;
EEDATA = *data2write;
EECON1 = 0x04;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1 = 0x06;
EECON1 = 0x02;


/*

BTFSC EECON1,WR ;Wait for write

MOVF DATA_EE_ADDR,W ;Data Memory
MOVWF EEADR ;Address to write

MOVF DATA_EE_DATA,W ;Data Memory Value
MOVWF EEDATA ;to write

BCF EECON1,EEPGD ;Point to DATA memory
BSF EECON1,WREN ;Enable writes

BCF INTCON,GIE ;Disable INTs.

MOVLW 55h ;
MOVWF EECON2 ;Write 55h
MOVLW AAh ;

MOVWF EECON2 ;Write AAh

BSF EECON1,WR ;Set WR bit to begin write

BSF INTCON,GIE ;Enable INTs.

BCF EECON1,WREN ;Disable writes

*/
}
#pragma codepage 1
void PICint(void)

  {

   PORTA = 0;
   PORTB = 0;
   PORTC = 0;
   PORTD = 0;

   ADCON0 = 0x38;
   ADCON1 = 0x06;
 
   TRISD  = 0;

             
   TRISA  = 0xFF;      
   PORTA  = 0xFF;

          
   TRISB  = 0xE0;       
            
   TRISC  = 0x88;
   
   E_LCD = 1;
   RW_LCD = 0;

   RS_LCD = 1;

   LED  = 0;
   M1_  = 0;
   M2_  = 1;

   Ignite = 0;

   T2CON  =	0x07;
	
	TMR2 =	0x00;
	PR2	= 0xFF;


	CCP1CON = 0x0F;
    CCP2CON = 0x0F;

 	FAN_LO = 0x00;
	FAN_HI = 0x00;

  	FEED_LO = 0x00;
	FEED_HI = 0x00;

   }
#pragma codepage 0