Using Energia to code in c

Testing the Energia IDE installation

As a first step it is always a good idea to test that Energia works on your system. Open Energia and select File>Examples>1.Basics>Blink

Select the correct board and chip as shown below, Tools>Board

Select Board

Make sure you choose the correct chip part number as the launchpad ships with two chips the M430G2553 and the M430G2452.

Make sure that your board is connected via the USB cable to your computer. Then check that the correct serial port is selected Tools>Serial. If more than one port is visible you can find the correct one by simply removing the USB cable and observing which connection disappears. Plug it back in and select the correct one before continuing.

Select PortNow simply press the upload arrow and the blink code will be compiled and uploaded to the launchpad. The Red LED should then begin to flash at around 1Hz.

c programming

To programme in c just open a new Energia sketch File>New. We first need to delete the repopulated cpp code that appears in the new sketch.

New Sketch

If you then click on the little down arrow on the right of the tab bar and select new tab from the pop up menu a new tab will appear. Give this tab a name such as my_code.c and click OK. The important thing in the name is the .c extension as this tells the complier that the code in this file is c code. You can also create .h files to store the header data.

New Tab A new tab will appear. Paste the following simple blink code within the main function

#include <msp430.h>

volatile unsigned int i;

void main(void) {

P1DIR |= (1<<0);
P1OUT &= ~(1<<0);

while(1) {
P1OUT ^= (1<<0);
i = 65535;// SW Delay
while(i --!= 0);

The above blink code uses a software delay that is not efficient as the processor is continually decrementing i just to get a delay. A better approach to flashing an LED using a timer and interrupts to control the rate of blinking is given below.

/* Alternate Blink for MSP430G2553
 * Benn Thomsen November 2013*/
#include "msp430g2553.h"

void main(void) {
 WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

/* Use Factory stored presets to calibrate the internal oscillator */
 BCSCTL1 = CALBC1_1MHZ; // Set DCO Clock to 1MHz

P1DIR |= BIT0 + BIT6; // Set P1.0 and P1.6 to output direction
 P1OUT |= BIT6; // Set P1.6 to on
 P1OUT &= ~BIT0; // Set P1.0 to off

 /* Configure timer A as a clock divider to generate delay */
 TACCTL0 = CCIE; // Enable counter interrupt on counter compare register 0
 TACTL = TASSEL_2 +ID_3 + MC_1; // Use the SMCLK to clock the counter, SMCLK/8, count up mode
 TACCR0 = 10000-1; // Set maximum count (Interrupt frequency 1MHz/8/10000 = 12.5Hz)

__enable_interrupt(); // Enable interrupts.
_BIS_SR(CPUOFF + GIE); // Enter LPM0 with interrupts enabled

// TimerA interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
 P1OUT ^= BIT0 + BIT6; // Toggle P1.0 and P1.6 using exclusive-OR

Your final code should look like the screenshot below.

Blink Code

To compile and upload the code to the microprocessor first make sure that the MSP430 Launch pad is plugged into one of the USB ports of your computer, then click the upload arrow. The red and green LEDs should now flash alternatively at around 6Hz.

23 thoughts on “Using Energia to code in c

  1. Thank you Benn. I am relatively new to the MSP430 line, but finally found an example using the timer, via programming through Energia. The program works as described. Thanks again for providing.

  2. Ben, I just tried your example above and I can’t get it to work. Energia doesn’t recognise anything referenced in the header file. I have the header, but I can’t get it to use it. Any suggestions?

  3. I was searching for a solution to code in C for the msp430 on a mac without install CCS/Eclipse. Did not know it can be done in Energia. Thank you.

    Just an FYI… Not sure if it’s because I downloaded the latest version of Energia for OSX. Got ” error: ‘::main’ must return ‘int’.” So I had to change “void main(void)” to “int main(void)” and added return 0 in the end to fix the error.

  4. Have you tried using your own low level main function (as above) and still link some of the Arduino libraries, like Serial or SPI? I’m guessing you’d need to modify a linker script somewhere, but I haven’t found it.

    I realize it sort of goes against the title of you post, but I’d like to be able to write my own main and still use high-level libraries.

  5. Ben,
    Thanks for the great code. To use the computer while waiting for interrupt where would one use the void loop()? Why is int main() required since you just run it once? Could all the timer setup commands be done in setup? Does Energia take care of storing and replacing the PC and required registers? I can’t get the Serial.println to work any where. Thanks again for the great post.

    • Here you are mixing the Energia c++ language with c. In the Energia c++ language all your code must be called from either within the setup() or loop() functions.

      When using c all the code must be called from within the main function. Typically this is of the form:

      int main(){
      // Setup code goes here

      // The main program loop that runs forever goes here

      // If using interrupts put the CPU to sleep with Global interrupts enabled
      __bis_SR_register(CPUOFF + GIE);

      //This program may also make use of interrupts. Interrupt Service routines go here.

      #pragma vector=TIMER0_A0_VECTOR
      __interrupt void Timer_A0_ISR (void)
      // Code to run inside interrupt routine

      // Wake CPU up so that it enters the main loop just where it was previously asleep
      __bic_SR_register(CPUOFF); // Wake CPU

  6. hello everyone im in need of your help. im using msp430g2553 microcontroller. i want to interface hall effect flow sensor which produces pulse output. can anyone suggest me the code for interfacing the sensor with msp430 and count the number of pulses generated by it.

    • Use one of the timer 0 and set the timer clock source to be one of the external pins e.g. P1.0/TA0CLK. To select this as the timer clock source you set the timer control register TA0CTL = TASSEL_0 + MC_1 + ID_0;

    • One can’t upload assembly code from within Energia, however, one can use the MSP-GCC toolchain that comes with Energia to upload assembly. I have a set of batch files for windows or a makefile for Mac OS to do this available here

  7. i have got this kind of error in eclipse
    d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: tmp_and_fan_code.cpp.elf section `.text’ will not fit in region `rom’
    d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: section .vectors loaded at [0000ffe0,0000ffff] overlaps section .text loaded at [0000f800,00010a85]
    d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/../../../../msp430/bin/ld.exe: region `rom’ overflowed by 3074 bytes
    d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libcrt0.a(_copy_data.o): In function `__do_copy_data’:
    /root/mspgcc-20120406/BUILD/gcc/gcc/../../../gcc-4.6.3/gcc/config/msp430/crt0.S:195: relocation truncated to fit: R_MSP430_16_BYTE against symbol `__data_load_start’ defined in *ABS* section in tmp_and_fan_code.cpp.elf
    core.a(TimerSerial.cpp.o): In function `TimerSerial’:
    D:\energia-0101E0011\hardware\msp430\cores\msp430/TimerSerial.cpp:90: relocation truncated to fit: R_MSP430_16_BYTE against symbol `vtable for TimerSerial’ defined in .rodata._ZTV11TimerSerial section in core.a(TimerSerial.cpp.o)
    d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3/crt0ivtbl16.o: In function `__isr_14′:
    (.text+0x2): relocation truncated to fit: R_MSP430_16 against symbol `_unexpected_’ defined in .text.crt0 section in d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libcrt0.a(_unexpected_.o)
    tmp_and_fan_code.cpp.o: In function `loop’:
    D:\energia-0101E0011/tmp_and_fan_code.ino:20: relocation truncated to fit: R_MSP430_16 against symbol `__floatunsisf’ defined in .text section in d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libgcc.a(_usi_to_sf.o)
    D:\energia-0101E0011/tmp_and_fan_code.ino:22: relocation truncated to fit: R_MSP430_16 against symbol `__mulsf3′ defined in .text section in d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libgcc.a(_mul_sf.o)
    D:\energia-0101E0011/tmp_and_fan_code.ino:22: relocation truncated to fit: R_MSP430_16 against symbol `__mulsf3′ defined in .text section in d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libgcc.a(_mul_sf.o)
    D:\energia-0101E0011/tmp_and_fan_code.ino:22: relocation truncated to fit: R_MSP430_16 against symbol `__mulsf3′ defined in .text section in d:/energia-0101e0011/hardware/tools/msp430/bin/../lib/gcc/msp430/4.6.3\libgcc.a(_mul_sf.o)

    please help me

    • The frequency of the PWM signal is determined by the top count value of the counter (TA0CCR0) and the counter clock frequency. Fpwm = Fclk/TA0CCR0. Note for the timer Fclk is the system clock divided by the value of the timer clock divider.

  8. Hey Benn,
    first of all thank you very much for this great tutorial. It works realy great and exactly as discribed in your tutorial for my msp430- launchpad.
    Unfortunatly I have a few problems using energia to code in c if I want to use my stellais board.
    I guess the compiler doesn’t find the correct headers. I tried to include standard headers like stdint.h, stdbool.h and a lot more. I had to define absolute pathes to these header files. The list of errors was reduced but there are still a few problems I can not solve. Because I’m using Energia on several PCs I would prefer not to use absolute pathes to the header files.
    Do you have a hint or an idea how I can compile c-code fo my stellaris launchpad?
    Wondering if there are preferences or arguments for the gcc-compiler I have to change?

    Best regards Perezoso

    • Unfortunately I don’t have a stellar is launchpad to play with, however, I would have thought it using energia with c should work on this if:

      a) you make sure you have selected the correct board from the Tools>Board menu
      b) You add the correct header file to your code, include

  9. Pingback: Using Energia with EZ430-2013 LaunchPad on Linux | plashless

  10. msp430-g++: error: Files\energia-1.6.10E18-windows\energia-1.6.10E18\hardware\tools\msp430/include: No such file or directory


Leave a Reply to steve Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s