Jump to content
43oh

Systick/timer interrupts for ADC sampling


Recommended Posts

Hi,

 

I am using MSP432 for analog data collection. until now im using analogRead(pin#) in the loop function.

 

I want to use systick/timer to create periodic interrupts and sample ADC periodically on energia.

 

What functions should I use to achieve this? I couldn't find much help online.

 

Any information or code help would be grateful.

 

Reg, J

 

 

 

Link to post
Share on other sites

Open up the example: File-Examples->10.Multitasking->TimerLibrary.

 

Then replace the Sketch on the first tab with the Sketch below. This Sketch sets up a timer to trigger every 500 ms. In the timer trigger function, it sets a flag that is used in the main loop to do an analogRead(). Do not perform analogRead() in the trigger function. This function is executed in the ISR context and should contain as minimal code as possible.

#include "Timer.h"

volatile uint8_t adc_flag = false;

Timer myTimer;

void timerFunction()
{
  adc_flag = true;
}

// Add setup code
void setup()
{

  Serial.begin(115200);

  Serial.print(Timer_getNumTimers(), DEC);
  Serial.println(" timers");

  Serial.print("myTimer.begin... ");
  myTimer.begin(timerFunction, 500);
  Serial.println("done");

  Serial.print("myTimer.start... ");
  myTimer.start();
  Serial.println("done");

  // Set the analog resolution to 14 bits
  analogReadResolution(14);
}

// Add loop code
void loop()
{
  if (adc_flag) {
    adc_flag = false;
    uint16_t adc_val = analogRead(A0);
    Serial.print("Analog value: ");
    Serial.println(adc_val);
  }
}

Thanks to @reivilo for putting this timer library together.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...