1 | /*****************************************************************************************************************************************************************************
|
---|
2 | * This sketch demonstrate how to use alarm in interrupt mode.
|
---|
3 | This mode is more conveniently because you use processor for other tasks and when alarm match occurs interrupt routine is executed.
|
---|
4 | In this way, alarm flag checking is indipendent from main program flow.
|
---|
5 | ******************************************************************************************************************************************************************************/
|
---|
6 |
|
---|
7 |
|
---|
8 | #include <RTCInt.h>
|
---|
9 |
|
---|
10 | RTCInt rtc;
|
---|
11 |
|
---|
12 | void setup()
|
---|
13 | {
|
---|
14 | Serial.begin(9600); //serial communication initializing
|
---|
15 | pinMode(13,OUTPUT);
|
---|
16 | rtc.begin(TIME_H24); //RTC initializing with 24 hour representation mode
|
---|
17 | rtc.setTime(17,0,5,0); //setting time (hour minute and second)
|
---|
18 | rtc.setDate(13,8,15); //setting date
|
---|
19 | rtc.enableAlarm(SEC,ALARM_INTERRUPT,alarm_int); //enabling alarm in polled mode and match on second
|
---|
20 | rtc.local_time.hour=17;
|
---|
21 | rtc.local_time.minute=5;
|
---|
22 | rtc.local_time.second=10; //setting second to match
|
---|
23 | rtc.setAlarm(); //write second in alarm register
|
---|
24 | }
|
---|
25 |
|
---|
26 | void loop()
|
---|
27 | {
|
---|
28 | digitalWrite(13,HIGH); //main program code
|
---|
29 | delay(100);
|
---|
30 | digitalWrite(13,LOW);
|
---|
31 | delay(900);
|
---|
32 |
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | /*************** Interrupt routine for alarm ******************************/
|
---|
37 | void alarm_int(void)
|
---|
38 | {
|
---|
39 | Serial.println("Alarm match!");
|
---|
40 | for(int i=0; i < 10; i++)
|
---|
41 | {
|
---|
42 | digitalWrite(13,HIGH);
|
---|
43 | //delay(200);
|
---|
44 | 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
|
---|
45 | digitalWrite(13,LOW);
|
---|
46 | //delay(800);
|
---|
47 | for(int j=0; j < 2000000; j++) asm("NOP");
|
---|
48 | }
|
---|
49 | RTC->MODE2.INTFLAG.bit.ALARM0=1; //clearing alarm0 flag
|
---|
50 | }
|
---|