From dd995683ea1f233b79724d524a9a3dbcdc171d30 Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Sun, 24 Jan 2010 11:13:23 +0000 Subject: Enhance TemperatureDatalogger project -- add RTC capabilities so that data is logged along with the current time and date. Make logging interval configurable, set by a C# PC host application. --- Projects/TemperatureDataLogger/Lib/DS1307.c | 129 ++++++++++++++++++++++ Projects/TemperatureDataLogger/Lib/DS1307.h | 111 +++++++++++++++++++ Projects/TemperatureDataLogger/Lib/FATFs/diskio.c | 14 ++- Projects/TemperatureDataLogger/Lib/FATFs/ffconf.h | 2 +- 4 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 Projects/TemperatureDataLogger/Lib/DS1307.c create mode 100644 Projects/TemperatureDataLogger/Lib/DS1307.h (limited to 'Projects/TemperatureDataLogger/Lib') diff --git a/Projects/TemperatureDataLogger/Lib/DS1307.c b/Projects/TemperatureDataLogger/Lib/DS1307.c new file mode 100644 index 000000000..37817e97f --- /dev/null +++ b/Projects/TemperatureDataLogger/Lib/DS1307.c @@ -0,0 +1,129 @@ +/* + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +#include "DS1307.h" + +void DS1307_Init(void) +{ + // Nothing to initialize +} + +void DS1307_SetDate(uint8_t Day, uint8_t Month, uint8_t Year) +{ +#if defined(DUMMY_RTC) + return; +#endif + + DS1307_DateRegs_t CurrentRTCDate; + CurrentRTCDate.Byte1.TenDay = (Day / 10); + CurrentRTCDate.Byte1.Day = (Day % 10); + CurrentRTCDate.Byte2.TenMonth = (Month / 10); + CurrentRTCDate.Byte2.Month = (Month % 10); + CurrentRTCDate.Byte3.TenYear = (Year / 10); + CurrentRTCDate.Byte3.Year = (Year % 10); + + if (TWI_StartTransmission(DS1307_ADDRESS_WRITE)) + { + TWI_SendByte(DS1307_DATEREG_START); + TWI_SendByte(CurrentRTCDate.Byte1.IntVal); + TWI_SendByte(CurrentRTCDate.Byte2.IntVal); + TWI_SendByte(CurrentRTCDate.Byte3.IntVal); + + TWI_StopTransmission(); + } +} + +void DS1307_SetTime(uint8_t Hour, uint8_t Minute, uint8_t Second) +{ +#if defined(DUMMY_RTC) + return; +#endif + + DS1307_TimeRegs_t CurrentRTCTime; + CurrentRTCTime.Byte1.TenSec = (Second / 10); + CurrentRTCTime.Byte1.Sec = (Second % 10); + CurrentRTCTime.Byte1.CH = false; + CurrentRTCTime.Byte2.TenMin = (Minute / 10); + CurrentRTCTime.Byte2.Min = (Minute % 10); + CurrentRTCTime.Byte3.TenHour = (Hour / 10); + CurrentRTCTime.Byte3.Hour = (Hour % 10); + CurrentRTCTime.Byte3.TwelveHourMode = false; + + if (TWI_StartTransmission(DS1307_ADDRESS_WRITE)) + { + TWI_SendByte(DS1307_TIMEREG_START); + TWI_SendByte(CurrentRTCTime.Byte1.IntVal); + TWI_SendByte(CurrentRTCTime.Byte2.IntVal); + TWI_SendByte(CurrentRTCTime.Byte3.IntVal); + + TWI_StopTransmission(); + } +} + +void DS1307_GetDate(uint8_t* Day, uint8_t* Month, uint8_t* Year) +{ +#if defined(DUMMY_RTC) + *Day = 1; + *Month = 1; + *Year = 1; + return; +#endif + + if (TWI_StartTransmission(DS1307_ADDRESS_WRITE)) + { + TWI_SendByte(DS1307_DATEREG_START); + + TWI_StopTransmission(); + } + + DS1307_DateRegs_t CurrentRTCDate; + + if (TWI_StartTransmission(DS1307_ADDRESS_READ)) + { + TWI_ReceiveByte(&CurrentRTCDate.Byte1.IntVal, false); + TWI_ReceiveByte(&CurrentRTCDate.Byte2.IntVal, false); + TWI_ReceiveByte(&CurrentRTCDate.Byte3.IntVal, true); + + TWI_StopTransmission(); + } + + *Day = (CurrentRTCDate.Byte1.TenDay * 10) + CurrentRTCDate.Byte1.Day; + *Month = (CurrentRTCDate.Byte2.TenMonth * 10) + CurrentRTCDate.Byte2.Month; + *Year = (CurrentRTCDate.Byte3.TenYear * 10) + CurrentRTCDate.Byte3.Year; +} + +void DS1307_GetTime(uint8_t* Hour, uint8_t* Minute, uint8_t* Second) +{ +#if defined(DUMMY_RTC) + *Hour = 1; + *Minute = 1; + *Second = 1; + return; +#endif + + if (TWI_StartTransmission(DS1307_ADDRESS_WRITE)) + { + TWI_SendByte(DS1307_TIMEREG_START); + + TWI_StopTransmission(); + } + + DS1307_TimeRegs_t CurrentRTCTime; + + if (TWI_StartTransmission(DS1307_ADDRESS_READ)) + { + TWI_ReceiveByte(&CurrentRTCTime.Byte1.IntVal, false); + TWI_ReceiveByte(&CurrentRTCTime.Byte2.IntVal, false); + TWI_ReceiveByte(&CurrentRTCTime.Byte3.IntVal, true); + + TWI_StopTransmission(); + } + + *Second = (CurrentRTCTime.Byte1.TenSec * 10) + CurrentRTCTime.Byte1.Sec; + *Minute = (CurrentRTCTime.Byte2.TenMin * 10) + CurrentRTCTime.Byte2.Min; + *Hour = (CurrentRTCTime.Byte3.TenHour * 10) + CurrentRTCTime.Byte3.Hour; +} diff --git a/Projects/TemperatureDataLogger/Lib/DS1307.h b/Projects/TemperatureDataLogger/Lib/DS1307.h new file mode 100644 index 000000000..491836079 --- /dev/null +++ b/Projects/TemperatureDataLogger/Lib/DS1307.h @@ -0,0 +1,111 @@ +/* + Copyright (C) Dean Camera, 2010. + + dean [at] fourwalledcubicle [dot] com + www.fourwalledcubicle.com +*/ + +#ifndef _DS1307_H_ +#define _DS1307_H_ + + /* Includes: */ + #include + + #include + + /* Type Defines: */ + typedef struct + { + union + { + struct + { + unsigned int Sec : 4; + unsigned int TenSec : 3; + unsigned int CH : 1; + }; + + uint8_t IntVal; + } Byte1; + + union + { + struct + { + unsigned int Min : 4; + unsigned int TenMin : 3; + unsigned int _RESERVED : 1; + }; + + uint8_t IntVal; + } Byte2; + + union + { + struct + { + unsigned int Hour : 4; + unsigned int TenHour : 2; + unsigned int TwelveHourMode : 1; + unsigned int _RESERVED : 1; + }; + + uint8_t IntVal; + } Byte3; + } DS1307_TimeRegs_t; + + typedef struct + { + union + { + struct + { + unsigned int Day : 4; + unsigned int TenDay : 2; + unsigned int _RESERVED : 2; + }; + + uint8_t IntVal; + } Byte1; + + union + { + struct + { + unsigned int Month : 4; + unsigned int TenMonth : 1; + unsigned int _RESERVED : 3; + }; + + uint8_t IntVal; + } Byte2; + + union + { + struct + { + unsigned int Year : 4; + unsigned int TenYear : 4; + }; + + uint8_t IntVal; + } Byte3; + } DS1307_DateRegs_t; + + /* Macros: */ + #define DS1307_TIMEREG_START 0x00 + #define DS1307_DATEREG_START 0x04 + + #define DS1307_ADDRESS_READ 0b11010001 + #define DS1307_ADDRESS_WRITE 0b11010000 + + /* Function Prototypes: */ + void DS1307_Init(void); + + void DS1307_SetDate(uint8_t Day, uint8_t Month, uint8_t Year); + void DS1307_SetTime(uint8_t Hour, uint8_t Minute, uint8_t Second); + + void DS1307_GetDate(uint8_t* Day, uint8_t* Month, uint8_t* Year); + void DS1307_GetTime(uint8_t* Hour, uint8_t* Minute, uint8_t* Second); + +#endif diff --git a/Projects/TemperatureDataLogger/Lib/FATFs/diskio.c b/Projects/TemperatureDataLogger/Lib/FATFs/diskio.c index e6be68734..0c0083bac 100644 --- a/Projects/TemperatureDataLogger/Lib/FATFs/diskio.c +++ b/Projects/TemperatureDataLogger/Lib/FATFs/diskio.c @@ -83,5 +83,17 @@ DRESULT disk_ioctl ( DWORD get_fattime (void) { - return (1UL << 25) | (1UL << 21) | (1UL << 16) | (1UL << 11) | (1UL << 5) | (1UL << 0); + uint8_t Day, Month, Year; + uint8_t Hour, Minute, Second; + + DS1307_GetDate(&Day, &Month, &Year); + DS1307_GetTime(&Hour, &Minute, &Second); + + + return ((DWORD)(20 + Year) << 25) | + ((DWORD)Month << 21) | + ((DWORD)Day << 16) | + ((DWORD)Hour << 11) | + ((DWORD)Minute << 5) | + (((DWORD)Second >> 1) << 0); } diff --git a/Projects/TemperatureDataLogger/Lib/FATFs/ffconf.h b/Projects/TemperatureDataLogger/Lib/FATFs/ffconf.h index 18510157d..9b414e979 100644 --- a/Projects/TemperatureDataLogger/Lib/FATFs/ffconf.h +++ b/Projects/TemperatureDataLogger/Lib/FATFs/ffconf.h @@ -36,7 +36,7 @@ / 3: f_lseek is removed in addition to level 2. */ -#define _USE_STRFUNC 1 /* 0, 1 or 2 */ +#define _USE_STRFUNC 0 /* 0, 1 or 2 */ /* To enable string functions, set _USE_STRFUNC to 1 or 2. */ -- cgit v1.2.3