From 4b968442110dd1a195a5120407ae6ea38d256c55 Mon Sep 17 00:00:00 2001 From: William Harrington Date: Wed, 7 Aug 2019 18:50:16 -0500 Subject: Add char array for day of week and use in Get_WeekDay function. Add a monthOfYear for month names for future use. --- ds3231.c | 36 ++++++++++++++++++++++++------------ ds3231.h | 5 +++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/ds3231.c b/ds3231.c index 38d1edd..b4e7ff9 100755 --- a/ds3231.c +++ b/ds3231.c @@ -1,12 +1,3 @@ -/* - * 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 @@ -16,6 +7,13 @@ #include "i2c.h" #include "main.h" +// Initialize character array for days of the week +char dayOfWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + +// Initialize character array for months of the year +char monthOfYear[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", + "Aug", "Sep", "Oct", "Nov", "Dec"}; + int BCD_2_DEC(int to_convert) { return (to_convert >> 4) * 10 + (to_convert & 0x0F); } @@ -24,8 +22,21 @@ int DEC_2_BCD(int to_convert) { return ((to_convert / 10) << 4) + (to_convert % 10); } -unsigned int Get_DayOfWeek(unsigned int y, unsigned int m, unsigned int d) { - return (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7 + 1; // 01 - 07, 01 = Sunday +unsigned int Get_DayOfWeek(unsigned int y, unsigned int m, unsigned int d) { + return (d += m < 3 ? y-- : y - 2, 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) % 7 + 1; // 01 - 07, 01 = Sunday +} + +unsigned int Get_Days_In_Month(unsigned int y, unsigned int m) { + return (m == 2) ? + (28 + Get_Is_Leap_Year(y)) : 31 - (m - 1) % 7 % 2; +} + +unsigned int Get_Is_Leap_Year(unsigned int y) { + return (year % 4) || ((year % 100 == 0) && (year % 400)) ? 0 : 1; +} + +char* Get_WeekDay(unsigned int d){ + return dayOfWeek[d - 1]; } void Read_Alarms_Temp() { @@ -94,9 +105,10 @@ void Update_Current_Date_Time() { I2C_Master_Start(); I2C_Master_Write(0xD0); // RTC Chip address I2C_Master_Write(0); - I2C_Master_Repeated_Start(); // Restart I2C + I2C_Master_Stop(); // Restart I2C //READ + I2C_Master_Start(); 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 diff --git a/ds3231.h b/ds3231.h index ba5bbbc..c361ff1 100755 --- a/ds3231.h +++ b/ds3231.h @@ -1,8 +1,13 @@ #include "conf.h" +char dayOfWeek[7][4]; + int BCD_2_DEC(int); int DEC_2_BCD(int); unsigned int Get_DayOfWeek(unsigned int, unsigned int, unsigned int); +unsigned int Get_Days_In_Month(unsigned int, unsigned int); +unsigned int Get_Is_Leap_Year(unsigned int); +char* Get_WeekDay(unsigned int); void Read_Alarms_Temp(void); void Set_Date(void); void Set_DayOfWeek(int); -- cgit v1.2.3-54-g00ecf