//DS3231 Time read/set and Alarm //Libraries #include //1 //I2C Address of the Module #define DS3231RTC_I2C_ADDRESS 0x68 //2 //Day of the week #define MONDAY 1 //3 #define TUESDAY 2 #define WEGNESDAY 3 #define THURSDAY 4 #define FRIDAY 5 #define SATURDAY 6 #define SUNDAY 7 //Alarm 1 Settings #define ALARM_ONCE_PER_SECOND 0 //4 #define ALARM_SECONDS_MATCH 1 #define ALARM_SECONDS_MINUTES_MATCH 2 #define ALARM_SECONDS_MINUTES_HOURS_MATCH 3 #define ALARM_SECONDS_MINUTES_HOURS_DATE_MATCH 4 #define ALARM_SECONDS_MINUTES_HOURS_DAY_MATCH 5 //Alarm 1 Mask Bits Array byte Alarm1MaskBits [6]={B01111000, //5 B01110000, B01100000, B01000000, B00000000, B10000000}; //Arduino UNO Interrupt pin and LED Pin const uint8_t IntPin = 2; //6 const uint8_t LEDPin = 4; bool ledstatus = 0; //Interrupt Service Routine Variable volatile byte Count = 0; //7 void setup(){ Serial.begin(9600); Wire.begin(); //8 pinMode(IntPin, INPUT_PULLUP); //9 pinMode(LEDPin, OUTPUT); //10 //Set time: Seconds, Minutes, Hours, Day, Date, Month, Year setRTCTime(0,15,10,FRIDAY,24,7,20); //11 //Set alarm 1: Seconds, Minutes, Hours, Day/Date, Setting setRTCAlarm1(0,30,12,12, ALARM_ONCE_PER_SECOND); //12 attachInterrupt(digitalPinToInterrupt(IntPin), ISRLED, FALLING); //13 } void loop(){ clearAlarm1(); //14 if(Count==1){ //15 Count = 0; ledstatus = !ledstatus; digitalWrite(LEDPin, ledstatus); } } //Convert from decimal to binary coded decimal byte decToBCD(byte val){ //16 return (((val/10)<<4)+val%10); } //Convert from binary coded decimal to decimal byte bcdToDec(byte val){ //17 return (10*(val>>4) + val%16); } void ISRLED(){ //18 Count++; } //Set the RTC Time Registers //19 void setRTCTime(byte Second, byte Minute, byte Hour, byte Day, byte Date, byte Month, byte Year){ Wire.beginTransmission(DS3231RTC_I2C_ADDRESS);//20 Wire.write(0x00); //21 Wire.write(decToBCD(Second)); //22 Wire.write(decToBCD(Minute)); Wire.write(decToBCD(Hour)); Wire.write(decToBCD(Day)); Wire.write(decToBCD(Date)); Wire.write(decToBCD(Month)); Wire.write(decToBCD(Year)); Wire.endTransmission(); //23 } //Set Alarm 1 //24 void setRTCAlarm1(byte Second, byte Minute, byte Hour, byte DayDate, byte Setting){ Second = decToBCD(Second) + bitRead(Alarm1MaskBits[Setting],3)*128; //25 Minute = decToBCD(Minute) + bitRead(Alarm1MaskBits[Setting],4)*128; Hour = decToBCD(Hour) + bitRead(Alarm1MaskBits[Setting],5)*128; DayDate = decToBCD(DayDate) + bitRead(Alarm1MaskBits[Setting],6)*128 + bitRead(Alarm1MaskBits[Setting],7)*64; Wire.beginTransmission(DS3231RTC_I2C_ADDRESS); Wire.write(0x07); //26 Wire.write(Second); Wire.write(Minute); Wire.write(Hour); Wire.write(DayDate); Wire.endTransmission(); Wire.beginTransmission(DS3231RTC_I2C_ADDRESS); Wire.write(0x0E); //27 Wire.write(B00011101); //28 Wire.endTransmission(); } //Clear Alarm 1 void clearAlarm1(){ //29 Wire.beginTransmission(DS3231RTC_I2C_ADDRESS); Wire.write(0x0F); Wire.write(B00000000); Wire.endTransmission(); } //Read the RTC Time Registers //30 void readRTCTime(byte* Second, byte* Minute, byte* Hour, byte* Day, byte* Date, byte* Month, byte* Year){ Wire.beginTransmission(DS3231RTC_I2C_ADDRESS); Wire.write(0x00); //31 Wire.endTransmission(); Wire.requestFrom(DS3231RTC_I2C_ADDRESS, 7); //32 *Second = bcdToDec(Wire.read()); //33 *Minute = bcdToDec(Wire.read()); *Hour = bcdToDec(Wire.read()); *Day = bcdToDec(Wire.read()); *Date = bcdToDec(Wire.read()); *Month = bcdToDec(Wire.read()); *Year = bcdToDec(Wire.read()); } //Display the Time over the Serial Monitor void displayTimeSerial(){ //34 byte Second, Minute, Hour, Day, Date, Month, Year; readRTCTime(&Second, &Minute, &Hour, &Day, &Date, &Month, &Year); Serial.print(Hour); Serial.print(":"); if(Minute <10) Serial.print("0"); Serial.print(Minute); Serial.print(":"); if(Second <10) Serial.print("0"); Serial.print(Second); Serial.print(" "); Serial.print(Date); Serial.print("/"); Serial.print(Month); Serial.print("/"); Serial.print(Year); Serial.print(" Day of the week: "); switch(Day){ case 1: Serial.println("Monday"); break; case 2: Serial.println("Tuesday"); break; case 3: Serial.println("Wednesday"); break; case 4: Serial.println("Thursday"); break; case 5: Serial.println("Friday"); break; case 6: Serial.println("Saturday"); break; case 7: Serial.println("Sunday"); break; } }