Error Message: operator [] not applicable to this operands[]

General discussion on mikroC.
Post Reply
Author
Message
antony1925
Posts: 18
Joined: 20 Nov 2011 08:54

Error Message: operator [] not applicable to this operands[]

#1 Post by antony1925 » 22 Nov 2011 14:12

Hello Everyone,

This is my code for the tachometer and I do not understand why i get this error message at this line when I bitwise shift left and OR the numbers.

Code: Select all

Rot_time = Rot_PH<<8|Rot_PL;  //combining both hi and low byte of rotation period
      Rpm = 240000000/Rot_time;
The error message says "Operator[] not applicable to this operands[]"

Please help me resolve this error, this is the only error I get and that is stopping me from testing.

Cheers
Antony

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

Re: Error Message: operator [] not applicable to this operan

#2 Post by Sparky1039 » 23 Nov 2011 02:03

It may be because you did not define the operation precedence for this line. Basically the compiler is confused on which task to do first, shift left 8, or bit wise OR.
Try using parenthesis (just like in math equations) to set the order of precedence and see if the error goes away.

Code: Select all

Rot_time = (Rot_PH << 8) | Rot_PL;  //combining both hi and low byte of rotation period 

antony1925
Posts: 18
Joined: 20 Nov 2011 08:54

Re: Error Message: operator [] not applicable to this operan

#3 Post by antony1925 » 23 Nov 2011 08:05

HI,

I have tried with the parenthesis but this time I get multiple errors telling me to change different parameters on the same line and in the next line when I am calculating the RPM.

I have attached a screenshot of what the error messages looks like.
errormessage.jpg
errormessage.jpg (160.9 KiB) Viewed 3101 times
If you have any ideas please help me solve this issue

cheers

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Error Message: operator [] not applicable to this operan

#4 Post by filip » 23 Nov 2011 11:10

Hi,

Please, can you post here the minimal code that can be compiled ?

Regards,
Filip.

antony1925
Posts: 18
Joined: 20 Nov 2011 08:54

Re: Error Message: operator [] not applicable to this operan

#5 Post by antony1925 » 23 Nov 2011 12:42

Hi Filip,

Sorry for the late reply.

Here is my code

Code: Select all

unsigned double Rpm = 0;
unsigned float Rot_time = 0;     // rot_PH + rot_PL
unsigned float Pulse_HSL;   // holding falling edge time for start of timing sequence
unsigned float Pulse_HSH;
unsigned float Pulse_EL;   // rising edge for end time sequence
unsigned float Pulse_EH;
unsigned float Rot_PH = 0;
unsigned float Rot_PL = 0;
void main()
{
      Soft_Uart_Init(PORTC,7,6,57600,0);
      delay_ms(500);
      TRISD= 0;  //Set all PORTD for output
      PORTD= 0;
      TRISC= 0x02;  //RC1 as input for CCP2
      
      INTCON  = 0;  //Disable all Interrupts for initialization of other registers
      T3CON   = 0;  //0000 0000  was $C0 Sets Timer1 as source clock for CCP1 and CCP2 leaves timer3 off
      CCP2CON = 0x04; // set for every faling edge, 05 every rising edge capture
      //CCP2CON = 0x06;  //set for 4th rising edge, $07 Initially Set CCP2 for 16th Rising Edge Capture
      T1CON   = 0x01;  //set for 8 bit time read, prescaler of 1 and turn timer 1 on
      //T1CON = 0x81; // set for 16bit read, prescaler 1 and timer1on
      TMR1L   = 0x00; //zero timer registers
      TMR1H   = 0x00;
      
while(1)
{

      if(PIR2.CCP2IF == 0 && CCP2CON == 0x04) // if flag is set and pulse is trigerred

      Rot_time = (Rot_PH << 8) | Rot_PL;  //combining both hi and low byte of rotation period
      Rpm = (240000000)/(Rot_time);
      Soft_Uart_write(rpm);
}
}
void interrupt()
{
      INTCON.GIE =0; // clearing GIE to prevent any other interrupts while handling this one
      
      if(PIR2.CCP2IF == 1)
      {
        switch(CCP2CON)
        {
          case 0x04: // start time of the sequence i.e. falling edge and gets start time
          Pulse_HSL = CCPR2L; // capture pulse high start time
          Pulse_HSH = CCPR2H; // get captured time high byte
          CCP2CON = 0x05;
          PIR2.CCP2IF = 0; // reset the flag
          break;
          
          case 0x05: // end time sequence so timing sequence complete, calculate time intervals
          Pulse_EL = CCPR2L; // get time of the rising edge
          Pulse_EH = CCPR2H;
          Rot_PL = Pulse_EL - Pulse_HSL; // rotation time = Pulse time HI + pulse time LOW
          //if(STATUS == 0){ Pulse_EH = Pulse_EH - 1; }
          Rot_PH = Pulse_EH - Pulse_HSH;
          CCP2CON = 0x04; // set start next timing sequence for falling edge.
          PIR2.CCP2IF = 0;
          break;
          
          default:  rpm =0;
         } // switch
       }  // if
}   // interrupt
Thank you!

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

Re: Error Message: operator [] not applicable to this operan

#6 Post by Sparky1039 » 23 Nov 2011 18:31

Seeing the whole code explains allot. Couple of issues...

First, there is no such data type in C called "unsigned float" or "unsigned double". Floating point data types have no "sign". They are by default already signed but is a special way.
Second, performing bit shifting on a "float" or "double" is not allowed because floating point data types are a special case where integer and fractional information are encoded into the whole value. Bit shifting and "ORing" will corrupt this data.
Third, there is no reason to use floating point data type to hold the captured value from the CCP module. The result output is a 16-bit count value that contains no fractional information so floats are not needed.

What you really need to do is carefully work through the requirements of your design and figure out the largest data type (size) needed for the computations. Also evaluate if floating point data types are really necessary. 98% of the time they are not. All of what you are trying to do can be done using integer math (whole numbers) and scaling.

antony1925
Posts: 18
Joined: 20 Nov 2011 08:54

Re: Error Message: operator [] not applicable to this operan

#7 Post by antony1925 » 23 Nov 2011 23:46

Thank you spark1039.

I will try and get back to you ASAP!

Regards
Antony

antony1925
Posts: 18
Joined: 20 Nov 2011 08:54

Re: Error Message: operator [] not applicable to this operan

#8 Post by antony1925 » 24 Nov 2011 00:00

Hi Spark1039,

Awesome, it compiles without any errors.

I have used 'int' and basically changed all the operands I am using.

Thanks for your help :D

Sincere Regards

Antony

Post Reply

Return to “mikroC General”