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.

Basic ADC Program

Hi,


I was going through the example code for ADC that is given with msp430. I couldnt understand the following.: ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE

My question is. ADC10CTL0 is 16 bit register. so how does adding few bits makes up the 16 bit value?

  • The line you posted is equivalent to the following:

    ADC10CTL0 = 0 + ADC10SHT_2 + ADC10ON + ADC10IE


    So, you start out with a value of 0 (in binary: 0b0000000000000000) and add the bit values you want (such as ADC10IE) and end up with a binary representation where some bits are 0 and some bits are 1.

  • Thanks Brian,


    This way of setting the bits looks complicated. Are the msp430 registers bit addressable? if thats the case why can we just set the individual bits? I guess that would be easy to do so.

    I also didnt understand following code:

    ADC10CTL0 |= ENC + ADC10SC


    If this line of code just sets the bits, then why do we need |= operator? I mean to say whats the difference between

    two line of code?

  • Ronak Sakaria said:

    I also didnt understand following code:

    ADC10CTL0 |= ENC + ADC10SC

    This line says "Take the current value of ADC10CTL0 register, and bitwise-OR the value with the values of ENC and ADC10SC then write the new value back to the ADC10CTL0 register".

    In other words, keep all the current register settings, and enable the converter and start a conversion.

    This is known as a Read-Modify-Write (RMW) sequence and is very common in embedded programming.

    Another version of the same principle is used to clear some but not all bits in a register. Take for instance:

    ADC10CTL0 &= ~(ENC + ADC10SC);

    This would clear the ENC and ADC10SC bits and leave all the other bits unchanged.

  • Ronak Sakaria said:
    Are the msp430 registers bit addressable

    Ronak, the MSP does not support bit-addressing. For the CPU., hardware registers are just normal memory, like ram or flash.
    In the past, there have been bitfield definitions for those registers. However, this kind of ‘convenience# has two major drawbacks.
    First, the C standard does not define the order in which bits are placed in a bitfield. So the code wouldn’t be portable (you could end up upside-down when switching from one compiler to the other). Also, bitfields on byte-sized registers are not covered by the C standard at all.
    But the major problem is that processor registers are volatile (and need to). Because reading them and writing to them may have side-effects. The compiler has to access them exactly as often and in the order as they are used in the source code.
    Imagine what happens when you set two port output bits one after another in two bit-set operations. Right, the ports will toggle one after another. But if you use a direct assignment, they will change at the same time. Which can’t be done with a bitfield.
    So using bitfields is not only not portable, it is also inefficient. And worst case some things can’t be done (or will at least yield different results regarding timing).
    See it like entering a number: first you enter all the digits, then you press enter once (write it to the register). Not each one independently.

    Ronak Sakaria said:
    ADC10CTL0 |= ENC + ADC10SC
    If this line of code just sets the bits, then why do we need |= operator? I mean to say whats the difference between two line of code?

    This line sets the two bits, but leaves all other bits unaltered. While in the original line, all bits are set or cleared according to the assigned value.

**Attention** This is a public forum