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.

Peripheral Rigister Question

Other Parts Discussed in Thread: MSP430G2231

I am having a tough time understanding how registers work.  I am working specifically with the MSP430g2231 from the LaunchPad.  I know Port 1 has 8 pins.  If I look at the memory map from the data sheet it says Port 1 is 8 bits (at least I think).  Therefore each pin seems as if it should be one bit.  This would make sense.  If the were all 0 you would have 0b00000000 or all ones 0b11111111.

However when I look further I see that Port 1 has 8 registers the most important being P1IN, P1OUT and P1DIR.  Each of these registers has a memory location 0x20, 0x21, and 0x22 respectively.  0x20 is 0b00100000.  If I understand it correctly each one of these registers has an effect on each of the 8 pins.  This makes me believe that each register is one byte.

If I look at it from the top down I get confused.  I thought I had a Port which is 8 bits in size giving me one byte.  Then I look at the registers P1IN etc and I think I have 8 registers each being 8 bits so then I would have 8 bytes.  Obviously this makes my thoughts on Port 1 being 8 bits seem wrong.  If I continue to try and make sense of it I get really confused.  I am going to try and explain.  If I use register P1IN as an example which is at memory location 0x20 this seems like it should be 1 bit.  In binary it is 0b00100000.  However I realize each pin can be set by each register.  So this would mean that 0x20 was a byte and I can change its 8 bit to one's or zeros.  If I were to type

P1IN = 0x00;

this sets everything to 0.  So if P1IN is at memory location 0x20 or 0b00100000 (I thought a bit) how can I set something below it to all 0's.  I thought 0x20 was the bit.

Clearly I am confused. I hope this makes sense to someone who can help me out.  If you could possibly go from the top down explaining what a Port is (byte or bits or what ever), then a register, on to the pins it would be greatly appreciated.  Maybe I have the order wrong.   I am not sure.

Take care, 

Jon

 

  • It is refreshing to see someone actually tries to understand something.

    Let us start with P1IN register. Yes, this register has an 8 bits “content”, each bit has to do with one of the 8 port 1 pins. BIT0 (the least significant bit) of P1IN reflects the input level at P1.0 pin. BIT7 (the most significant bit) of P1IN reflects the input level at P1.7 pin. The P1IN register also has an “address” which is actually 16 bit wide and is always 0B0000000000100000. The “address” is just how the CPU addresses this register; it does not change while the “content” of P1IN changes to reflect the input levels of P1.0 through P.7 pins.

    Now, since the content of P1IN only reflects the input voltage levels, "P1IN = 0x00;" does not serve any propose.  Instead, "a_variable = P1IN;', or "if (P1IN & 0x08) ...;" are legitimate usages. The compiler will look up in the header files and find P1IN has the address 0x0020. It will generate code to read the content of address 0x0020 and then either write that content into a_variable, or perform the "if" test.

    In the above explanation, I assumed that all port 1 pins are digital input pins. This is true after power-up or reset. There is another register P1DIR that can change this situation. The content of P1DIR is also 8 bits wide, each bit controls the direction of one of the 8 port 1 pins. When a bit in the P1DIR register is 0, the corresponding port 1 pin is an input pin. When a bit in the P1DIR register is 1, the corresponding port 1 pin is an output pin. After power-up or reset, the content of P1DIR is 0B00000000, which makes all port 1 pins as input pins. The CUP can, following the instructions of the firmware, change the content of P1DIR and make some or all port 1 pins as output pins. In order to do so, the CPU addresses P1DIR by its 16-bit address 0B0000000000100001.

    "P1DIR = 0x14;" is a legitimate usage. The compiler will generate code to change the content of address 0x0021 to 0x14. The effect is, P1.5 and P1.2 are outputs while other port 1 pins are inputs.

    The P1OUT register determines the digital voltage level for the output pins (as determined by the content of P1DIR register) of port 1. And there are a few more registers that deal with port 1.

    Hope the above get you started over the initial hurdle.

  • This is great information and has helped a lot.  Thanks for taking the time to respond to my questions.

    All the best,

    Jon

  • In addition to the explanations already given, there's more to know about port register naming and locations:

    On newer devices, port registers are intermixed. PORT1 and PORT2, while being independently usable, can be addressed as a combined 16 bit wide PORTA. This done by nesting the registers of PORT1 and PORT2. So if P1IN is 0x00020, P2IN is 0x00021. A word access to 0x00020 will therefore read/write both ports registers as if they were one single 16 bit wide port. Note that I wrote 20 bit values, as it is just a memory address and one newer MSPs, memory addresses can be 20 bit.

    Also, PORT1 and PORT2 have additional registers for controlling the interrupt functionality. (these registers are too intermixed so you can access them as one 16bit register). All following ports 3..12 (if the particular MSP has them) do not have these interrupt control registers.

    In the device datasheet (not users guide) is a diagram of the actual port wiring. It might help you to understand the mechanism. It's just necessary to know that each bit in a port register is associated to one particular port pin. And all registers of one port (P1IN/P1OUT/P1DIR etc.) refer with their bits to the same (8/16) port pins but do different things.

    Jon Thornham said:
    P1IN = 0x00;

    Won't do anything. P1IN is a read-only register and only reflects the current state of the associated (8) port pins. :)

  • Thanks to everyone who responded.  Your suggestions have definitely helped.  I also found the site below which is a great reference for a guy like me.

    http://mspsci.blogspot.com/

    Take care,

    Jon

**Attention** This is a public forum