This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

passing carry bit to a function as parameter in C

Dear all,

I have a small keyboard in my application and I poll all the keys periodically. I can read all the key values with 2 bytes. Next, I check the bits of these bytes for specific keys.

In my program, I have function (check_button()) which gets the byte value and the bit index of the required key. It extracts the bit using '>>' and '<<', and interprets the value.

for (button_index=0; button_index<8; button_index++)
   check_button(button_port_1, button_index);

:
:
:

void check_button(unsigned char buttons, unsigned char button_index) {
   get the bit using << and >>
   process it
}

However, since this button checks are performed reqularly, I'm trying to re-engineer them for higher performance.

I'm not much familier with assembly, but following seems as a better way to do keyboard check:

for (button_index=0; button_index<8; button_index++) {
   button_port_1 >>= 1; (I need to get the carry bit)
   check_button(carry_bit_taken_above, button_index);
}

Finally, my question is: Is it possible to get the carry bit as above in some way, and pass it as bit parameter to a function in C?

Thanks for reading,

  • Carry bit is accessible as 'CY' in C51;

    But beware - there is not necessarily any guarantee that the carry bit will be retained between 'C' statements.
    If you really need to be working at that level of machine-dependance, you probably need to do it in assembler.

    Note that C51 does provide a bit data type - could you use that, rather than rely specifically on the CPU's Carry Flag?

  • Andrew,

    Thanks for the reply.

    I know the bit data type. But, fastest way of accessing bits of a byte seems as using RRC and accessing carry flag.

    Am I wrong?

    Maybe I should change the question as follows:


    What is the fastest method to access any bit of a byte programmitically?

  • The question is really how to test a given bit in a byte. Here is code I often use.

    typedef unsigned char BYTE;
    
    BYTE code  BMaskC[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
    
    main()
    {
      BYTE Dummy;
      BYTE Button,Idx;
    
      if( Button & BMaskC[Idx] )
        Dummy++;
    } 
    
                 ; FUNCTION main (BEGIN)
    0000 E500        R     MOV     A,Idx
    0002 900000      R     MOV     DPTR,#BMaskC
    0005 93                MOVC    A,@A+DPTR
    0006 5500        R     ANL     A,Button
    0008 6002              JZ      ?C0002
    000A 0500        R     INC     Dummy
    000C         ?C0002:
    000C 22                RET     
    

  • Jon,

    Thanks for the reply. I'll use this approach for bit access.