Pages: [1]   Go Down
Author Topic: Nano timers and interrupt usage  (Read 429 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I am new to Arduino though I'd worked with PIC before.  I got the MsTimer2 working on the Mega but on the Nano, it doesn't work.  Does Nano have timer2.  As the matter of fact, where do I find out more about the resources of certain device and how to use them?  Supposed in Arduino, lots of features are written up as libs, but I've also seen people writing timers and interrupt using direct keywords just like in MCU???  Please enlighten me in this new subject.
Thanks,
The simple sketch I tried:

#include <MsTimer2.h>
boolean output = HIGH;

void flash() {
  digitalWrite(13, output);     // pin13 has an LED build on board
  output = !output;
}

void setup() {
  pinMode(13, OUTPUT); 
  MsTimer2::set(500, flash); // 500ms period
  MsTimer2::start();
}

void loop() {}
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5145
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Nano has a Timer2 and the new version of the library should support it too.

Your code is missing a "volatile":

Code:
volatile boolean output = HIGH;

That's because you're changing the value in an interrupt routine and without the "volatile" the compiler may optimize the variable away because it's not used in active code. The "volatile" is a hint to the compiler that the variable is used in a way not transparent to the compiler.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27113
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As the matter of fact, where do I find out more about the resources of certain device and how to use them?
Best source for resources is the Atmel datasheet on the part.
Often times there are assembler and C code examples too.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: