Friday 12 January 2018

Timers and interrupts - basic using timerOne library

Timers

Many arduino functions use timers.
Mostly, they are hidden. 
The Uno, running the ATmega328 has 3 timers: Timer 0,1,2.
 
The Arduino Uno also has a central system clock that runs at 16MHz.
It's a square wave produced by this crystal on the left.
This clock can be directly or indirectly connected to the timers.
16Mhz is the max speed that the timers can increment their counters.
However we can control the speed of the timers using what is called a prescaler. 
The prescaler divides the clock

Each of these timers, count up till they reach a max counter value.
They then tick back to zero. This is called overflow.
 
Timer0: 
TCNT0 (Timer Counter 0)
Timer0 is a 8bit timer. 
Thus it can store a maximum counter value of 255.
It's used in functions like Delay(), Millis(), Micros() .
analogWrite() pins 5,6
Timer0 is already set up to generate a millisecond interrupt 
to update the millisecond counter reported by millis().
 
 Timer1:  
TCNT1 (Timer Counter 1)
Timer1 is a 16bit timer.
Thus it can store a maximum counter value of 65535. 
It's used by the servo library 
analogWrite() pins 9,10
 
Timer2:  
TCNT0 (Timer Counter 0)
Timer2 is a 8bit timer
This counts from zero to 255.
It's used by the tone() function.
analogWrite() pins 3,11
 

 Interrupts

An interrupt is a mechanism for performing an immediate action , irrespective of whatever else
the program is doing.
You can think of it as something extra that runs in the background while your Arduino is 
executing the main body of code. It will do this extra thing at certain pre-determined times.

There are 2 types of interrupts.
1. Hardware
2. Software

Hardware interrupts happen, based on something happening on a pin.
(Eg a certain pin goes high or low)
Software interrupts happen at certain times.
This can be pretty useful as the interrupt allows you to pause the events taking place in the loop() at precisely timed intervals.This allows the program to execute a separate set of commands. 
Once these commands are done the Arduino picks up again where it was in the loop().
It's important to not make your interrupts complicated, otherwise it may pause
the rest of your program for too long.
 
Interrupts are meant to be short and sweet.
 
Interrupts can generally be enabled / disabled with the function interrupts() / noInterrupts()
By default in the Arduino firmware interrupts are enabled. 
 
 
THis circuit uses just two 220 ohm resistors, two LEDs.
The anode of the LEDs connect to pins 9 & 10 (via the resistors). 
Cathode to gnd.
 
For this first bit of code , we are using the TimerOne library. 
This is a 3rd party library. It's the easy way to write your own timer interrupt service routines.
Timer1 gives finer PWM control and/or running an periodic interrupt function
Author: Jesse Tane, Jérôme Despatis, Michael Polli, Dan Clemens, Paul Stoffregen.
 
The code:

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
/*
 This program mixes the delay function with timer interrupts
 Uses two digital pins & two LEDs
 */

#include <TimerOne.h>
String LEDStatus="OFF";

// using pins 10 & 9
// the red led is using delay
// Green LED uses interrupt
int GreenLED=10;

int RedLED=9;
int redDelay=1000;//millisecs
 
void setup()
{
 
  pinMode(GreenLED, OUTPUT);    
  pinMode(RedLED,OUTPUT);

 // Timer setup code is done inside the setup(){} function in an Arduino sketch.
// initialize the interrupt, specifying what time frame you want it to “interrupt” on,  
// and then what you want it to do when the interrupt alarm goes off.

  Timer1.initialize(200000); //0.2 secs ... millimicrosecs
  Timer1.attachInterrupt( FlashGreenLED );
 Serial.begin(9600);
}
 
void loop()
{
digitalWrite(RedLED, HIGH);
delay(redDelay);
digitalWrite(RedLED, LOW);
delay(redDelay);
}
 
void FlashGreenLED() // void = function
{
if (LEDStatus=="ON"){
  digitalWrite(GreenLED,LOW);
  LEDStatus="OFF";
  return;
  }
if (LEDStatus=="OFF"){
  digitalWrite(GreenLED,HIGH);
  LEDStatus="ON";
  return;
}
}
 
// &&&&&&&&&&&&&&&&&&&&&&&&&

-----------------------------------------------------------------------------

Links
+ https://www.instructables.com/Arduino-Timer-Interrupts/
+ https://toptechboy.com/arduino-lesson-28-tutorial-for-programming-software-interrupts/

This is a good link for the TimerOne library

No comments:

Post a Comment