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.

Msp430 LED toggle

Other Parts Discussed in Thread: ENERGIA, MSP430WARE

Dear all,

i am using energia for programming msp430g2 launch pad i am having problem in writing code for toggle led using a button switch . can anybody give me the code .

The code should do;

button pressed and realsed - led on

button again pressed and released - led off

and so onnn.....

please give me energia code. 

  • Hey Aman,

    Listen aman, This is an E2E (engineer to engineer) forum. No one is going to be paid here for providing code. This is TIE2E community forum not energia so ask there.

    Are you a very very newbie in the field of electronics and programming thats why u asking such silly question. made code using CCS and then if you have problem then feel free to ask.

     

    Regards,

    Dharmendra Sharma

    INDIA

    __________________________________
    Do, click "Verify Answer", if any of my replies helps solve your problem.

  • Aman.

    BTW. 

    HINT for coding..

    >>>>> use flag variable 

    Regards,

    Dharmendra Sharma

    INDIA

    __________________________________
    Do, click "Verify Answer", if any of my replies helps solve your problem.

     

  • I am new at using these microcontrollers and also i am not that much perfect at programming and moreover i am not a engineer

  • Aman,

    ok.. I found a similar from my old codes. Don't forget to make input pin pull down. 




    #include <msp430.h> /* * main.c */ #define RED BIT0 // 00000001 #define GREEN BIT6 // 01000000 #define SENSOR BIT1 // 00000010 unsigned char flag=0; int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer BCSCTL1 = CALBC1_16MHZ; DCOCTL = CALDCO_16MHZ; // using DCO (calibrated) at 16 MHZ P1DIR |= RED + GREEN; // bit 0 and 6 as output P1SEL=0X00; // set IO as function of port P2DIR=0X00; //P2REN=0X00; while(1) { while(( P2IN & SENSOR) == SENSOR) { flag++;flag=flag%2; _delay_cycles(1600000);// 50ms delay } if(flag==1) { P1OUT |= (BIT0 + BIT6); } if(flag==0) { P1OUT &= ~(BIT0 + BIT6); } } }

    Regards,

    Dharmendra Sharma

    INDIA

    __________________________________
    Do, click "Verify Answer", if any of my replies helps solve your problem.

  • Dharmendra Sharma said:
    unsigned char flag=0;

    Flags are commonly used to indicate only one Signal-Flag (one bit);

    typedef struct FlagBits {
    	unsigned char Flag1		: 1 ;	// bit 0
    	unsigned char Flag2		: 1 ;	// bit 1
    	unsigned char Flag3		: 1 ;	// bit 2
    	unsigned char Flag4		: 1 ;	// bit 3
    	unsigned char Flag5		: 1 ;	// bit 4
    	unsigned char Flag6		: 1 ;	// bit 5
    	unsigned char Flag7		: 1 ;	// bit 6
    	unsigned char Flag8		: 1 ;	// bit 7
    } FlagBits;
    
    typedef union Flags {
    	unsigned char	All;
    	FlagBits		Bit;
    } Flags;
    
    Flags	myFlags;
    
    void Main (void)
    {
    	myFlags.All = 0;
    	myFlags.Bit.Flag1 = 0;
    	myFlags.Bit.Flag2 = 1;
    	// ...
    }
    

    Dharmendra Sharma said:
    flag++;flag=flag%2;

    This is a complex way to toggle between ‘0’ and ‘1’ -> flag ^= 1; is much shorter. BTW the wile loop will exit with a random flag value.

  • Leo,

    I do agree with you. But few things, 

    1. I found this code from my old collection, It was one of the my starting codes for MSP430, why I am going to make a complete code for someone, do I have much free time?

    2. he (aman) is a newbie, not an engineer , not have basic knowledge of C/C++. What would you think he would be able to catch the things like using structure ('.' (dot) operator). I also made some comments after some lines for his ease. 

    3. Is there any standered rules for C/C++?

    4. Is there is any error with logic??

    I appreciate your effort but not the way, you should reply it to the person how asked the question.

    Take it easy.

    Regards,

    Dharmendra Sharma

    INDIA

    __________________________________
    Do, click "Verify Answer", if any of my replies helps solve your problem.

  • Dear Aman:

    It's not easy learning how to program an MSP430, especially on your own. I think it's great that you have the interest and desire, but it does take a lot of patients and effort. Don’t give up, and for every hurdle you cross, the more you will learn of how exciting and gratifying this hobby can be.

    It looks like you want to use the Energia  language to program the MSP430. As a beginner you will want to create very simple projects. And those simple projects, like blinking an LED, have very little difference in whether you use the Energia language or the C programming language. The C language is actually very easy to learn on your own, and you don’t need to learn everything about it, just the basics.

    So if you are serious about programming the MSP430 as a hobby, I recommend learning the C programming language. It will take just as much time and effort to learn the energia programming language as compared to the C programming language for writing little programs to blink LEDs, control switches, and such.

    If you do decide to take my advice, go to your local library and check out this little book, “The C Programming Language” by Brian Kernighan and Dennis Richie. Read the first three or four chapters. They are very short, easy to read, and you could probably do it in a day or so. If you are in highschool, you can handle this book. Then you can download a free copy of Code Composer Studio from Texas Instruments for creating small C programming projects. The Code Composer Studio has a help system that explains how to create projects. It's not hard to create a project.

    Texas Instruments also provides a bunch of free code examples for all of its microcontrollers, and one of them is a simple little blinking LED program. The code examples are all packaged in a zip file, and the package is called MSP430ware. You can find a link to the zip file in the “Tools and Software” section of the Texas Instruments “Microcontrollers (MCU)” home page. Here is the link:

    http://www.ti.com/lsds/ti/microcontrollers_16-bit_32-bit/tools_software.page

     

    And just for kicks, there is free MSP430 Teaching CD you may download for free. It can be found here:

    http://e2e.ti.com/group/universityprogram/educators/w/wiki/2043.msp430-teaching-rom.aspx

    If you ever make it to program an LED, then next try writing a program for a seven segment LED display so you can display some numbers on it.

     

    Good luck Aman, and have some fun!

**Attention** This is a public forum