[259] | 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 | NOTE: for M0/M0 pro only you can select the oscillator source for count.
|
---|
| 7 | If you want to use a low power oscillator use rtc.begin(TIME_H24, LOW_POWER); function.
|
---|
| 8 | If you want to use a more accurate oscillator use rtc.begin(TIME_H24, HIGH_PRECISION); function.
|
---|
| 9 | ******************************************************************************************************************************************************************************/
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | #include <RTCInt.h>
|
---|
| 13 |
|
---|
| 14 | RTCInt rtc;
|
---|
| 15 |
|
---|
| 16 | void setup()
|
---|
| 17 | {
|
---|
| 18 | Serial.begin(9600); //serial communication initializing
|
---|
| 19 | pinMode(13,OUTPUT);
|
---|
| 20 | rtc.begin(TIME_H24); //RTC initializing with 24 hour representation mode
|
---|
| 21 | rtc.setTime(17,0,5,0); //setting time (hour minute and second)
|
---|
| 22 | rtc.setDate(13,8,15); //setting date
|
---|
| 23 | rtc.enableAlarm(SEC,ALARM_INTERRUPT,alarm_int); //enabling alarm in polled mode and match on second
|
---|
| 24 | rtc.time.hour=17;
|
---|
| 25 | rtc.time.minute=5;
|
---|
| 26 | rtc.time.second=10; //setting second to match
|
---|
| 27 | rtc.setAlarm(); //write second in alarm register
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | void loop()
|
---|
| 31 | {
|
---|
| 32 | digitalWrite(13,HIGH); //main program code
|
---|
| 33 | delay(100);
|
---|
| 34 | digitalWrite(13,LOW);
|
---|
| 35 | delay(900);
|
---|
| 36 |
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | /*************** Interrupt routine for alarm ******************************/
|
---|
| 41 | void alarm_int(void)
|
---|
| 42 | {
|
---|
| 43 | Serial.println("Alarm match!");
|
---|
| 44 | for(int i=0; i < 10; i++)
|
---|
| 45 | {
|
---|
| 46 | digitalWrite(13,HIGH);
|
---|
| 47 | //delay(200);
|
---|
| 48 | 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
|
---|
| 49 | digitalWrite(13,LOW);
|
---|
| 50 | //delay(800);
|
---|
| 51 | for(int j=0; j < 2000000; j++) asm("NOP");
|
---|
| 52 | }
|
---|
| 53 | rtc.alarmClearFlag(); //clearing alarm flag
|
---|
| 54 | }
|
---|