This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Structure problem

I'm trying  to catch a pulse on P1.

#define IR_PIN BIT4   //P1.4

I create a structure:

volatile struct
{
  unsigned char ToggBit: 1; 
  unsigned char Value: 1;        
}
rc5bits;

and a variable as alternative way:

unsigned char ToggBit;

void Get_IR_Data(void)
{
     //now i read the value of IR_PIN
    //rc5bits.ToggBit = P1IN&IR_PIN;      //doesn't work
    ToggBit = P1IN&IR_PIN;                     //works
  
    if (ToggBit)
    { P5OUT |= BIT1;}             //set led
    else
    { P5OUT &= ~BIT1;}        //clear led
   
    __delay_cycles(10000);
     
  }

if i pass the pin value to rc5bits.ToggBit  - it doesn't work , but if i pass it to ToggBit  - it work.

What's the problem? Can't grasp it!

  • Hi,

    in rc5bits.ToggBit = P1IN&IR_PIN; a implicit type convertion from char (8bit) to a one bit type is implicitly performed. I do not kown how the compiler is doing that, but could be that this is done by just using the LSB for that which would be P1IN.0 instead of P1IN.4 what you want. Try rc5bits.ToggBit = (P1IN&IR_PIN)>0; instead.

    Comments:

    Best is always to have a look into the assembler code for check such problems.

    Seems you would like to receive an infra red signal, I would recomment to do this by an timer pin capture function rather than by polling a port.

    regards

    spachner

  • assembler code looks like this:

    rc5bits.ToggBit = P1IN&IR_PIN;

    tst.b &P1IN

    bic.b #0x1, &rc5bits

    as i understand it tests P1IN and set or reset bit1 in register rc5bits - looks logical.

    i don't poll a port, i have an interrupt on pin , after the interrup i disable it , decode IR signal , and enable it again, the problem is to write the appropriate bits. Structure somehow doesn't work.

    If i do this:

    volatile struct
    {
      unsigned char bit0: 1;
      unsigned char bit1: 1;
      unsigned char bit2: 1;
      unsigned char bit3: 1;
      unsigned char bit4: 1;
      unsigned char bit5: 1;
      unsigned char bit6: 1;
      unsigned char bit7: 1;
    }
    Command;

    so Command.bit0 = Value; doesnt work.

     

     

  • Hi John7,

    the parentheses are missing! Try this:

    #define IR_PIN  (P1IN & BIT4)

    rc5bits.ToggBit = IR_PIN;  // equals rc5bits.ToggBit = (P1IN & BIT4);

    aBUGSworstnightmare

     

  • Nope, I do not think so. '=' has a lower priority than '&', so

    rc5bits.ToggBit = (P1IN & BIT4);

    rc5bits.ToggBit = P1IN & BIT4;

    is the same for the compiler.

    spachner

  • This might be a silly quesiton, but when you use the line of code

    rc5bits.ToggBit = P1IN&IR_PIN;

    are you also changing the if else statement's test criteria to

    if(rc5bits.ToggBit)

     or are you leaving it

    if (ToggBit)

     

**Attention** This is a public forum