A HAL-based library for interfacing Maxim Integrated's DS1307 Real-time Clock IC with STM32 via I2C.
- Set second, minute, hour, day date, month and year all in one command.
- Read decoded values of time.
- Set square-wave output settings.
- Copy the DS1307.h file into
[Your Project Folder] > Core > Inc
- Copy the DS1307.c file into
[Your Project Folder] > Core > Src
-
Line 16 of DS1307.h specifies the HAL library for the STM32 board in use. Change it as per your hardware.
#include "stm32f1xx_hal.h" //HAL library for the bluepill(STM32F103C8)
-
The next line specifies the handle of the I2C bus in use. Change it as per your schematic.
extern I2C_HandleTypeDef hi2c2; //handle for the i2c2 bus connected to my DS1307
Takes an 8-byte array and populates it with decimal values of [second,minute,hour,day,date,month,year, square-wave settings] read from the RTC
void readTime(uint8_t allTime[8])
Parameter | Type | Description |
---|---|---|
alltime |
uint8_t* |
Holds the data read. |
Takes DS1307 seconds register bits and returns the corresponding decimal value.
uint8_t getSeconds(uint8_t inTime)
Parameter | Type | Description |
---|---|---|
inTime |
uint8_t |
Bits from the DS1307 seconds register. |
Takes DS1307 hours register bits and returns the corresponding decimal value.
uint8_t getHours(uint8_t inTime)
Parameter | Type | Description |
---|---|---|
inTime |
uint8_t |
Bits from the DS1307 hours register. |
Determines whether the RTC is in 12 or 24-hour
char getMeridian()
- Returns
H
to indicate 24-hour mode andA
orP
indicating 12-hour mode AM or PM.
Takes DS1307 minute/day/date/month/year register values and returns a corresponding decimal value.
uint8_t getTime(uint8_t inTime)
Parameter | Type | Description |
---|---|---|
inTime |
uint8_t |
Bits from the minute or day or date or month or year register. |
Takes decimal values of [second,minute,hour,day,date,month,year, square-wave settings] and 12/24-hour clock setting and writes them to the RTC
setTime(uint8_t timeSet[8], char meridian)
Parameter | Type | Description |
---|---|---|
timeSet |
uint8_t* |
Holds the data to be written to the timekeeper registers. |
meridian |
char |
For the 12 or 24-hour setting. |
Setting the time
uint8_t pData[] = {0,1,1,4,17,11,21,0b00000011 }; // {sec,min,hour,day date, month,year, squarewave enable 32.768kHz}
setTime(pData, 'A'); // 01:01:00AM 4 17/11/2021
- The representation of day of the week is arbitrary but must be sequential...to wit, if 1 corresponds to Monday, 2 will be Tuesday
Reading the time
uint8_t rData[8]={0}; //holds timekeeper register values
uint8_t deBug[30]={0}; //holds string to be printed for dedugging
char meridian; // 12/24hr more
readTime(rData);
meridian = getMeridian();
sprintf((char *)deBug, " %i:%i:%i%c %i %i/%i/%i\r\n", rData[2], rData[1], rData[0], meridian,rData[3],rData[4],rData[5],rData[6]);
CDC_Transmit_FS(deBug, 25);