Basic functions related to Time and Interrupts in Energia

MSP430 Time Functions

a)   millis()

  • This function returns the number of milliseconds passed since the Launchpad board started running the current program.
  • This number overflows (rolls back to zero) after approximately 50 days.
  • The value returned by millis is an unsigned long int.
  • Example
unsigned long time;
time = millis();

 

b)   micros()

  • This function returns the number of microseconds passed since the Launchpad board started running the current program.
  • This number overflows (rolls back to zero) after approximately 70 minutes.
  • The value returned by micros is an unsigned long int.
  • Example 
unsigned long time;
time = micros();

 

c)   delay(value)

  • value: the number of milliseconds to pause the program.
  • This function pauses the program for the number of milliseconds specified.

 

d)   delayMicroseconds(value)

  • value: the number of microseconds to pause the program.
  • This function pauses the program for the number of microseconds specified.

 

e)   sleep(value)

  • value: the number of milliseconds to send the microcontroller to sleep mode.
  • This function puts the microcontroller into sleep mode for the milliseconds specified. It is better than using delay(), since it saves power and clock cycles.

 

MSP430 Launchpad Timer Program

/* Time Functions */

unsigned long time_value;

/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup()
{
  pinMode(2, OUTPUT); 			/* Pin 2 is defined as Output */
  Serial.begin(9600); 			/* opens serial port, sets data rate to 9600 bps */
}

/* Loop runs over and over after the startup function */
void loop()
{
  digitalWrite(2, HIGH); 		/* Make pin 2 HIGH */
  delay(2000); 					/* Wait for 2 seconds */
  digitalWrite(2, LOW); 
  delayMicroseconds(100); 		/* Wait for 100 microseconds */
  Serial.print("Time passed since power on in milliseconds:"); 
  time_value = millis(); 		/* Read value of number of microseconds since program started executing */
  Serial.println(time_value);
  delay(2000);
  Serial.print("Time passed since power on in microseconds:");
  time_value = micros(); 		/* Read value of number of milliseconds since program started executing */
  Serial.println(time_value);  
  sleep(1000); 					/* Put the controller in sleep for 1 seconds */
}

 

 MSP430 Interrupt Functions

a)   interrupts()

  • This function re-enables interrupts after they have been disabled by noInterrupts()

 

b)   noInterrupts()

  • This function disables interrupts.
  • Interrupts can be re-enabled using interrupts().

 

c)   attachInterrupt(interrupt, ISR, mode)

  • interrupt: is the pin number on which the interrupt event is to be monitored.
  • ISR: The ISR to call when an interrupt event occurs. This function must take no parameters and return nothing.
  • mode: Decides which event should cause an interrupt. 4 options are available to select from.

i)   LOW (Not available on MSP430 and C2000)
     Interrupt generated whenever the pin is LOW.

ii)  CHANGE
     Interrupt generated whenever pin changes value.

iii) RISING
     Interrupt generated whenever pin changes from LOW to HIGH.

iv)  FALLING
     Interrupt generated whenever pin changes from HIGH to LOW.

 

d)   detachInterrupt(interrupt)

  • Turns off the interrupt specified by interrupt

 

MSP430 Interrupt Program

/* Interrupt Functions */


/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup() 
{
  
}

/* Loop runs over and over after the startup function */
void loop()
{
  noInterrupts();
  /* critical, time-sensitive code here */
  interrupts();
  /* other code here */
}

/* Interrupt Functions */


volatile bool led_state = LOW;

/* Setup is run once at the start (Power-On or Reset) of sketch */
void setup() 
{
  pinMode(2, OUTPUT); 			/* Pin 13 is defined as Output */
  pinMode(5, INPUT_PULLUP); 	/* Interrupt pin */
  attachInterrupt(5, blink, FALLING);
}

/* Loop runs over and over after the startup function */
void loop()
{
  digitalWrite(2, led_state); 	/* Write state value to pin 13 which has on-board LED */
}

/* ISR */
void blink() {
  led_state = !led_state; 		/* Toggle state value of LED */
}

Components Used

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