[PIC]Calculation Problem

Status
Not open for further replies.

shanix

Newbie level 3
Joined
Oct 28, 2014
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
21
Hi,

Im just learning pi16 programing using mikroc pro for pic and im having trouble displaying my variable which is X1 in lcd. This is my code

Code:
int main2 (unsigned int a[]); //function to get array largest element position

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;

unsigned int DataA[3] = {9,2,3};
unsigned int DataB[3]=  {1,9,3};
unsigned int DataC[3]=  {1,2,9};

float Time[3]= {0.2,0.4,0.8};

float ta,tb,tc;
int index;
float V1,tab1,tcb1,L1=1.00,X1;
char txt[15];


void main() {

    PORTA = 0;
    TRISA = 0X01;
    TRISB =0 ;
    PORTB = 0;
    TRISD =0 ;
    PORTD = 0;

    LCD_Init();
    LCD_Cmd(_LCD_CURSOR_OFF);
    LCD_Cmd(_LCD_CLEAR);
    LCD_Out(1, 1, "ANSWER:");

      index = main2(DataA); //get DataA max element position
      ta = Time[index];       // use the position to get data from Time
      index = main2(DataB);
      tb = Time[index];
      index = main2(DataC);
      tc = Time[index];

      tab1 = ta-tb;  
      tcb1 = tc-tb;

      delay_ms(200);

                      
         ///for calculation
         if(tab1 < tcb1){
         V1 = (float)1/(2*tcb1);
         X1 = 0.5*( (V1*tab1)+ (0.5*1) );
         }

         else if(tab1 > tcb1){
         V1 = (float)1/(2*tab1);
         X1 = (1-0.5)*( (V1*tab1)+ (0.5*1) );
         }

         FloatToStr(X1, txt);
         delay_ms(500);
         LCD_Out(2,1,txt);

      while (1){}  
}


int main2 (unsigned int a[]){
 int pos;
 unsigned int c;
 unsigned int maximum = a[0];
 unsigned int x;

  for (c = 1; c < 3; c++)
  {
    x = a[c];
    if ( x > maximum)
    {
       maximum  = a[c];
       pos = c;
    }
  }
  
 return pos;
}


The lcd displays correctly if only i changed the if else condition with something like this
if(1 < 2){//perform calulation}
else if(1 > 2){//perform calulation}

Im just learning pic hope someone could help me out with this..thank you.
 

Hi thankx for the reply,

I have tried print out tab1 and tcb1 on the lcd like this and the answer is correct which is tab1 = -0.2 tcb1 = 0.4

Code:
FloatToStr(tcb1, txt);
delay_ms(500);
LCD_Out(2,1,txt);

But for some reason my lcd on protues is acting kind of weird. It is blinking non stop. It will only work proper if i changed tab1 and tcb1 variable to a constant value such as 1 inside the if else statement like this

Code:
 if(tab1 < tcb1){
         V1 = (float)1/(2*1);              //V1 = (float)1/(2*tcb1);  tcb1 changed to 1
         X1 = 0.5*( (V1*1)+ (0.5*1) );// X1 = 0.5*( (V1*tab1)+ (0.5*1) ); 
         }

         else if(tab1 > tcb1){
         V1 = (float)1/(2*1);                      //V1 = (float)1/(2*tcb1);
         X1 = (1-0.5)*( (V1*1)+ (0.5*1) );  // X1 = (1-0.5)*( (V1*tab1)+ (0.5*1) );
         }
 
Last edited by a moderator:

Re: changed code in if statement


Code C - [expand]
1
2
3
4
5
6
7
8
if(tab1 < tcb1){
  V1 = (float)1/(2*1);                 //V1 = (float)1/(2*tcb1);  tcb1 changed to 1
  X1 = 0.5*( (V1*1)+ (0.5*1) ); // X1 = 0.5*( (V1*tab1)+ (0.5*1) );
  }
else if(tab1 > tcb1){
  V1 = (float)1/(2*1);                        //V1 = (float)1/(2*tcb1);
  X1 = (1-0.5)*( (V1*1)+ (0.5*1) );  // X1 = (1-0.5)*( (V1*tab1)+ (0.5*1) );
  }

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…