Sunday 26 October 2014

week 6 semester 2 - The program

here is where we reach the part which coding or programming take place. since the circuit have be troubleshoot and work properly, it need a program to complete the system. so, the program for this project or circuit not very complex or complicated, because it just want to show the display of the voltage input and the total voltage collected in the storage.




 //variables
unsigned int batA,batB,batC,batD;
unsigned char arr_batA[4],arr_batB[4],arr_batC[4],arr_batD[5];

/*----------------------------- Initialization function -----------*/
void mcu_init()
{
//Lcd
Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init
Lcd_Cmd(Lcd_CLEAR);//Clear display
Lcd_Cmd(Lcd_CURSOR_OFF);//Turn cursor off

//relay
TRISD=0;
PORTD=1;
}
/*-------------------------- End of intialization --------------------*/

/*----------------------------- display values ----------------*/
void display_values()
{
 unsigned long temp,percent;
 unsigned int total;
//calculat battery A voltage
 temp=(unsigned long)(batA*20);
 temp=temp*100;
 batA = (unsigned int)(temp/1023);
  arr_batA[0]=batA/1000;
  arr_batA[1]=(batA%1000)/100;
  arr_batA[2]=((batA%1000)%100)/10;
  arr_batA[3]=((batA%1000)%100)%10;
  Lcd_Chr(1, 4, arr_batA[0]+'0');
  Lcd_Chr(1, 5, arr_batA[1]+'0');
  Lcd_Chr(1, 6, '.');
  Lcd_Chr(1, 7, arr_batA[2]+'0');
  Lcd_Chr(1, 8, arr_batA[3]+'0');


//calculate battery B  voltage
 temp=(unsigned long)(batB*20);
 temp=temp*100;
 batB = (unsigned int)(temp/1023);
  arr_batB[0]=batB/1000;
  arr_batB[1]=(batB%1000)/100;
  arr_batB[2]=((batB%1000)%100)/10;
  arr_batB[3]=((batB%1000)%100)%10;
  Lcd_Chr(1, 13, arr_batB[0]+'0');
  Lcd_Chr(1, 14, arr_batB[1]+'0');
  Lcd_Chr(1, 15, '.');
  Lcd_Chr(1, 16, arr_batB[2]+'0');
  Lcd_Chr(1, 17, arr_batB[3]+'0');
  
  //calculate battery C  voltage
 temp=(unsigned long)(batC*20);
 temp=temp*100;
 batC = (unsigned int)(temp/1023);
  arr_batC[0]=batC/1000;
  arr_batC[1]=(batC%1000)/100;
  arr_batC[2]=((batC%1000)%100)/10;
  arr_batC[3]=((batC%1000)%100)%10;
  Lcd_Chr(2, 4, arr_batC[0]+'0');
  Lcd_Chr(2, 5, arr_batC[1]+'0');
  Lcd_Chr(2, 6, '.');
  Lcd_Chr(2, 7, arr_batC[2]+'0');
  Lcd_Chr(2, 8, arr_batC[3]+'0');
  
  //calculate battery D voltage
 temp=(unsigned long)(batD*20);
 temp=temp*100;
 batD = (unsigned int)(temp/1023);
  arr_batD[0]=batD/1000;
  arr_batD[1]=(batD%1000)/100;
  arr_batD[2]=((batD%1000)%100)/10;
  arr_batD[3]=((batD%1000)%100)%10;
  Lcd_Chr(2, 13, arr_batD[0]+'0');
  Lcd_Chr(2, 14, arr_batD[1]+'0');
  Lcd_Chr(2, 15, '.');
  Lcd_Chr(2, 16, arr_batD[2]+'0');
  Lcd_Chr(2, 17, arr_batD[3]+'0');
  
total=batA+batB+batC;
  arr_batD[0]=total/1000;
  arr_batD[1]=(total%1000)/100;
  arr_batD[2]=((total%1000)%100)/10;
  arr_batD[3]=((total%1000)%100)%10;
  Lcd_Chr(3, 7, arr_batD[0]+'0');
  Lcd_Chr(3, 8, arr_batD[1]+'0');
  Lcd_Chr(3, 9, '.');
  Lcd_Chr(3, 10, arr_batD[2]+'0');
  Lcd_Chr(3, 11, arr_batD[3]+'0');
percent= ((unsigned long)batD*100)/12;
  arr_batD[0]=percent/10000;
  arr_batD[1]=(percent%10000)/1000;
  arr_batD[2]=((percent%10000)%1000)/100;
  arr_batD[3]=(((percent%10000)%1000)%100)/10;
  arr_batD[4]=(((percent%10000)%1000)%100)%10;
  Lcd_Chr(4, 9, arr_batD[0]+'0');
  Lcd_Chr(4, 10, arr_batD[1]+'0');
  Lcd_Chr(4, 11, arr_batD[2]+'0');
  Lcd_Chr(4, 12, '.');
  Lcd_Chr(4, 13, arr_batD[3]+'0');
  Lcd_Chr(4, 14, arr_batD[4]+'0');
 


}
/*--------------------------------------------------------------*/

/*----------------------------- main function -----------------*/
void main()
{
 mcu_init();
//static text
 Lcd_Out(1, 1, "VA:      VB:");
 Lcd_Out(2, 1, "VC:      VD:");
 Lcd_Out(3, 1, "Total:");
 Lcd_Out(4, 1, "% of VD:");
 while(1)
 {
batA=Adc_Read(0); //read batA
batB=Adc_Read(1);//read batB
batC=Adc_Read(2);//read batC
batD=Adc_Read(3);//read batD
 display_values();

 //compare values to control relay
 if(batD<1100)//need charging
 {
 PORTD.F0=1;//relay on
 }
  if (batD>=1200)//enough charging
 {
 PORTD.F0=0;//relay off
 }
 }

}
/*------------------------------ End of main-------------------*/

No comments:

Post a Comment