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.

Require Tutorial for Programming using C Language of MSP430

Other Parts Discussed in Thread: MSP430F149, ENERGIA

Hey guys,

I want some basic tutorial for programming In C for MSP430.

  • I think your question is too generic. What are you having trouble with ?
  • Actually i can programming in assembly language but now i want to move on C, so i require some basic tutorial to learn with example code...

    I have basic knowledge of C, Now i require some tutorial for MSP programming with C

  • is there any one have tutorial?

  • Jignesh Doshi1 said:
    is there any one have tutorial?

    You shall split your "C for msp430" learning in two parts: 1) C programming 2) msp430 - related C stuff.

    1) There are loads of C learning resources around - various youtube video, courses and books. Just use search

    2)  You shall check workshop materials and videos. C source code examples also will be useful Check web page of the chip you use, for example C source examples of msp430x2 series

    Energia could help you get on C track too.

  • Hey guys,

    I want to generate 1Sec, 1mS delay for MSP430F149, so how i have to calculate?

  • You shall use timers. 

    Resources can be found at. Be sure to watch the video before proceeding. I advise you to complete the full training before you go any further.

    Teja

  • You can use the built-in function _delay_cycles.

    It will delay cycles not secods, so you will have to a bit of math first. I don't know your board, but if I searched correctly its 8MHz.

    This means it goes through 8 000 000 cycles in 1 secod. If you delay 8 000 000 cycles, you will have waited for 1 second.

    For 1 milisecond, divide it by 1000, 8 000 000 / 1 000 = 8 000. So I would say:

    1 second: _delay_cycles (8000000);
    1 milisec: _delay_cycles(8000);

    I hope you can use those in your project.

  • As We calculate 1us execution time for 1 instruction in 8051 Micro Controller.

    Like my crystal is 11.0592 Mhz in 8051, we can calculate 11.0592 / 12 ( its clock pulse) = 0.9216 Mhz and that we have to calculate 1 / 0.9216  = 1.08us / 1 cycle (Execution time).

    My question is how can we calculate like this in MSP430F149 Microcontroller.

  • When you use __delay_cycles(), you do not have to calculate, compiler calculates for you. Just make sure you pass MCLK cycles as an agument.

    [edit] For __delay_cycles(NCYCLES) Formula is: NCYCLES = (MCLK_freq/1000)*delay_msecs

  • User guide has all information you need. You will read it before posting your questions here.

  • I am posting my code in below..

    please verify it and give me feedback.

    Code is for 4 bit LCD 20*4 lines with MSP430.

    data pin is P1.4,1.5,1.6,1.7 and RS pin is P2.3 and EN pin is P2.2.

    #include <msp430.h>
    
    #define en BIT2;
    
    #define rs BIT3;
    
    #define rw BIT4;
    
    #define lcd_data_pin P1OUT;
    
    void lcd_ini();
    
    void lcd_command(unsigned char);
    
    void lcd_data(unsigned char);
    
    void lcd_welcome();
    
    void lcd_reset();
    
    void set_clk();
    
    void main(void)
    
    {
    
    unsigned char comm = 0x81,disp = 0x41;
    
    WDTCTL   = WDTPW | WDTHOLD;                // Stop watchdog timer
    
    set_clk();
    
    P1DIR |= BIT0 + BIT1 + BIT2 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7;
    
    P2DIR |= BIT2 + BIT3;
    
    lcd_ini();
    
    while(1)
    
    {
    
    //lcd_welcome();
    
    P2OUT &= ~BIT3;
    
    P1OUT = (comm & 0xF0);
    
    P2OUT |= BIT2;
    
    __delay_cycles(100);
    
    P2OUT &= ~BIT2;
    
    comm = comm << 4 ;
    
    P1OUT = (comm & 0xF0);
    
    P2OUT |= BIT2;
    
    __delay_cycles(100);
    
    P2OUT &= ~BIT2;
    
    __delay_cycles(500);
    
    P2OUT |= BIT3;
    
    P1OUT = (disp & 0xF0);
    
    P2OUT |= BIT2;
    
    __delay_cycles(100);
    
    P2OUT &= ~BIT2;
    
    disp = disp << 4 ;
    
    P1OUT = (disp & 0xF0);
    
    P2OUT |= BIT2;
    
    __delay_cycles(100);
    
    P2OUT &= ~BIT2;
    
    __delay_cycles(500);
    
    }
    
    }
    
    void set_clk()
    
    {
    
    do
    
    {
    
    IFG1 &= ~OFIFG;
    
    __delay_cycles(10000);
    
    __delay_cycles(10000);
    
    }
    
    while(OFIFG == 1);
    
    BCSCTL1 |= RSEL0 + RSEL1 + RSEL2; //LOW NOMINAL FREQUENCY
    
    BCSCTL2 |= SELS; //XT2 AS SMCLK SOURCE
    
    BCSCTL2 |= SELM1;
    
    BCSCTL2 &= ~SELM0;
    
    BCSCTL2 &= ~DIVS0 + ~DIVS1; //SMCLK DIVIDER 1
    
    return;
    
    }
    
    void lcd_ini()
    
    {
    
    lcd_reset();
    
    lcd_command(0x28);
    
    // __delay_cycles(5000);
    
    lcd_command(0x0C);
    
    __delay_cycles(128000);
    
    }
    
    void lcd_reset()
    
    {
    
    P2OUT &= ~BIT3;
    
    P2OUT |= BIT2;
    
    lcd_command(0x20);
    
    __delay_cycles(128000);
    
    lcd_command(0x20);
    
    _delay_cycles(128000);
    
    lcd_command(0x20);
    
    // _delay_cycles(128000);
    
    }
    
    void lcd_command(unsigned char comm)
    
    {
    
    P2OUT &= ~BIT3;
    
    P1OUT = (comm & 0xF0);
    
    P2OUT |= BIT2;
    
    __delay_cycles(100);
    
    P2OUT &= ~BIT2;
    
    comm = comm << 4 ;
    
    P1OUT = (comm & 0xF0);
    
    P2OUT |= BIT2;
    
    __delay_cycles(100);
    
    P2OUT &= ~BIT2;
    
    __delay_cycles(500);
    
    }
    
    void lcd_data(unsigned char disp)    // function to send data on LCD
    
    {
    
    P2OUT |= BIT3;
    
    P1OUT = (disp & 0xF0);
    
    P2OUT |= BIT2;
    
    __delay_cycles(10);
    
    P2OUT &= ~BIT2;
    
    disp = disp << 4 ;
    
    P1OUT = (disp & 0xF0);
    
    P2OUT |= BIT2;
    
    __delay_cycles(10);
    
    P2OUT &= ~BIT2;
    
    _delay_cycles(50);
    
    }

  • any 1 has verify it?
  • , E2E is not the place for your code to be verified. You write your code, debug it and if you face any issue, then you should post it here. Don't expect us to review each and every line of code when we're not paid to do so.

    Teja

  • Jignesh Doshi1 said:
    I want to generate 1Sec, 1mS delay for MSP430F149, so how i have to calculate?

    That has nothing to do with using c instead of assembly.

  • , I know you are not paid...but i have problem to display char in LCD that's why i have posted my code and request to verify if is there any bug in programming then tell me so that i can go further...
  • I have resolve my problem...
  • Excellent!

    From next time on, please open a new thread instead of continuing on this thread.

    Teja
  • It looks like you've received two suggestions for how to create a 1us delay.

    1. The __delay_cycles() intrinsic function is the easiest solution. The downside of this solution is that it uses the CPU, and thus it consumes more power than the other solution. (Note, using the Energia/Arduino delay function does not use extra power, as they were able to implement it with a timer.)
       
    2. The timer solution offered by teja is the better overall solution as you can put the CPU into low-power mode while the timer is running. That said, it does require work to setup and configure. As you pointed out in your question, the time base needs to be determined based upon the clock rate.
       
      The workshop chapter teja referenced provides some examples for calculating how to setup the timer. The timers (e.g. TIMER_A) on the MSP430 allow quite a bit of flexibility. You are able to select which system clock will drive the timer; you also have the ability to divide-down that clock before it reaches the timer/counter. We created a little worksheet in order to calculate the timer settings, similar to your working out the details for your previous MCU.
       

    Take Care,
    Scott

**Attention** This is a public forum