MSP430 Launchpad

Discussion in 'Embedded Systems and Microcontrollers' started by tenacity2986, Nov 1, 2012.

  1. tenacity2986

    Thread Starter New Member

    Oct 31, 2012
    9
    0
    When programming the MSP430 on the launchpad, I am trying to setup delays and am looking at the blinking light LED as an example. If I have a for loop that counts to say 1 million, will it take 1 second with a 1Mhz clock?

    Im trying to understand the relationship between iterations and clock cycles.
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,367
    No. You have to examine the number of clock cycles required for each and every instruction within the software loop.

    It is much better to use the hardware timer for timing loops.
     
  3. tenacity2986

    Thread Starter New Member

    Oct 31, 2012
    9
    0
    do all msp430's have built in RTC? The TI website says nothing about having them... it seems like the entire family has it but im not really sure.
     
  4. panic mode

    Senior Member

    Oct 10, 2011
    1,321
    304
    if one loop takes 20 clock cycles and you use 1MHz clock, then 1MHz/20=50000 loops per second so in this example you would make loop of 50000. you can increase length of the loop by adding more instructions (NOPs for example).

    each instruction takes certain number of clocks, in general some instructions take longer time to execute (more clocks) than others. moreover this varies from platform to platform.

    your intuition is correct, longer loops will keep the mcu busy longer and if you use this as a way to create timing for your indicators, they will flash slower with greater counter range. but it does not mean that loop of 1000000 on 1MHz will take 1s. it depends on what is inside the loop and how efficient loop is.
     
  5. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    I think that all msp430s have a low frequency oscillator, which can be easily turned into RTC.
    As for the timing, the msp has on average 4-5 clock cycles per instruction, so counting to a million should take more than 10 sec, my guess is 15.
     
  6. tenacity2986

    Thread Starter New Member

    Oct 31, 2012
    9
    0
    Strange... so im pretty sure im doing this right.. but on the launchpad for the msp430 by TI, both leds on the board don't blink at the same time! i wonder why?
    #include <msp430g2211.h>

    unsigned int i = 0; // Initialize variables. This will keep count of how many cycles between LED toggles


    void main(void)
    {
    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer. This line of code is needed at the beginning of most MSP430 projects.
    // This line of code turns off the watchdog timer, which can reset the device after a certain period of time.

    P1DIR |= 0x41; // P1DIR is a register that configures the direction (DIR) of a port pin as an output or an input. 00100001 = 0x41


    // To set a specific pin as output or input, we write a '1' or '0' on the appropriate bit of the register.


    // P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>


    // Since we want to blink the on-board red LED, we want to set the direction of Port 1, Pin 0 (P1.0) as an output

    // We do that by writing a 1 on the PIN0 bit of the P1DIR register
    // P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>
    // P1DIR = 0000 0001
    // P1DIR = 0x01 <-- this is the hexadecimal conversion of 0000 0001



    for (;;) // This empty for-loop will cause the lines of code within to loop infinitely
    {



    //P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR operation (^=)
    P1OUT ^= 0x01;

    // P1OUT is another register which holds the status of the LED.
    // '1' specifies that it's ON or HIGH, while '0' specifies that it's OFF or LOW
    // Since our LED is tied to P1.0, we will toggle the 0 bit of the P1OUT register



    for(i=0; i< 20000; i++); // Delay between LED toggles. This for-loop will run until the condition is met.
    //In this case, it will loop until the variable i increments to 20000.
    }
    }
     
  7. tenacity2986

    Thread Starter New Member

    Oct 31, 2012
    9
    0
    hey if i set P1DIR |= 0x1E; and P1OUT ^= 0x1E; in a infinite loop.... shouldn't all the outputs go high and low at the same time?


    The pin after the begginnig of the range (00011110, third LSB) is blinking opposite to 2nd LSB. All others are blinking in unison.
     
  8. tenacity2986

    Thread Starter New Member

    Oct 31, 2012
    9
    0
    #include <msp430g2211.h>

    unsigned int i = 1; // Initialize variables. This will keep count of how many cycles between LED toggles

    void main(void)
    {
    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer. This line of code is needed at the beginning of most MSP430 projects.
    // This line of code turns off the watchdog timer, which can reset the device after a certain period of time.

    P1DIR = 0XFF; //TURNS ALL PORT 1 PINS P1.0 THROUGH P1.7 TO BE OUTPUTS
    P1OUT = 0; // SETS ALL OUTPUTS TO 0

    for(i=1; i<2; i++)
    {
    P1OUT = 0x41;
    _delay_cycles(30000000);
    }

    P1OUT = 0;
    }


    Works much better now :)
     
  9. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Doesn't your C compiler have a function to delay in mS?

    You should be able to use Delay_mS(500) or Delay_mS(1000) etc.
     
  10. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    First, please tell us which compiler you're using? Are you using CCS (Code Composer Studio) v4 or v5?
     
  11. tenacity2986

    Thread Starter New Member

    Oct 31, 2012
    9
    0
    sorry yes i am using code composer version 5 and it does have a function for delay....

    Got it all workingg .... what I really want to do next is set UART up on P1.1 (Tx) to transmit serially to a display... hopefully i can get this working
     
  12. tenacity2986

    Thread Starter New Member

    Oct 31, 2012
    9
    0
    Is there a way to reverse the reset pin? Currently when you depress the reset, it grounds the pin, and upon release brings it high resettting...

    I need the chip to reset when it sees ground, and do nothing when it sees an open circuit at reset (floating) ... those are the only two states i have... maybe a transistor would be cheaper NPN style :)
     
  13. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    The reset pin on the Launchpad is "active low". That is, the MSP430 chip is held in reset as long as the pin is held low. Are you saying you want to reverse that?
     
Loading...