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/MSP430F2491: MSP430F2491

Part Number: MSP430F2491
Other Parts Discussed in Thread: MSP430G2553

Tool/software: Code Composer Studio

dio_t1.rar

when i try to define dirction port by bitwise opperation like that

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define SETBIT(VAR,BITNO)          VAR|= (1<<BITNO)   <---------------------------

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

it work  fine and i can made pin number 0 had output diection like that

SETBIT(PORT1,BITNO) // made pin number 0 output diection .

but when i  made liberary to called in main.c

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Set_Pin_direction(u8 port_id,u8 pin_id,u8 pin_direction)
{
    if (port_id<=PORT_D && pin_id<=DIO_PIN_7)
    {
       if(pin_direction==OUTPUT)
       {
           switch(port_id)
           {
           case PORT_A:  SET_BIT(DDRA_REG,pin_id);  break;                 <------ this is from my made  DIO_prog.c
           case PORT_B:  SET_BIT(DDRB_REG,pin_id);  break;
           case PORT_C:  SET_BIT(DDRC_REG,pin_id);  break;
           case PORT_D:  SET_BIT(DDRD_REG,pin_id);  break;
           }
       }
       else if (pin_direction==INPUT)
       {
           switch(port_id)
           {
           case PORT_A:  CLR_BIT(DDRA_REG,pin_id);  break;
           case PORT_B:  CLR_BIT(DDRB_REG,pin_id);  break;
           case PORT_C:  CLR_BIT(DDRC_REG,pin_id);  break;
           case PORT_D:  CLR_BIT(DDRD_REG,pin_id);  break;
           }
       }
    }
    else
    {
        /*do nothing*/
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

i made the porogram like that to made it more modelar and avoid spageti code

it made activate pin 1 instead pin 0 in port 1 and that made me very upset

so

why did the program do that (shifting pin number by one )?

thank you

  • i upload dio driver with main.c

  • Unfortunately, I can't open that .RAR file, for whatever reason.

    If you can just post one line of code that causes an incorrect action, and say what action it took, that might be enough.

    My first guess is that you're doing something like:

    > Set_pin_direction(PORT_A, _BV(0), OUTPUT);

    where  _BV(0) is (already) defined as (1 << 0), i.e.  a bit position, not a bit number.

  • i am do that so what is the deffrent ?

  • Can you post an example of a (failing) call to this function?

    My guess (since I haven't seen it) is that you're inadvertantly doing the shift twice.

  • How is "PIN0" defined?

    I wrote a wrapper around your function and ran it on a G2ET Launchpad with a G2553 (also an F2-series MCU). At the end, P1DIR=0x11 and P2DIR=0x01.

    I'm not sure what we're doing differently.

    #include <msp430.h>
    #include <stdint.h>
    #define G2553   1
    typedef uint8_t u8;
    #define SET_BIT(VAR,BITNO)          VAR|= (1<<BITNO)   //<---------------------------
    #define CLR_BIT(VAR,BITNO)          VAR&= ~(1<<BITNO)   //<---------------------------
    #define DIO_PIN_7   7
    #define PORT_A  0
    #define PORT_B  1
    #if !G2553
    #define PORT_C  2
    #endif
    #define PORT_D  4       // just for the check
    #define DDRA_REG    P1DIR
    #define DDRB_REG    P2DIR
    #if defined(PORT_C)
    #define DDRC_REG    P3DIR
    #define DDRD_REG    P4DIR
    #endif
    #define INPUT   0
    #define OUTPUT  1
    void Set_Pin_direction(u8 port_id,u8 pin_id,u8 pin_direction)
    {
        if (port_id<=PORT_D && pin_id<=DIO_PIN_7)
        {
           if(pin_direction==OUTPUT)
           {
               switch(port_id)
               {
               case PORT_A:  SET_BIT(DDRA_REG,pin_id);  break;       //          <------ this is from my made  DIO_prog.c
               case PORT_B:  SET_BIT(DDRB_REG,pin_id);  break;
    #if defined(PORT_C)
               case PORT_C:  SET_BIT(DDRC_REG,pin_id);  break;
               case PORT_D:  SET_BIT(DDRD_REG,pin_id);  break;
    #endif
               }
           }
           else if (pin_direction==INPUT)
           {
               switch(port_id)
               {
               case PORT_A:  CLR_BIT(DDRA_REG,pin_id);  break;
               case PORT_B:  CLR_BIT(DDRB_REG,pin_id);  break;
    #if defined(PORT_C)
               case PORT_C:  CLR_BIT(DDRC_REG,pin_id);  break;
               case PORT_D:  CLR_BIT(DDRD_REG,pin_id);  break;
    #endif
               }
           }
        }
        else
        {
            /*do nothing*/
        }
    }
    int main(void)
    {
    	WDTCTL = WDTPW | WDTHOLD;	// stop watchdog timer
    	
    	Set_Pin_direction(PORT_A,0,OUTPUT); // P1.0 as output
        Set_Pin_direction(PORT_A,4,OUTPUT); // P1.4 as output
        Set_Pin_direction(PORT_B,0,OUTPUT); // P2.0 as output
    #if defined(PORT_C)
        Set_Pin_direction(PORT_C,0,OUTPUT); // P3.0 as output
        Set_Pin_direction(PORT_D,0,OUTPUT); // P4.0 as output
    #endif
        while (1)
        {
            LPM0;
        }
        /*NOTREACHED*/
    	return 0;
    }
    

  • /*
     * types.h
     *
     *  Created on: ??‏/??‏/????
     *      Author: Hero
     */

    #ifndef TYPES_H_
    #define TYPES_H_
    typedef unsigned  char                   u8;
    typedef signed    char                   s8;
    typedef unsigned  int                    u16;
    typedef signed    int                    s16;
    typedef float                            f32;
    typedef double                           d64;
    typedef unsigned  long int               u32;
    typedef signed    long int               s32;




    #endif /* TYPES_H_ */
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /*
     * bitwise.h
     *
     *  Created on: ??‏/??‏/????
     *      Author: Hero
     */

    #ifndef BITWISE_H_
    #define BITWISE_H_
    #define SETBIT(VAR,BITNO)          VAR|= (1<<BITNO)
    #define CLRBIT(VAR,BITNO)          VAR&=~(1<<BITNO)
    #define TOGBIT(VAR,BITNO)          VAR^= (1<<BITNO)
    #define GETBIT(VAR,BITNO)          VAR= (VAR>>BITNO)&1
    #define SETPORT(VAR)               VAR|=0xff
    #define CLRPORT(VAR)               VAR=0x00
    #define GIVPORT(VAR,VAL)           VAR=VAL




    #endif /* BITWISE_H_ */

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /*
     * DIO_private.h
     *
     *  Created on: ??‏/??‏/????
     *      Author: Hero
     */

    #ifndef DIO_PRIVATE_H_
    #define DIO_PRIVATE_H_
    #define P1IN                   *((volatile u8*)0x0020)
    #define P1OUT                  *((volatile u8*)0x0021)
    #define P1DIR                  *((volatile u8*)0x0022)

    #define P2IN                   *((volatile u8*)0x0028)
    #define P2OUT                  *((volatile u8*)0x0029)
    #define P2DIR                  *((volatile u8*)0x002A)

    #define P3IN                   *((volatile u8*)0x0018)
    #define P3OUT                  *((volatile u8*)0x0019)
    #define P3DIR                  *((volatile u8*)0x001A)

    #define P4IN                   *((volatile u8*)0x001C)
    #define P4OUT                  *((volatile u8*)0x001D)
    #define P4DIR                  *((volatile u8*)0x001E)

    #define P5IN                   *((volatile u8*)0x0030)
    #define P5OUT                  *((volatile u8*)0x0031)
    #define P5DIR                  *((volatile u8*)0x0032)

    #define P6IN                   *((volatile u8*)0x0034)
    #define P6OUT                  *((volatile u8*)0x0035)
    #define P6DIR                  *((volatile u8*)0x0036)


    #endif /* DIO_PRIVATE_H_ */
    /*
     * DIO_prog.c
     *
     *  Created on: ??‏/??‏/????
     *      Author: Hero
     */
    #include"types.h"
    #include"bitwise.h"
    #include"DIO_private.h"
    #include"DIO_interface.h"
    void Set_BIT_DIR(u8 Port_NO, u8 BIT_NO,u8 Direction)
    {
        if(Port_NO<=Port6 && BIT_NO<=PIN7)
        {
            if(Direction==OUTPUT)
            {
                switch(Port_NO)
                {
                case Port1: SETBIT(P1DIR,BIT_NO); break;
                case Port2: SETBIT(P2DIR,BIT_NO); break;
                case Port3: SETBIT(P3DIR,BIT_NO); break;
                case Port4: SETBIT(P4DIR,BIT_NO); break;
                case Port5: SETBIT(P5DIR,BIT_NO); break;
                case Port6: SETBIT(P6DIR,BIT_NO); break;
                }
            }
            else if (Direction==INPUT)
            {
                switch(Port_NO)
                {
                case Port1: CLRBIT(P1DIR,BIT_NO); break;
                case Port2: CLRBIT(P2DIR,BIT_NO); break;
                case Port3: CLRBIT(P3DIR,BIT_NO); break;
                case Port4: CLRBIT(P4DIR,BIT_NO); break;
                case Port5: CLRBIT(P5DIR,BIT_NO); break;
                case Port6: CLRBIT(P6DIR,BIT_NO); break;
                }
            }
        }
        else
        {
            /*Do no thing*/
        }
    }
    void Set_BIT_VAL(u8 Port_NO, u8 BIT_NO,u8 Value)
    {
        if((Port_NO<=Port6)&&(BIT_NO<=PIN7))
        {
            if(Value==HIGH)
            {
                switch(Port_NO)
                {
                case Port1: SETBIT(P1OUT,BIT_NO); break;
                case Port2: SETBIT(P2OUT,BIT_NO); break;
                case Port3: SETBIT(P3OUT,BIT_NO); break;
                case Port4: SETBIT(P4OUT,BIT_NO); break;
                case Port5: SETBIT(P5OUT,BIT_NO); break;
                case Port6: SETBIT(P6OUT,BIT_NO); break;
                }
            }
            else if(Value==LOW)
            {
                switch(Port_NO)
                {
                case Port1: CLRBIT(P1OUT,BIT_NO); break;
                case Port2: CLRBIT(P2OUT,BIT_NO); break;
                case Port3: CLRBIT(P3OUT,BIT_NO); break;
                case Port4: CLRBIT(P4OUT,BIT_NO); break;
                case Port5: CLRBIT(P5OUT,BIT_NO); break;
                case Port6: CLRBIT(P6OUT,BIT_NO); break;
                }
            }
        }
        else
        {
            //do no thing
        }
    }
    //u8 GET_BIT_VAL(u8 Port_NO, u8 BIT_NO)
    //{
    //    u8 u8result;
    //    if((Port_NO<=Port6)&&(BIT_NO<=PIN7))
    //    {
    //        switch(Port_NO)
    //        {
    //        case Port1: u8result= GETBIT(P1OUT,BIT_NO); break;
    //        case Port2: u8result= GETBIT(P2OUT,BIT_NO); break;
    //        case Port3: u8result= GETBIT(P3OUT,BIT_NO); break;
    //        case Port4: u8result= GETBIT(P4OUT,BIT_NO); break;
    //        case Port5: u8result= GETBIT(P5OUT,BIT_NO); break;
    //        case Port6: u8result= GETBIT(P6OUT,BIT_NO); break;
    //        }
    //    }
    //    return u8result;
    //}
    void TOG_BIT_VAL(u8 Port_NO, u8 BIT_NO)
    {
        if((Port_NO<=Port6)&&(BIT_NO<=PIN7))
        {
            switch(Port_NO)
            {
            case Port1: TOGBIT(P1IN,BIT_NO); break;
            case Port2: TOGBIT(P2IN,BIT_NO); break;
            case Port3: TOGBIT(P3IN,BIT_NO); break;
            case Port4: TOGBIT(P4IN,BIT_NO); break;
            case Port5: TOGBIT(P5IN,BIT_NO); break;
            case Port6: TOGBIT(P6IN,BIT_NO); break;
            }
        }
    }
    void SET_PORT_VAL(u8 Port_NO,u8 Value)
    {
        if(Port_NO<=Port6)
        {
            if(Value==HIGH)
            {
            switch(Port_NO)
            {
            case Port1: SETPORT(P1IN); break;
            case Port2: SETPORT(P2IN); break;
            case Port3: SETPORT(P3IN); break;
            case Port4: SETPORT(P4IN); break;
            case Port5: SETPORT(P5IN); break;
            case Port6: SETPORT(P6IN); break;
            }
            }
            else if(Value==LOW)
            {
            switch(Port_NO)
            {
            case Port1: CLRPORT(P1IN); break;
            case Port2: CLRPORT(P2IN); break;
            case Port3: CLRPORT(P3IN); break;
            case Port4: CLRPORT(P4IN); break;
            case Port5: CLRPORT(P5IN); break;
            case Port6: CLRPORT(P6IN); break;
            }
            }
        }
        else
        {
            //do no thing
        }
    }
    void SET_PORT_DIR(u8 Port_NO,u8 Direction)
    {
        if(Port_NO<=Port6)
        {
            if(Direction==OUTPUT)
            {
            switch(Port_NO)
            {
            case Port1: SETPORT(P1DIR); break;
            case Port2: SETPORT(P2DIR); break;
            case Port3: SETPORT(P3DIR); break;
            case Port4: SETPORT(P4DIR); break;
            case Port5: SETPORT(P5DIR); break;
            case Port6: SETPORT(P6DIR); break;
            }
            }
            else if(Direction==INPUT)
            {
            switch(Port_NO)
            {
            case Port1: CLRPORT(P1DIR); break;
            case Port2: CLRPORT(P2DIR); break;
            case Port3: CLRPORT(P3DIR); break;
            case Port4: CLRPORT(P4DIR); break;
            case Port5: CLRPORT(P5DIR); break;
            case Port6: CLRPORT(P6DIR); break;
            }
            }
        }
        else
        {
            //do no thing
        }
    }
    void GIVE_PORT_VAL(u8 Port_NO,u8 PORT_VAL)
    {
        if(Port_NO<=Port6)
        {
            switch(Port_NO)
            {
            case Port1: GIVPORT(P1IN,PORT_VAL); break;
            case Port2: GIVPORT(P2IN,PORT_VAL); break;
            case Port3: GIVPORT(P3IN,PORT_VAL); break;
            case Port4: GIVPORT(P4IN,PORT_VAL); break;
            case Port5: GIVPORT(P5IN,PORT_VAL); break;
            case Port6: GIVPORT(P6IN,PORT_VAL); break;
            }
        }
        else
        {
            //do no thing
        }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /*
     * DIO_interface.h
     *
     *  Created on: ??‏/??‏/????
     *      Author: Hero
     */

    #ifndef DIO_INTERFACE_H_
    #define DIO_INTERFACE_H_
    #define         HIGH                   1
    #define         LOW                    0
    #define         INPUT                  0
    #define         OUTPUT                 1
    #define         Port1                  1
    #define         Port2                  2
    #define         Port3                  3
    #define         Port4                  4
    #define         Port5                  5
    #define         Port6                  6
    #define         PIN0                   0
    #define         PIN1                   1
    #define         PIN2                   2
    #define         PIN3                   3
    #define         PIN4                   4
    #define         PIN5                   5
    #define         PIN6                   6
    #define         PIN7                   7
    /****************************************************order*********************************************************/
    void Set_BIT_DIR(u8 Port_NO, u8 BIT_NO,u8 Direction);
    void Set_BIT_VAL(u8 Port_NO, u8 BIT_NO,u8 Value);
    u8 GET_BIT_VAL(u8 Port_NO, u8 BIT_NO);
    void SET_PORT_VAL(u8 Port_NO,u8 Value);
    void SET_PORT_DIR(u8 Port_NO,u8 Direction);
    void GIVE_PORT_VAL(u8 Port_NO,u8 PORT_VAL);
    #endif /* DIO_INTERFACE_H_ */
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    #include <msp430.h>
    #include"bitwise.h"
    #include"types.h"
    #include"DIO_interface.h"
    #include <msp430.h>
    #include  <msp430f2491.h>
    int main (void)
    {
       WDTCTL = WDTPW | WDTHOLD;
       BCSCTL1 = CALBC1_1MHZ;       // Set range
       DCOCTL = CALDCO_1MHZ;         // Set DCO step + modulation = 1MHz clock
       Set_BIT_DIR(Port1, PIN0,OUTPUT);
        //P1OUT = 0x00;
        CLRPORT(P1OUT);

        volatile u8 i;
        while(1)
        {
                   Set_BIT_VAL(Port1, PIN0 ,HIGH);
                           
                              SETBIT(P1OUT,PIN1);
                              _delay_cycles(1000);
                           
                              CLRBIT(P1OUT,PIN1);
                              _delay_cycles(1000);
      }
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    that is my code and i want to kow why did when i used : Set_BIT_DIR(u8 Port_NO, u8 BIT_NO,u8 Direction);

    select pin number 1 insted pin number 0 ?

  • I built this for the G2553 (G2ET) and I don't see the behavior you describe. Using the debugger "Registers" view:

    >P1DIR goes 0x00->0x01 based on Set_BIT_DIR(Port1, PIN0,OUTPUT);

    >P1OUT goes 0x00->0x01 based on Set_BIT_VAL(Port1, PIN0 ,HIGH);

    >P1OUT goes 0x01->0x03 based on SETBIT(P1OUT,PIN1);

    >P1OUT goes 0x03->0x01 based on CLRBIT(P1OUT,PIN1);

    >After that P1OUT alternates between 0x01 and 0x03.

    This is what I would expect to happen from this code. Did you maybe mean to use PIN0 instead of PIN1 in the SETBIT/CLRBIT sequence? 

  • P1DIR goes 0x00->0x01 based on Set_BIT_DIR(Port1, PIN0,OUTPUT);

    i can not understanding clearly what did you mean ?

    but i see that you said "P1DIR goes 0x00->0x01 based on Set_BIT_DIR(Port1, PIN0,OUTPUT); " is that mean (selectring pin 0 to pin 1)?

    how can i fix this code ?

  • The call asked to set bit 0 ("PIN0") of P1DIR to 1 and it did. The result 0x01 has bit 0 set (and no others).

    I guess I don't understand the question.

  • ok but in my workspace it sit pin1 in port 1 instite pin0 and this is my program

    so

    i think it work in your workspace and not mine

    and

    that is the problem.

  • The code you posted sets both bit 0 and bit 1 in P1OUT, so I'll set that aside for a moment.

    As I understand it, when you call "Set_BIT_DIR(Port1, PIN0,OUTPUT);" it changes P1DIR from 0x00 to 0x02. Is that correct?

  • Set_BIT_DIR(u8 Port_NO, u8 BIT_NO,u8 Direction) the problem is when i select

    Set_BIT_DIR(port1, pin1,output);

    it made pin0 is output instead to pin1.

    that may problem and i attach from protuse register simulation that phenomena i made code revision many time but i can not resolve the problem .

    did the problem in the protuse not in the code ???

  • When I added "Set_BIT_DIR(Port1, PIN1,OUTPUT);" it changed P1DIR to 0x02 (as expected).

    I'm not familiar with the ISIS program from your screenshot. Is it a simulator?

    I've been running on an MSP430G2553 on a Launchpad, and using the debugger "Register" view. Do you have an actual device to try?

  • i understand now and i made sure that the problem in protuse , i donot have the devise now due to i work from remote area far way from my gear and lab , so that i use protuse i thank the drive is ok , and that resolve my question thank you .