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

accessing bitwise data in a data buffer

hi all,
iam a new to embedded programming and keil software.
i am struck with a problem how to access a bit in a data buffer.

the algo i need to implement is

data_buf of 32 bits is stored in the micro controller. i need to send those bits bit by bit. ie. starting from bit 0 to bit 31.

can anybody pls suggest me how to write a program in keil.

thanks in advance
uday

Parents
  • "i did not ask to teach me bitwise operators mr. andrew."

    True; I'm just suggesting that you can achieve your aim by using the standard ANSI 'C' bitwise logical operators (let's call them "mask" operators if it helps) and the standard ANSI 'C' shift operators.

    You didn't answer whether you're already familiar with 'C', so Look them up in your 'C' textbook, if necessary. Then think about what you're trying to achieve, what the operators do, and thus how to proceed.

    "i have declared a buffer holding a 32 bit value."

    No you haven't:

    unsigned int data_buf[40];
    You have declared a buffer containing 40 unsigned int values - in Keil C51, that's forty 16-bit values.
    See the Manual
    P0^3 = data_buf[i];
    Here, you're trying to send a whole unsigned int (16 bits) to a single port pin.

    Note that the sytax is wrong: the P0^3 is a Keil-specific extension to the language - it can only be used on the right-hand side of a bit definition.
    Elsewhere, the '^' operator performs its standard ANSI 'C' exclusive-OR function.
    See the Manual

    "my problem was how to access each bit of the buffer and send it."

    Exactly - and you can achieve this using the standard ANSI 'C' shift and "mask" operators!

    BTW: Note that you have commented-out the entire code:
    /********** comment starts here
    unsigned int data_buf[40];
    unsigned int i;
    for (i=0;i<=39;i++)
    {
    P0^3 = data_buf[i];
    }
    *********/ comment ends here!

    As you said you're new to embedded programming, note also that the 8051 is an 8-bit processor; therefore working with 16-bit and larger values is relatively inefficient.
    Since your array has only 40 members, you can use an unsigned char as the index instead of an int.

Reply
  • "i did not ask to teach me bitwise operators mr. andrew."

    True; I'm just suggesting that you can achieve your aim by using the standard ANSI 'C' bitwise logical operators (let's call them "mask" operators if it helps) and the standard ANSI 'C' shift operators.

    You didn't answer whether you're already familiar with 'C', so Look them up in your 'C' textbook, if necessary. Then think about what you're trying to achieve, what the operators do, and thus how to proceed.

    "i have declared a buffer holding a 32 bit value."

    No you haven't:

    unsigned int data_buf[40];
    You have declared a buffer containing 40 unsigned int values - in Keil C51, that's forty 16-bit values.
    See the Manual
    P0^3 = data_buf[i];
    Here, you're trying to send a whole unsigned int (16 bits) to a single port pin.

    Note that the sytax is wrong: the P0^3 is a Keil-specific extension to the language - it can only be used on the right-hand side of a bit definition.
    Elsewhere, the '^' operator performs its standard ANSI 'C' exclusive-OR function.
    See the Manual

    "my problem was how to access each bit of the buffer and send it."

    Exactly - and you can achieve this using the standard ANSI 'C' shift and "mask" operators!

    BTW: Note that you have commented-out the entire code:
    /********** comment starts here
    unsigned int data_buf[40];
    unsigned int i;
    for (i=0;i<=39;i++)
    {
    P0^3 = data_buf[i];
    }
    *********/ comment ends here!

    As you said you're new to embedded programming, note also that the 8051 is an 8-bit processor; therefore working with 16-bit and larger values is relatively inefficient.
    Since your array has only 40 members, you can use an unsigned char as the index instead of an int.

Children
No data