Loading...
 

Arduino M0 RTC Alarm Interrupt Example

RTC Alarm in interrupt mode Example

This example shows you how to configure an alarm in interrupt mode using the RTCint library.
RTCint library gives you opportunity to use the internal RTC of ATSAMD21G18A, the microcontroller mounted on M0 and M0 PRO boards.

Note:
You need the RTCint library to use this sketch. It is contained into the new IDE 1.7.7 and into Arduino Studio 0.0.4. Download it now!!!  Click here.

Hardware Required

  • Arduino M0/M0 Pro
  • 1 green led
  • 1 red led
  • 2 resistor of 220 Ohm
  • 1 Buzzer
  • wires
  • 1 breadboard

Circuit

Image  
Connect the anode of the red LED (usually the longer pin) in series to a resistence of 220 Ohm and to digital pin 12, then connect the cathode of led to GND of board.
Also connect, in the same way, the green led but taking care to connect the anode to pin 13.
Finally connect the positive pole of buzzer to pin 11 of board and the negative to the Ground.

Now you can upload the sketch using an USB cable.

Note:
In the picture is shown the circuit realized using an Arduino M0 Pro.

Code

This sketch turns on the green led. When the alarm occurs the green led goes off, the red led blinks , the buzzer plays and it prints on Serial monitor:"Alarm match!".
This example uses the methods of the RTCint library.
The alarm is enable with:"rtc.enableAlarm(SEC,ALARM_INTERRUPT,alarm_int)".
It is possible to choose the alarm match inserting these parameters into parentheses:
-OFF alarm disabled
-SEC alarm match on seconds
-MMSS alarm match on minutes and seconds
-HHMMSS alarm match on hours, minute and seconds
-DDHHMMSS alarm match on day, hours, minutes and seconds
-MMDDHHMMSS alarm match on month, day hours, minutes and seconds
-YYMMDDHHMMSS alarm match on year, month, day, hours, minutes and seconds.
Also it is possible to indicate alarm mode inserting into parentheses: "ALARM_POLLED,NULL" for alarm polled or "ALARM_INTERRUPT,my_function" for interrupt.
In this example is used the interrupt mode.
This mode is more conveniently because you use processor for other tasks and when alarm match occurs interrupt routine is executed.
In this way, alarm flag checking is indipendent from main program flow.
Note:
First uploading the sketch, set the date, the current time and the alarm time.

The complete code and its detailed description are shown down.

/***************************************************************************************************************************************************************************
* This sketch demonstrate how to use alarm in interrupt mode.
This mode is more conveniently because you use processor for other tasks and when alarm match occurs interrupt routine is executed.
In this way, alarm flag checking is indipendent from main program flow.
****************************************************************************************************************************************************************************/
//*RTC Alarm in interrupt mode*/
#include <RTCInt.h> //include RTCint library

RTCInt rtc;         //create an RTCInt type object
int buzzer=11;      //connect the buzzer to digital pin 11
int red=12;         //connect red led to digital pin 12
int green=13;       //connect green led to digital pin 13
/*setup*/
void setup() 
{
  SerialUSB.begin(9600);    //serial communication initializing
  pinMode(green,OUTPUT);    //define green as output
  pinMode(red,OUTPUT);      //define red as output
  pinMode(buzzer,OUTPUT);   //define buzzer as output
  digitalWrite(green,LOW);  //initialize the green to LOW level
  digitalWrite(red,LOW);    //initialize the green to LOW level
  rtc.begin(TIME_H24);      //RTC initializing with 24 hour representation mode
  rtc.setTime(17,0,5,0);    //setting time (hour minute and second)
  rtc.setDate(13,8,15);     //setting date
  rtc.enableAlarm(SEC,ALARM_INTERRUPT,alarm_int); //enabling alarm in interrupt mode 
  rtc.local_time.hour=17;   //setting hour alarm
  rtc.local_time.minute=5;  //setting minute alarm
  rtc.local_time.second=10; //setting second to match
  rtc.setAlarm();           //write second in alarm register
}
/*loop*/
void loop()
{  noTone(buzzer);           // disable the buzzer
   digitalWrite(green,HIGH); //turn on green led
   delay(500);               //wait 500 millisecond 
}

/*************** Interrupt routine for alarm ******************************/
void alarm_int(void)
{
  SerialUSB.println("Alarm match!");
    for(int i=0; i < 30; i++)
    { digitalWrite(green,LOW);       //turn off green led
      tone(buzzer,200);              //play buzzer
      digitalWrite(red,HIGH);        //turn on red green
      for(int j=0; j < 1000000; j++) asm("NOP");  //in interrupt routine you cannot use delay function then an alternative is NOP instruction cicled many time as you need
      digitalWrite(red,LOW);         //turn off red green italWrite(13,LOW);
      for(int j=0; j < 2000000; j++) asm("NOP");
    }
    RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag
}