100% found this document useful (1 vote)
200 views

Energia

The document describes Energia, an open-source electronics prototyping platform based on the Texas Instruments MSP430 microcontroller. Energia uses the mspgcc compiler and is based on the Wiring and Arduino frameworks. It includes an integrated development environment based on Processing for writing Energia programs, which use structure, variables/constants, and functions. Key functions described include setup(), loop(), digital I/O, analog I/O, PWM, delay(), and serial communication.

Uploaded by

Daniel V Sam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
200 views

Energia

The document describes Energia, an open-source electronics prototyping platform based on the Texas Instruments MSP430 microcontroller. Energia uses the mspgcc compiler and is based on the Wiring and Arduino frameworks. It includes an integrated development environment based on Processing for writing Energia programs, which use structure, variables/constants, and functions. Key functions described include setup(), loop(), digital I/O, analog I/O, PWM, delay(), and serial communication.

Uploaded by

Daniel V Sam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

ENERGIA

Energia is an open-source electronics prototyping platform started by Robert Wessels in January of 2012 with the goal to bring the Wiring and Arduino framework to the Texas Instruments MSP430 based LaunchPad. Energia uses the mspgcc compiler by Peter Bigot and is based on the Wiring and Arduino framework. Energia includes an integrated development environment (IDE) that is based on Processing. The foundation of Energia and Arduino is the Wiring framework that is developed by Hernando Barragan. The framework is thoughtfully created with designers and artists in mind to encourage a community where both beginners and experts from around the world share ideas, knowledge and their collective experience. The Energia team adopts the philosophy of learning by doing and strives to make it easy to work directly with the hardware.

Energia programs can be divided in three main parts: structure, values (variables and constants), and functions Structure setup(),loop() C o ntro l Struc ture s : if , if...else, for, switch case, while, do... while, break, continue, return, goto

Variables/Co nstants HIGH | LOW, INPUT | OUTPUT, INPUT_PULLUP |INPUT_PULLDOWN, true | false, integer constants, floating point constants Functions Digita l I/O pinMode(), digitalWrite(), digitalRead() A na log I/ O analogReference(), analogRead(), analogWrite() PWM Tim e millis(), micros(), delay(), delayMicroseconds() Ma th min(), max(), abs(), constrain(), map(), pow(), sqrt() C om m unic ation Serial, Stream

setup()
The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start using libraries, etc. The setup function will only run once, after each powerup or reset of the LaunchPad board.

loop()
After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. Use it to actively control the LaunchPad board.

DIGITAL I/O
Digital input means reading a logic high/low from a pin and digital output means setting a pin a logic high/low. For MSP430 logic high is almost 3.3v and logic low is 0v.The following are the main functions used for digital i/o operations.

pinMode()
Configures the specified pin to behave either as an input or an output. See the description of digital pins for details. S y nta x pinMode(pin, mode) Pa ra m e ters pin: the number of the pin whose mode you wish to set mode: either INPUT or OUTPUT or INPUT_PULLUP R e turns None

digitalWrite()
Write a HIGH or a LOW value to a digital pin. If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 3V for HIGH, 0V (ground) for LOW. If the pin is configured as an INPUT, writing a HIGH value with digitalWrite() will enable an internal pullup resistor . Writing LOW will disable the pullup. The pullup resistor is enough to light an LED dimly, so if LEDs appear to work, but very dimly, this is a likely cause. The remedy is to set the pin to an output with the pinMode() function. S y nta x digitalWrite(pin, value) Pa ra m e ters pin: the pin number value: HIGH or LOW R e turns None

Example 1: //Program to light up a LED void setup() { pinMode(RED_LED, OUTPUT);

// initialize the digital pin as an output.

} void loop() { digitalWrite(RED_LED, HIGH); // set the LED on

delay()
Pauses the program for the amount of time (in milliseconds) specified as parameter. S y nta x delay(ms) Pa r a m e ters ms: the number of milliseconds to pause (unsigned long) R e tur ns nothing delay() comes under the category of time functions(not digital i/o). They are very useful in creating time delays which are necessary in various applications. Example 2: //program to blink an LED void setup() { pinMode(RED_LED, OUTPUT); // initialize the digital pin as an output. } void loop() { digitalWrite(RED_LED, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(RED_LED, LOW); // set the LED off delay(1000); // wait for a second }

digitalRead()
Reads the value from a specified digital pin, either HIGH or LOW. S y nta x digitalRead(pin) Pa ra m e ters pin: the number of the digital pin you want to read (int) R e turns HIGH or LOW Example 3: //Interfacing a switch

int buttonState = 0; // variable for reading the pushbutton status void setup() { pinMode(GREEN_LED, OUTPUT); // initialize the LED pin as an output pinMode(PUSH2, INPUT_PULLUP); // initialize the pushbutton pin as an input } void loop(){ buttonState = digitalRead(PUSH2); // read the state of the pushbutton value if (buttonState == HIGH) // check if the pushbutton is pressed { digitalWrite(GREEN_LED, HIGH); // turn LED on

} else { digitalWrite(GREEN_LED, LOW); // turn LED off } }

ANALOG I/O
Analog I/O functions can handle values other than logic high and low. These functions enable us to input/ouput any value between Vcc to 0.

Analog to Digital Conversion (ADC)


The analog read operations in microcontrollers are implemented using ADC.Actually, a microcontroller can work only on 0 & 1.The analog data is sampled, quantized and encoded so that the microcontroller can process it.This process of conversion of analog data to digital form is called analog to digital conversion.

analogRead()
Reads the value from the specified analog pin. It will map input voltages between 0 and ~3 volts (VCC) into integer values between 0 and 1023. This yields a resolution between readings of: 3 volts / 1024 units or, .0029 volts (2.9 mV) per unit. The input range and resolution can be changed using analogReference(). S y nta x analogRead(pin) Pa ra m e ters pin: the number of the analog input pin to read from. R e turns int (0 to 1023); Example 4: //program to demonstrate analog read int val;//variable to store the value coming from the analog input pin void setup(){ pinMode(2,OUTPUT); pinMode(14,OUTPUT); } void loop(){ val = analogRead(A0); if(val>512) { digitalWrite(2, HIGH);

//declare pin 2 and pin 14 as output

// read the value from the analog input pin

// turn the red_led on

digitalWrite(14, LOW); } else { digitalWrite(14, HIGH); digitalWrite(2, LOW); } }

// turn the red_led off

// turn the green_led on // turn the green_led off

PWM (pulse width modulation)


Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (~3 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width.The analog write operation is

implemented using pwm.analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.

analogWrite()
Writes an analog value (PWM wave) to a pin.

S y nta x analogWrite(pin, value) Pa ra m e ters pin: the pin to write to. value: the duty cycle: between 0 (always off) and 255 (always on). R e turns Nothing Example 5: //program to increase and decrease brigthness of a led void setup() {} void loop() { for (int b=255; b>= 0;b=b-10) { analogWrite(14, b); //write the value b to pin 14 delay(100); } for (int c=0; c<=255;c=c+10) { analogWrite(14, c) ; //write the value c to pin 14 delay(100); } }

Serial communication/UART
Serial communication is the process of sending data one bit at a time, sequentially, over a communication channel. This is in contrast to parallel communication, where several bits are sent as a whole, on a link with several parallel channels. The UART, or Universal Asynchronous Receiver / Transmitter, is a feature of microcontroller useful for communicating serial data (text, numbers, etc.) to your PC. The device changes incoming parallel information (within the microcontroller/PC) to serial data which can be sent on a communication line. The following are some of the important functions used for serial communication between MSP430 and PC.

begin()
Sets the data rate in bits per second (baud) for serial data transmission. S y nta x Serial.begin(speed) Pa ra m e ters speed: in bits per second (baud) - long R e turns

Nothing

print()
Prints data to the serial port as human-readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. S y nta x Serial.print(val) Serial.print(val, format) Pa ra m e ters val: the value to print - any data type format: specifies the number base (for integral data types) or number of decimal places (for floating point types) R e turns byte print() will return the number of bytes written, though reading that number is optional

println()
Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n'). This command takes the same forms as Serial.print(). Example 6: //program to read an analog input and print the result to the serial monitor void setup() { Serial.begin(9600); // initialize uart and set baud rate } void loop() { int sensorValue = analogRead(A0); //read the analog value on pin A0 Serial.println(sensorValue); // displays the value on the serial monitor }

read()
Reads incoming serial data S y nta x Serial.read() Pa r a m e ters None

R e tur ns The first byte of incoming serial data available (or -1 if no data is available) int

available()
Get the number of bytes (characters) available for reading from the serial port. This is data that's already arrived and stored in the serial receive buffer (which holds 128 bytes). S y nta x Serial.available() Pa ra m e ters none R e turns the number of bytes available to read

Example 7: //program to switch a led depending on data entered by the user via keyboard char a='0';//intialises the variable a void setup() { Serial.begin(9600); // initialize uart and set the baud rate pinMode(2,OUTPUT);//sets red_led as oupput } void loop() { if(Serial.available())//checks if data is available in serial read buffer { a=Serial.read();//reads serial data and stores in a if(a=='1') { digitalWrite(2,HIGH);//if a=1 red_led is turned on } else { digitalWrite(2,LOW);//if a!=1 red_led is turned off } } }

LCD interfacing

LCD (Liquid Crystal Display) is one of the most common display device in used. The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins: A register select (RS) pin that controls where in the LCD's memory you're writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD's controller looks for instructions on what to do next. A Read/Write (R/W) pin that selects reading mode or writing mode An Enable pin that enables writing to the registers 8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you're writing to a register when you write, or the values you're reading when you read. There's also a display constrast pin (Vo), power supply pins (+5V and Gnd) and LED Backlight (Bklt+ andBKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively. The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library simplifies this for you so you don't need to know the lowlevel instructions. The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins.

LiquidCrystal Library

This library allows an Arduino board to control LiquidCrystal displays (LCDs) based on the Hitachi HD44780 (or a compatible) chipset, which is found on most text-based LCDs. The library works with in either 4- or 8-bit mode (i.e. using 4 or 8 data lines in addition to the rs, enable, and, optionally, the rw control lines). F unc tions use d

LiquidCrystal()
Creates a variable of type LiquidCrystal. The display can be controlled using 4 or 8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those lines unconnected. S y nta x ( 4 data line s) LiquidCrystal(rs, enable, d4, d5, d6, d7) Pa ra m e ters rs: pin that is connected to the RS pin on the LCD enable: pin that is connected to the enable pin on the LCD d0 to d7: pins that are connected to the corresponding data pins on the LCD.

begin()
Specifies the dimensions (width and height) of the display. S y nta x lcd.begin(cols, rows) Pa ra m e ters lcd: a variable of type LiquidCrystal cols: the number of columns that the display has rows: the number of rows that the display has Here, we are using 16*2 lcd . so,cols=12,rows=2.

clear()
Clears the LCD screen and positions the cursor in the upper-left corner. S y nta x lcd.clear() Pa ra m e ters lcd: a variable of type LiquidCrystal

setCursor()
Position the LCD cursor; that is, set the location at which subsequent text written to the LCD will be displayed. S y nta x lcd.setCursor(col, row) Pa ra m e ters lcd: a variable of type LiquidCrystal

col: the column at which to position the cursor (with 0 being the first column) row: the row at which to position the cursor (with 0 being the first row)

print()
Prints text to the LCD. S y nta x lcd.print(data) Pa ra m e ters lcd: a variable of type LiquidCrystal data: the data to print (char, byte, int, long, or string) R e turns byte print() will return the number of bytes written, though reading that number is optional Example 8: /* This program displays "IEEE - PreTathva Workshop" on the LCD. The circuit Connection: ============================================== LCD pin Connect to pin of MSP430 ---------------------------------------------01 - GND GND, pot 02 - VCC +5V, pot 03 - Contrast Pot wiper 04 - RS Pin8 (P2.0) 05 - R/W GND 06 - EN Pin9 (P2.1) 07 - DB0 GND 08 - DB1 GND 09 - DB2 GND 10 - DB3 GND 11 - DB4 Pin10 (P2.2) 12 - DB5 Pin11 (P2.3) 13 - DB6 Pin12 (P2.4) 14 - DB7 Pin13 (P2.5) 15 - BL+ +5V 16 - BLGND

============================================== */ #include <LiquidCrystal.h> // include the library code // initialize the library with the numbers of the interface pins LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); void setup() { lcd.begin(16, 2); // set up the LCD's number of columns and rows lcd.print("IEEE - PreTathva"); // Print a message to the LCD lcd.setCursor(0, 1); // set the cursor to column 0, line 1 // note: line 1 is the second row, since counting begins with 0 lcd.print("Workshop"); // Print a message to the LCD on line 1. } void loop() { } Example 9: //program to print data entered through PC Keyboard on LCD #include <LiquidCrystal.h> // include the library code

// initialize the library with the numbers of the interface pins LiquidCrystal lcd(P2_0, P2_1, P2_2, P2_3, P2_4, P2_5); void setup() { lcd.begin(16, 2); // set up the LCD's number of columns and rows lcd.print("IEEE - Workshop"); // Print a message to the LCD. delay(100); Serial.begin(9600); // initialize serial communication } void loop() { // if characters have arrived from the PC if (Serial.available()) { delay(100); // wait a bit for the entire message to arrive lcd.clear(); // clear the screen lcd.print("IEEE - Workshop"); lcd.setCursor(0,1); lcd.print(">"); // read all the available characters

while (Serial.available() > 0) { lcd.write(Serial.read()); } } }

// display each character to the LCD

You might also like