Tutorial 2:
What is energia?
Energia is the program that we are going to be using to communicate with our launchpad, Energia is an open source prototyping platform, this means that everyone can upload their codes and it receives support by a really big community of users. This program was designed for the MSP 430 and it has a really simple and clean interface which makes things simpler for us to understand, and another good thing abouth this program is that the code is like the Arduino micro-controller.
Summary:
In this tutorial we will learn what is Energia, and teach our MSP 430 it's first trick how to make the onboard LED's blink as we want.
Pre-written functions of energia that we are going to be using:
This functions are already on Energia and are there for us to use it and make our life a little easier:
- void setup () {}: its always at the beginning of the code, is where we initialize our launchpad pins as inputs or outputs and it goes inside the brackets "{}", pretty much what goes in void setup only needs to happen once.
- void loop () {}: as its name suggests the code inside " the loop", will be repeated over and over again, this is where the sketch spends most of it's lifetime. In a blink sketch , the repetitive task would be to blink over and over again.
- pinMode (parameter1, parameter2,): this functions is to set up certain pin as an INPUT or as an OUTPUT. Its always inside the void setup, and that is because in void setup we setup what we are going to be using, and determine input or output is a setup. It consists of 2 parameters, parameter 1 is which pin we want to setup, and parameter 2 if we want it as an INPUT or an OUTPUT.
Ex. pinMode (2, OUTPUT). Here we setup pin 2 as an output, which makes sense because pin 2 is the RED_LED so a light is an OUTPUT and not a input. We could put RED_LED, A0, P1_0 instead of 2 and it would be exactly the same thing, because those are nicknames that Energia recognizes, it can be seen on the pinout. - digitalWrite (parameter1, parameter 2): this function allows to determine the state of certain pin as HIGH or LOW, HIGH being equal to send 3V to that pin and LOW send 0V, so we can say that HIGH is on and LOW is off. parameter 1 is which pin we want to setup and parameter 2 if we want it with HIGH or LOW voltage (on or off).
Ex. digitalWrite(2, HIGH). Here we send 3V to pin 2 which is the RED_LED, we can say that we are turning on the RED_LED(pin 2). Remember that pin 2 = as putting RED_LED, A0, P1_0. - delay (parameter 1): The last function of this sketch, this function makes the program pause for a specific amount of time. This function only has one parameter, which is the number of mili-seconds to pause for.
Ex. delay (1000). Here we pause the program at that specific moment for 1000 mili-seconds which is equal to 1 full second. - Comments: first thing that you are going to see in the sketch that we are going to be using, are a bunch of sentences in green, those sentences or paragraphs in green are called comments, and those are in the code just to help us, you can see the comments in Energia but once you upload the code to the MSP 430 is like they never existed. To put comments in your code just to help yourself of what is each thing, or help others, you just need to put "//" before a sentence, but that is just for one line comments, if the comment is really long and consists of several lines, start the comment with "/*" and finish it with "*\"
Step 1:
Download Energia from the official webpage, make sure to download the newest version of the program. once you download it, unzip it to the location of your preference, the folder should be named Energia followed by the version number, just click on the folder and there should be an Energia icon below all the folders.
step 2:
Once you have Energia up and running on your computer, lets teach our launchpad it first trick. First of all connect you MSP 430 to your computer using the included USB cable. Now go to Energia: Tools > Board >Launchpad MSP 430G2xxxx <--- There you need to select the MSP 430 that you are using, the way to know which MSP 430 you have, go ahead and look at the brain of your launchpad and in the middle of the chip if you take a good look at it, you will be able to see that it has the model there. So make sure to select the right one!!
STEP 3:
Now that Energia is set up properly, we need to go to Energia to import an already prewritten example of Energia.
File> Examples> 1. Basics> Blink
What this example code does is that it makes an LED of our launchpad blink.This example is from the public domain, so everyone can use, that is one of the cool things of Energia, we have a lot of codes available to us.
File> Examples> 1. Basics> Blink
What this example code does is that it makes an LED of our launchpad blink.This example is from the public domain, so everyone can use, that is one of the cool things of Energia, we have a lot of codes available to us.
STEP 4:
To get our code from Energia to the launchpad, we first need to convert the sketch into something that the launchpad can understand. This process is called compiling. We can tell energia to compile our sketch by pressing the checkmark button on the top left of the window. A sketch is a code before its compiled.
STEP 5:
Once our sketch is compiled we can upload it to the brain of our launchpad by pressing the arrow button right beside the compiling button (checkmark). This will automatically compile our sketch and the upload it to the MSP 430.
STEP 6:
Right after the code is done uploading, the launchpad should automatically start running the code, that is now stored in the microcontroller brain.
At this point, your launchpad should be toggling the RED_LED once per second
At this point, your launchpad should be toggling the RED_LED once per second
COngratulations you just taught your launchpad its first trick! :D
But now with the help of the list of pre-written functions of energia make sure to analyze the code and understand it. Still I am going to explain it really simple and fast:
- First we set up the RED_LED (pin 2) as an OUTPUT on the void setup, which makes sense because a light is an output.
- On void loop, we send a HIGH voltage to the RED_LED (we turn it on), and then we keep it with HIGH voltage for 1000 ms.
- Right after that, we send LOW voltage to the RED_LED (we turn it off), and then we keep it with LOW voltage for 1000 ms.
- So that's the end of the code, but remember that void loop is always going to be repeat it again and again, so the RED_LED is going to be blinking until the end of time or until we upload a new code to the MSP 430.
Challenge:
Take your time to modify the code to make the MSP 430 do this:
Try it by yourself, in case you can't figure it put I am going to leave the codes below.
- Toggle the GREEN_LED insted of the RED_LED.
- Make the GREEN_LED and the RED_LED blink together.
- Make the GREEN_LED and the RED_LED blink alternatively.
Try it by yourself, in case you can't figure it put I am going to leave the codes below.