aboutsummaryrefslogtreecommitdiffstats
path: root/ds3231.c
diff options
context:
space:
mode:
Diffstat (limited to 'ds3231.c')
-rwxr-xr-xds3231.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/ds3231.c b/ds3231.c
new file mode 100755
index 0000000..53a944f
--- /dev/null
+++ b/ds3231.c
@@ -0,0 +1,137 @@
+/*
+ * File: Header file to use DS3231 RTC module with PIC16F877A
+ * Author: B.Aswinth Raj
+ * Created on 5 May, 2018, 10:06 PM
+ * Connect the RTC module to SDA and SCK pins of the PIC.
+ * This header needs the PIC16f877a_I2C.h header to compile
+ */
+
+
+//PIN 18 -> RC3 -> SCL
+//PIN 23 -> RC4 ->SDA
+
+/****** Functions for RTC module *******/
+
+#include "ds3231.h"
+#include "i2c.h"
+#include "main.h"
+
+int BCD_2_DEC(int to_convert) {
+ return (to_convert >> 4) * 10 + (to_convert & 0x0F);
+}
+
+int DEC_2_BCD(int to_convert) {
+ return ((to_convert / 10) << 4) + (to_convert % 10);
+}
+
+void Read_Alarms_Temp() {
+ I2C_Master_Start(); // Start I2C
+ I2C_Master_Write(0xD0); // RTC chip address
+ I2C_Master_Write(0x07); // (alarm1 seconds register)
+ I2C_Master_Repeated_Start(); // Restart I2C
+
+ //READ
+ I2C_Master_Write(0xD1); // Initialize data read
+ alarm1_sec = BCD_2_DEC(I2C_Master_Read(1)); // Read alarm1 seconds 07h
+ alarm1_min = BCD_2_DEC(I2C_Master_Read(1)); // Read alarm1 minutes 08h
+ alarm1_hour = BCD_2_DEC(I2C_Master_Read(1)); // Read alarm1 hours 09h
+ I2C_Master_Read(1); // skip alarm1 day/date 0Ah
+ alarm2_min = BCD_2_DEC(I2C_Master_Read(1)); // Read alarm2 minutes 0Bh
+ alarm2_hour = BCD_2_DEC(I2C_Master_Read(1)); // Read alarm2 hours 0Ch
+ I2C_Master_Read(1); // Skip alarm2 day/date 0Dh
+ control_reg = I2C_Master_Read(1); // Read control register 0Eh
+ status_reg = I2C_Master_Read(1); // Read status register 0Fh
+ I2C_Master_Read(1); // Skip aging offset register
+
+ temperature_msb = I2C_Master_Read(1); // Read temperature MSB 011h
+
+ temperature_lsb = I2C_Master_Read(0); // Read temperature LSB 012h
+ I2C_Master_Stop();
+}
+
+void Set_Date() {
+ I2C_Master_Start(); // Start I2C
+ I2C_Master_Write(0xD0); // RTC Chip address
+ I2C_Master_Write(4); // send register address
+ I2C_Master_Write(DEC_2_BCD(date)); //update date
+ I2C_Master_Write(DEC_2_BCD(month)); //update month
+ I2C_Master_Write(DEC_2_BCD(year)); //update year
+ I2C_Master_Stop();
+}
+
+void Set_DayOfWeek() {
+ I2C_Master_Start(); // Start I2C
+ I2C_Master_Write(0xD0); // RTC Chip address
+ I2C_Master_Write(3); // send register address
+ I2C_Master_Write(dayofweek); //update day of week
+ I2C_Master_Stop();
+}
+
+void Set_Time() {
+ I2C_Master_Start(); // Start I2C
+ I2C_Master_Write(0xD0); // RTC Chip address
+ I2C_Master_Write(0); // send register address
+ I2C_Master_Write(DEC_2_BCD(sec)); //update sec
+ I2C_Master_Write(DEC_2_BCD(min)); //update min
+ I2C_Master_Write(DEC_2_BCD(hour)); //update hour
+ I2C_Master_Stop();
+}
+
+void Set_Time_Date() {
+ I2C_Master_Start(); // Start I2C
+ I2C_Master_Write(0xD0); // RTC Chip address
+ I2C_Master_Write(0); // send register address
+ I2C_Master_Write(DEC_2_BCD(sec)); //update sec
+ I2C_Master_Write(DEC_2_BCD(min)); //update min
+ I2C_Master_Write(DEC_2_BCD(hour)); //update hour
+ I2C_Master_Write(1); //ignore updating day
+ I2C_Master_Write(DEC_2_BCD(date)); //update date
+ I2C_Master_Write(DEC_2_BCD(month)); //update month
+ I2C_Master_Write(DEC_2_BCD(year)); //update year
+ I2C_Master_Stop();
+}
+
+void Update_Current_Date_Time() {
+ //START to Read
+ I2C_Master_Start();
+ I2C_Master_Write(0xD0); // RTC Chip address
+ I2C_Master_Write(0);
+ I2C_Master_Repeated_Start(); // Restart I2C
+
+ //READ
+ I2C_Master_Write(0xD1); // Initialize data read
+ sec = BCD_2_DEC(I2C_Master_Read(1)); // Read sec from register 00h
+ min = BCD_2_DEC(I2C_Master_Read(1)); // Read min from register 01h
+ hour = BCD_2_DEC(I2C_Master_Read(1)); // Read hour from register 02h
+ weekday = I2C_Master_Read(1); // Read day from register 03h
+ date = BCD_2_DEC(I2C_Master_Read(1)); // Read date from register 04h
+ month = BCD_2_DEC(I2C_Master_Read(1)); // Read month from register 05h
+ year = BCD_2_DEC(I2C_Master_Read(1)); // Read year from register 06h
+ I2C_Master_Stop();
+
+ //END Reading
+
+ I2C_Master_Start();
+ I2C_Master_Write(0xD1);
+ I2C_Master_Read(1);
+ I2C_Master_Stop();
+}
+
+void Write_Alarms() {
+ I2C_Master_Start(); // Start I2C
+ I2C_Master_Write(0xD0); // RTC Chip Address
+ I2C_Master_Write(7); // Send Alarm1 register address
+ I2C_Master_Write(0); // Write 0 to Alarm1 seconds
+ I2C_Master_Write(DEC_2_BCD(alarm1_min)); // Write Alarm1 minutes
+ I2C_Master_Write(DEC_2_BCD(alarm1_hour)); // Write Alarm1 hours
+ I2C_Master_Write(0x80); // Alarm1 when H:M:S matches
+ I2C_Master_Write(DEC_2_BCD(alarm2_min)); // Write Alarm2 minutes
+ I2C_Master_Write(DEC_2_BCD(alarm2_hour)); // Write Alarm2 hours
+ I2C_Master_Write(0x80); // Alarm2 when H:M:S matches
+
+ //Write data to control register (enable interupt when alarm);
+ I2C_Master_Write(4 | alarm1_status | (alarm2_status << 1));
+ I2C_Master_Write(0); // Clear alarm flag bits
+ I2C_Master_Stop(); // Stop I2C
+ __delay_ms(200); // Wait 200ms
+} \ No newline at end of file