Advertisement
Guest User

DS3231 Alarm Interrupt

a guest
Jan 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   DS3231_set.pde
  3.   Eric Ayars
  4.   4/11
  5.  
  6.   Test of set-time routines for a DS3231 RTC
  7.  
  8. */
  9.  
  10. #include <Wire.h>
  11.  
  12.  
  13. void setup() {
  14.   // Start the serial port
  15.   Serial.begin(9600);
  16.  
  17.   // Start the I2C interface
  18.   Wire.begin();
  19.  
  20.   pinMode(3, INPUT_PULLUP);
  21.   attachInterrupt(digitalPinToInterrupt(3), writeOut, FALLING);
  22. }
  23.  
  24. void writeOut()
  25. {
  26.   Serial.println("Recieved");
  27. }
  28.  
  29. void loop() {
  30.   //First lets set the alarm 2
  31.   setAlarm();
  32. }
  33.  
  34. void setAlarm()
  35. {
  36.   // Set our alarm bits
  37.   Wire.beginTransmission(0x68); //begin session to RTC
  38.   Wire.write(0x0B); //Address we wish to write to
  39.   Wire.write(0B1000000); //A2M2 Enabled with 10 for minutes
  40.   Wire.write(0B1000000); //A2M3 Enabled with 11 set for hours (24 hr format)
  41.   Wire.write(0B1000000); //A2M4 Enabled with trig on Day 1
  42.   Wire.write(0B0000110); //Alarm 2 enabled, interupt enabled, alarm 1 disabled, oss diabled
  43.   Wire.write(0B0000000); //Disable Osc Stop bit, 32khz, A2 flag, A1 flag
  44.   Wire.endTransmission(); //End write Session
  45.  
  46.   Wire.beginTransmission(0x68); //begin write session for 0x0B
  47.   Wire.write(0x0B); //goto the bit we want
  48.   Wire.endTransmission(); //close our request
  49.   Wire.requestFrom(0x68, 5);
  50.  
  51.   for (int i = 0; i < 5; i++)
  52.   {
  53.     Serial.print("Line " + (String)i + " :");
  54.     Serial.print(Wire.read(), BIN);
  55.     Serial.print("\r\n");
  56.   }
  57.  
  58.  
  59.   Serial.println("--------------");
  60.  
  61.   while (true)
  62.   {
  63.     delay(1000);
  64.     Wire.beginTransmission(0x68); //begin write session for 0x0B
  65.     Wire.write(0x00); //goto the bit we want
  66.     Wire.endTransmission(); //close our request
  67.     Wire.requestFrom(0x68, 16);
  68.     for (int j = 0; j < 16; j++)
  69.     {
  70.       if (j == 0)
  71.       {
  72.         byte timeVal = Wire.read();
  73.         int ten = timeVal >> 4;
  74.         int one = timeVal & 0b00001111;
  75.         Serial.println("T" + (String)ten + (String)one);
  76.       }
  77.       else if (j == 14)
  78.       {
  79.         Serial.print(Wire.read(), BIN);
  80.         Serial.println();
  81.         Serial.print(Wire.read(), BIN);
  82.         Serial.println();
  83.       }
  84.       else
  85.       {
  86.         Wire.read();
  87.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement