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.

CCS/msp430fr5857: GPIO PxIN register.

Part Number: MSP430FR5857

Tool/software: Code Composer Studio

Hey,

This question might seem a little childish but hear me out.

Each bit in each PxIN register reflects the value of the input signal at the corresponding I/O pin when the
pin is configured as I/O function. These registers are read only.
• Bit = 0: Input is low
• Bit = 1: Input is high

This is what is given in the datasheet. My question is that if I set one pin as output high then will it show as high for that particular bit in PxIN register.?

Thank you.

Viswanath

  • Hi Viswanath,

    look to the port schematic in datahseet e..g. page #85 in the block diagram shows that output signal is fed back to input means once you change the PxOUT value while your PxDIR is set to ouput you will see this in the PxIN register.

    Best regards,
    Dietmar
  • Hey Deitmar,
    Thank you for answering.
    I asked the question because I wrote a code to interface my MSP430fr5857 with a keypad.

    #include <msp430.h>

    /*
    * main.c
    */
    //const char keypad[] = {'1','2','3','4','5','6','7','8','9','*','0','#'};


    unsigned int keydetect()
    {
    unsigned int p=15;
    unsigned int i=0;
    for(i=0; i<3; i++)
    {
    unsigned int temp=0x00;
    unsigned int t2=0x00;
    unsigned int t3=0x00;
    P3DIR |= BIT0|BIT1|BIT2|BIT3;
    P3OUT &= 0x00;
    P3REN |= BIT4|BIT5|BIT6;
    P3OUT |= (0x01<<i);
    temp |= P3OUT;
    t2 |= P3IN;
    t3 |= t2|temp;
    switch(t3)
    {
    case 0x11:
    {
    p=0;
    break;
    }
    case 0x21:
    {
    p=1;
    break;
    }
    case 0x41:
    {
    p=2;
    break;
    }
    case 0x12:
    {
    p=3;
    break;
    }
    case 0x22:
    {
    p=4;
    break;
    }
    case 42:
    {
    p=5;
    break;
    }
    case 14:
    {
    p=6;
    break;
    }
    case 24:
    {
    p=7;
    break;
    }
    case 44:
    {
    p=8;
    break;
    }
    case 18:
    {
    p=9;
    break;
    }
    case 28:
    {
    p=10;
    break;
    }
    case 48:
    {
    p=11;
    break;
    }
    }
    }
    return p;
    }
    void led()
    {
    P1DIR = BIT0|BIT1|BIT2|BIT3;
    unsigned int key;
    key=keydetect();
    P1OUT |=key;
    }

    unsigned int main()
    {

    WDTCTL = WDTPW | WDTHOLD;
    PM5CTL0 &= ~LOCKLPM5;
    while(1)
    {
    led();
    }
    }

    Written above is my code. I have 4 leds connected which will glow in response to the button pressed. But it does not seem to work even though the code is correct. Can you go through the code and tell me if I have done any mistakes?
    Thank you
    Viswanath.
  • sorry. This is the modified code. Please go through this.


    #include <msp430.h>

    /*
    * main.c
    */
    //const char keypad[] = {'1','2','3','4','5','6','7','8','9','*','0','#'};


    unsigned int keydetect()
    {
    unsigned int p=15;
    unsigned int i=0;
    for(i=0; i<3; i++)
    {
    unsigned int temp=0x00;
    unsigned int t2=0x00;
    unsigned int t3=0x00;
    P3DIR |= BIT0|BIT1|BIT2|BIT3;
    __delay_cycles(1600000);
    P3OUT &= 0x00;
    P3REN |= BIT4|BIT5|BIT6;
    P3OUT |= (0x01<<i);
    temp |= P3OUT;
    t2 |= P3IN;
    switch(t3)
    {
    case 0x11:
    {
    p=0;
    break;
    }
    case 0x21:
    {
    p=1;
    break;
    }
    case 0x41:
    {
    p=2;
    break;
    }
    case 0x12:
    {
    p=3;
    break;
    }
    case 0x22:
    {
    p=4;
    break;
    }
    case 0x42:
    {
    p=5;
    break;
    }
    case 0x14:
    {
    p=6;
    break;
    }
    case 0x24:
    {
    p=7;
    break;
    }
    case 0x44:
    {
    p=8;
    break;
    }
    case 0x18:
    {
    p=9;
    break;
    }
    case 0x28:
    {
    p=10;
    break;
    }
    case 0x48:
    {
    p=11;
    break;
    }
    }
    }
    return p;
    }
    void led()
    {
    P1DIR = BIT0|BIT1|BIT2|BIT3;
    unsigned int key;
    key=keydetect();
    P1OUT |=key;
    }

    unsigned int main()
    {

    WDTCTL = WDTPW | WDTHOLD;
    PM5CTL0 &= ~LOCKLPM5;
    while(1)
    {
    led();
    }
    }
  • Hi Viswanath,

    so I assume you have you LEDs connected to P1.0 - P1.3 but I do not 100% undertstand what you do with Port3?

    You set P3.0-P3.3. to output low and shift a 1 from P3.0 to P3.3 in the for loop.
    Before you enable the pull downs on P3.4 - P3.6. So where are your buttons connected and how they are connected?
    Is P3.0-P3.3. somehow connected to P3.4 - P3.6?


    Also why you write P3OUT and P3IN not directly to your variables but with a logical OR?

    In your modified code the switch structure selects accroding variable "t3"  it is never written compared to the previous post, is this intended?

    Why you not simply connect the buttons to input pins with pull up/down enabled and read the corresponding input register??

  • Deitmar,
    P1.0- P1.3 are connected to LEDS. I am using a 3x4 matrix keypad (3 colums and 4 rows) in which the rows are connected to P3.0 - P3.3 and the colums are connected to P3.4 - P3.6. What I thought of doing was to initially keep the columns as input (pulled low) and then rows and output low. Then while checking I will make each row high in sequence(by use of for loop) and then check for key press. If any of the column pins are high(implying a key has been pressed) then the value of key being pressed in 'p' and then given to the led() function. This makes the respective led's glow and then I know which key is pressed . And about t3, I forgot to remove the variable declaration while modifying the code.

    Each key acts as a switch. When a key is pressed the switch is closed hence connecting the respective column and row pins.

    Viswanath
  • And instead of t3 in the switch case i am using t2.
  • Viswanath,

    so you have 12 buttons where you activate row by row and check the inputs on P3.4-P3.6 to check for the column, right?

    But you make t2 = t2 | P3IN which means the value from your previous for loop is stored on t2 because you loop 0x01, 0x02, 0x04, 0x08 in the for loop I expect to have 0xXF in the end on t2 right? Can you  please check this with the debugger?

    Also you write p to key and this and again OR this to P1.0 which means you write the value of the enalbed row to P1IN sooner or later I expect that all LEDs are on right? Means you need to ensure LEDs were turned off when the button is not pressed anymore.

    So when you debug this project what is the value of the variable key when no button is pressed?
    Means can you be a bit more specific what is not working? How the variables are looking like?

    Hope this makes sense.

  • Hi Deitmar.

    Thank you for your help. I have edited the code and this code works.

    This is what I wanted to do.

    #include <msp430.h>
    
    /*
     * main.c
     */
    //const char keypad[] = {'1','2','3','4','5','6','7','8','9','*','0','#'};
    unsigned int key= 0x00;
    unsigned int p;
    
    unsigned int keydetect()
    {
        unsigned int temp,i,t2;
        p=15;
        for(i=0; i<4; i++)
        {
            temp=0x00;
            t2=0x00;
            P3DIR =0x00;
            P3DIR = BIT0|BIT1|BIT2|BIT3;
            P3OUT &= 0x00;
            P3REN =0x70;
            temp = (0x01<<i);
            P3OUT =temp;
            t2 = P3IN;
            switch(t2)
            {
            case 0x11:
            {
                p=0;
                break;
            }
            case 0x21:
            {
                p=1;
                break;
            }
            case 0x41:
            {
                p=2;
                break;
            }
            case 0x12:
            {
                p=3;
                break;
            }
            case 0x22:
            {
                p=4;
                break;
            }
            case 0x42:
            {
                p=5;
                break;
            }
            case 0x14:
            {
                p=6;
                break;
            }
            case 0x24:
            {
                p=7;
                break;
            }
            case 0x44:
            {
                p=8;
                break;
            }
            case 0x18:
            {
                p=9;
                break;
            }
            case 0x28:
            {
                p=10;
                break;
            }
            case 0x48:
            {
                p=11;
                break;
            }
            }
        }
        return p;
    }
    void led()
    {
        P1DIR = BIT0|BIT1|BIT2|BIT3;
        key=keydetect();
        P1OUT =key;
    }
    
    unsigned int main()
    {
    
        WDTCTL = WDTPW | WDTHOLD;
        PM5CTL0 &= ~LOCKLPM5;
        while(1)
        {
            led();
        }
    }
    I just have to think how to work with debouncing of keys but thats for later

    Thank you for all the help.



    Viswanath.

**Attention** This is a public forum