Basic functions related to startup and GPIO in Energia

Let’s see some basic functions related to Startup and GPIO that are frequently used in Energia IDE.

 

Functions at startup

a)  setup()

  • This function is called at the beginning of the sketch.
  • It is used for initializing variables, pin modes, etc.
  • It runs only once after each power-up, or when the TI LaunchPad board resets.

 

b)  loop()

  • Once the setup function is completed, the loop function is executed over and over continuously.

 

On-Board LED Blinking Code using MSP430 Launchpad

/* Blinking on-board LED connected to pin 2 of MSP-EXP430G2 Launchpad board */

/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
    pinMode(2, OUTPUT); 	/* Pin 2 is defined as Output */
}

/* Loop runs over and over after the startup function */
void loop()
{
    digitalWrite(2, HIGH); 	/* Make pin 2 High, LED ON */
    delay(1000); 			/* Wait for 1 second */
    digitalWrite(2, LOW);	/* Make pin 2 Low, LED OFF*/
    delay(1000); 			/* Wait for 1 second */
}

 

Functions for using GPIO

a)  pinMode(pin, mode)

  • pin: The pin for which a specific mode is selected
  • mode: The mode for the pin specified in the function. It can be INPUT, INPUT PULLUP, INPUT PULLDOWN, or OUTPUT. 
  • This function is used to configure the pin specified to behave as Input (INPUT), Input with pull up resistor (INPUT_PULLUP),  Input with a pull-down resistor (INPUT_PULLDOWN), or Output (OUTPUT).
  • Example  
pinMode(3, INPUT); //configure digital pin 3 as an input pin.

 

b)  digitalRead(digital_pin)

  • digital_pin: The digital pin which is to be read. 
  • This function is used to read the digital signal from the specified digital pin (digital_pin).
  • The function returns either HIGH or LOW.
  • Example 
digitalRead(11); //reads value on pin 11. 

 

c)  digitalWrite(pin,value)

  • pin: The digital pin to which value is to be written.
  • value: Can be HIGH or LOW. 
  • This function is used to write a HIGH or a LOW value to a digital pin.
  • Example 
digitalWrite(12, HIGH); //makes the pin 12 HIGH. 

 

d)  analogRead(analog_pin)

  • analog_pin: The analog pin whose value is to be read.
  • This function is used to read the analog signal from the specified analog pin (analog_pin).
  • The function returns an integer value in the range of 0 to 1023.
  • Example 
analogRead(A3); //reads the value on analog pin A3. 

 

e)  analogWrite(pin,value)

  • pin: The analogl pin to which value is to be written. 
  • value: Can be any number between 0 to 255. 0 being 0% duty cycle and 255 being 100% duty cycle.
  • This function is used for generating PWM on PWM digital pins.
  • value can be any number between 0 to 255. 0 being 0% duty cycle and 255 being 100% duty cycle.
  • Example 
analogWrite(10, 128); //generates a PWM wave of 50% duty cycle on pin 10.

 

LED Brightness Control using potentiometer Code using MSP430 Launchpad

/* Blink on-board LED connected to pin 2 of MSP-EXP430G2 Launchpad board */
/* Read analog value on analog pin A4 of MSP-EXP430G2 Launchpad board */
/* Read digital value on pin 6 of MSP-EXP430G2 Launchpad board */
/* Vary intensity of external LED connected to pin 11 of MSP-EXP430G2 Launchpad board */


/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
    pinMode(2, OUTPUT); /* Pin 2 is defined as Output */
    pinMode(11, OUTPUT);  /* Pin 11 is defined as Output */
    pinMode(6, INPUT); /* Pin 6 is defined as Input */
}

/* Loop runs over and over after the startup function */
void loop()
{
    digitalWrite(2, HIGH); /* Make pin 2 High, LED ON */
    delay(1000); /* Wait for 1 second */
    digitalWrite(2, LOW); /* Make pin 2 Low, LED OFF*/
    delay(1000); /* Wait for 1 second */

    for(int i =0; i<256; i++)
    {
        analogWrite(11, i); /* Vary intensity of LED connected externally to pin 11 of MSP-EXP430G2 Launchpad board */
        /* Vary the intensity by applying PWM of duty cycle varying from 0 to 100% (writing value 0 to 255) */
        delay(300); /* Wait for 300 milliseconds */
    }

   int dig_val; 
   dig_val = digitalRead(6); /* Read digital signal on pin 8 */

    int adc_val;
    adc_val = analogRead(A4); /* Read analog signal present on pin A4 */
}

Components Used

TI Launchpad MSP-EXP430G2
TI Launchpad MSP-EXP430G2
1
Ad