Time interrupt

Hello, I'm new with Arduino so I'm not an expert.

I have to write a program which includes the timing interrupt: given the initial time I want that every 27 ms it does something.

I wrote the following sketch:

#include<Wire.h>
#include<RTClib.h>
RTC_DS3231 rtc;

void setup(){
Serial.begin(9600);

if (!rtc.begin()){
    Serial.println("Error.");
    while(true);
  }
  if (rtc.lostPower()){
    rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  }
}

but then I don't know how to manage the interrupt. Someone can give me an example?

do you really need an interrupt or just code to does something every 27 msec?

void
loop (void)
{
    static unsigned long msecLst = 0;
           unsigned long msec    = millis();

#define Period  27
    if (msec - msecLst > Period)  {
        msecLst = msec;
        // do something
    }
}

I need an interrupt, I mean: I don't want in the loop that the Arduino checks every time if 27 ms are passed.

Homework?

gfvalvo:
Homework?

Master's thesis

Your inclusion of an RTC makes your question unclear. Is this interrupt supposed to come from the RTC or internally from the Arduino?

If the former, you can look in the RTC's datasheet. I know some can give you a 1-second signal that can be used as an interrupt. I'm doubtful about the availability of a 27ms period pulse.

So, you'll probably have to use one of the chip's internal timers (you didn't say, I'm assuming you're using an Arduino Uno). Try Timer 1. Read about it in Chapter 16 of the ATMega328P datasheet that you can download here.

Ok thank you gfvalvo, I'm actually using an Arduino DUE and I think I'll need an external RTC, I have to ask to the professor.

The chip on a DUE also has timers. Get thee to the datasheet, it will tell all.

Using an RTC is only required for absolute timing - relative to the ‘real’ time of day.
What you’re doing is creating a pulse relative to the previous pulse, no external reference to a global time reference is needed.

Setting up a hardware ‘timer’ (not a ‘clock,’), once, then using its output will be by far the tidiest way to approach this.

You need ‘tick - tick - tick...’,
NOT ‘08:03.37.200 - 08:03.37.227 - 08:03.37.254...’

Get the TimerOne library and it will do all the work for you. If you want to understand what it is doing, just look in the library about what registers, etc. it is setting.