0

Feather M0 timer for timer interrupt
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Feather M0 timer for timer interrupt

by 7elEvan on Sat Aug 06, 2016 10:53 am

Hello,

I'm working with the Feather M0 Adalogger to record 5 kHz data from the LIS3DH accelerometer. As this is a little faster than the writing on the SD card is happy with, I am planning to save my data to a series of buffers in an interrupt while writing to the SD card outside of the interrupt. The first step of the process seems to be getting a timer interrupt going... and so far, I've been unsuccessful because I have not been able to call the timers properly. I've tried the TimerOne library, but get this error:

'class TimerOne' has no member named 'initialize'

The rest of the error states that it could be incompatible with SAMD architecture. I'm currently running the interrupt example from the TimerOne library. Any help with setting up/accessing a timer on this chip would be greatly appreciated!

Thanks!

Code: Select all | TOGGLE FULL SIZE
#include <TimerOne.h>

// This example uses the timer interrupt to blink an LED
// and also demonstrates how to share a variable between
// the interrupt and the main program.


const int led = 13;  // the pin with a LED

void setup(void)
{
  pinMode(led, OUTPUT);
  Timer1.initialize(150000);
  Timer1.attachInterrupt(blinkLED); // blinkLED to run every 0.15 seconds
  Serial.begin(9600);
}


// The interrupt will blink the LED, and keep
// track of how many times it has blinked.
int ledState = LOW;
volatile unsigned long blinkCount = 0; // use volatile for shared variables

void blinkLED(void)
{
  if (ledState == LOW) {
    ledState = HIGH;
    blinkCount = blinkCount + 1;  // increase when LED turns on
  } else {
    ledState = LOW;
  }
  digitalWrite(led, ledState);
}


// The main program will print the blink count
// to the Arduino Serial Monitor
void loop(void)
{
  unsigned long blinkCopy;  // holds a copy of the blinkCount

  // to read a variable which the interrupt code writes, we
  // must temporarily disable interrupts, to be sure it will
  // not change while we are reading.  To minimize the time
  // with interrupts off, just quickly make a copy, and then
  // use the copy while allowing the interrupt to keep working.
  noInterrupts();
  blinkCopy = blinkCount;
  interrupts();

  Serial.print("blinkCount = ");
  Serial.println(blinkCopy);
  delay(100);
}

7elEvan
 
Posts: 13
Joined: Mon Aug 01, 2016 11:53 am

Re: Feather M0 timer for timer interrupt

by franklin97355 on Sat Aug 06, 2016 4:59 pm

If you think it might not be compatible try compiling for a chip you know works with it (Uno maybe) and see what you get.

franklin97355
 
Posts: 17304
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Feather M0 timer for timer interrupt

by RH3 on Mon Aug 08, 2016 12:50 am

Try the following, which will flash the standard Pin 13 LED at 25Hz. I used this to simulate the pulses from an external fuel flow sensor before the hardware components were finished.

Just need to change the interrupt period to suit your application and the prescaler set for the CPU speed used (16MHz in this example).

Works on a UNO, MEGA and should run on Feather M0 without requiring external libraries

Code: Select all | TOGGLE FULL SIZE

#define ledPin 13                              // Used to show simulated pulse on red LED
int timer1_counter;                            // Used for simulated sensor pulses


void setup() {
 
 // initialize timer1
  noInterrupts();           // disable all interrupts
  TCCR1A = 0;
  TCCR1B = 0;
  timer1_counter = 63661;   // preload timer 65536-16MHz/256/ = 25Hz. Change this value for the required interrupt rate
  TCNT1 = timer1_counter;   // preload timer
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts();             // enable all interrupts 
}

void loop() {
  // put your main code here, to run repeatedly:

}

ISR(TIMER1_OVF_vect)        // interrupt service routine used only for simulated sensor pulses
{
  TCNT1 = timer1_counter;                         // preload timer
  digitalWrite(ledPin, digitalRead(ledPin) ^ 1);  // Flash LED at 25Hz
  pulseCount++;
}

RH3
 
Posts: 34
Joined: Sun Aug 03, 2014 4:22 am

Re: Feather M0 timer for timer interrupt

by 7elEvan on Mon Aug 08, 2016 6:44 pm

franklin97355: Thanks for the reply. I just used my Uno, and the code runs well. I do believe it is an issue with library compatibility.

RH3: Thank you for the reply as well. I tried your code on the M0, and it would not compile; looks like there is an issue with the ISR function. I get this error:

"expected constructor, destructor, or type conversion before '(' token"

Is there a trick to setting up an ISR with the M0? It compiles on the Uno just fine.

(Side note... RH3, how did you calculate the preload for the timer? I'm having trouble seeing where the 65536 comes in... thanks!)

7elEvan
 
Posts: 13
Joined: Mon Aug 01, 2016 11:53 am

Re: Feather M0 timer for timer interrupt

by hsusmann on Tue Oct 18, 2016 12:14 pm

I too would like to use timers on a Feather M0. I tried the last posting's code suggestion and it would not compile on the Feather M0. It did not know what TCCR1A, TCCR1B, TCNT1, CS12, etc... are. They must be defined somewhere else.

Does anyone know where these are defined?

Also, it was not happy with the ISR(TIMER1_OVF_vect) function at all.

Does anyone know how to define an interrupt service routine function for the Feather M0 in the Arduino IDE? (v. 1.6.13)

Perhaps there is some #include statement that I need to add??

Thanks in advance!

hsusmann
 
Posts: 20
Joined: Sat Nov 15, 2014 8:46 pm

Re: Feather M0 timer for timer interrupt

by sid1202 on Thu Oct 27, 2016 8:12 pm

I was looking into using a M0 feather and while researching differences I found this about timers on the Aduino Zero

https://forum.arduino.cc/index.php?topic=332275.15

Sid

sid1202
 
Posts: 369
Joined: Sun Jul 17, 2016 2:19 pm

Please be positive and constructive with your questions and comments.