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.

pwm output based on the bit

I want to modulate/stop the pwm output based on the bit.

If bit is 1, then stop the pwm.if the bit is 0, then i start the pwm (by changing outmod or something else).

So for a given byte, how do I access the bits.

Can I use char variable to store the byte?

How to access bits of the char?

  • Hithesh said:
    How to access bits of the char?

    That's basic C.

    However, the MSP heades provide some convenience defines for bits.

    if (value & BIT0) {code if bit #0 is set}
    else { code if bit #0 is not set }

    value is the variable that holds your bit(s), and yes, it can be a char (or better, unsigned char). And it can hold 8 individual control bits.

    There are many, many other ways. Recursive, iterative, comparative (the example above is a simple comparative, and probably the one you might want to use)

  • So for 8-bits there are 8 íf' loops.

    Any way to reduce this.

  • Hithesh said:
    So for 8-bits there are 8 íf' loops. Any way to reduce this.


    Yes, the iterative or recursive approach, combined with a switch statement. Well, it won't be less code, since you still have 8 independent cases.

    unsigned char cnt = 1;
    do{
      switch(value & cnt){
        case BIT0: ...
          break;
        case BIT1: ...
          break;
        case BIT2: ...
          break;
        ...
      }
      cnt <<1;
    } while (cnt);

    However, if you have to do this if a bit is set and that if not, then the if/else chain is the simplest way to cover all 16 cases.

**Attention** This is a public forum