Skip to content

Instantly share code, notes, and snippets.

@mburumaxwell
Last active February 5, 2017 18:15
Show Gist options
  • Save mburumaxwell/9613843 to your computer and use it in GitHub Desktop.
Save mburumaxwell/9613843 to your computer and use it in GitHub Desktop.
PCF8563 with NXP Cortex-M3, LPC1343 Keil
#include <LPC13xx.h>
#include <stdbool.h>
#include "i2c.h"
#include <time.h>
#include "pcf8563.h"
tm_t timestamp;
int main(void) {
SystemCoreClockUpdate(); /* Initialize CPU and CMSIS */
I2CInit(I2CMASTER); /* initialize the I2C for sensor modules */
pcf8563Init(); /* initialize RTC provider, PCF8563 */
pcf8563Get(&timestamp); /* read time */
while (1); /* endless loop */
}
#include <LPC13xx.h>
#include <stdbool.h>
#include <time.h>
#include "i2c.h"
#include "pcf8563.h"
extern volatile uint8_t I2CMasterBuffer[I2C_BUFSIZE];
extern volatile uint8_t I2CSlaveBuffer[I2C_BUFSIZE];
extern volatile uint32_t I2CReadLength, I2CWriteLength;
static bool _pcf8563Initialised = false;
int bcd_to_byte(uint8_t bcd) { return (((bcd >> 4) * 10) + (bcd & 0x0F)); }
uint8_t int_to_bcd(int in) { return ((in / 10) << 4) + (in % 10); }
void pcf8563Init(void) {
// assuming you have initialized I2C before
// Set initialisation flag
_pcf8563Initialised = true;
}
void pcf8563Set(tm_t *timestamp) {
if (!_pcf8563Initialised) pcf8563Init();
//stop counting
I2CWriteLength = 3;
I2CReadLength = 0;
I2CMasterBuffer[0] = PCF8563_ADDR_WRITE;
I2CMasterBuffer[1] = 0x00;//control register
I2CMasterBuffer[2] = (1 << 5);//set stop bit
I2CEngine();
//set date
I2CWriteLength = 9;
I2CReadLength = 0;
I2CMasterBuffer[0] = PCF8563_ADDR_WRITE;
I2CMasterBuffer[1] = 0x02;// Start from address 2(VL_seconds)
I2CMasterBuffer[2] = int_to_bcd(timestamp->tm_sec);//register address 2
I2CMasterBuffer[3] = int_to_bcd(timestamp->tm_min);//register address 3
I2CMasterBuffer[4] = int_to_bcd(timestamp->tm_hour);//register address 4
I2CMasterBuffer[5] = int_to_bcd(timestamp->tm_mday);//register address 5
I2CMasterBuffer[6] = int_to_bcd(timestamp->tm_wday);//register address 6
I2CMasterBuffer[7] = int_to_bcd(timestamp->tm_mon);//register address 7
I2CMasterBuffer[8] = int_to_bcd(timestamp->tm_year);//register address 8
I2CEngine();
//continue counting
I2CWriteLength = 3;
I2CReadLength = 0;
I2CMasterBuffer[0] = PCF8563_ADDR_WRITE;
I2CMasterBuffer[1] = 0x00;//control register
I2CMasterBuffer[2] = 0;//release stop bit
I2CEngine();
}
void pcf8563Get(tm_t *timestamp) {
if (!_pcf8563Initialised) pcf8563Init();
I2CWriteLength = 2;
I2CReadLength = 0;
I2CMasterBuffer[0] = PCF8563_ADDR_WRITE;
I2CMasterBuffer[1] = 2;// Start from address 2(VL_seconds)
I2CEngine();
I2CWriteLength = 1;
I2CReadLength = 7;
I2CMasterBuffer[0] = PCF8563_ADDR_READ;
I2CEngine();
timestamp->tm_sec = bcd_to_byte(I2CSlaveBuffer[0] & 0x7F);//register address 2
timestamp->tm_min = bcd_to_byte(I2CSlaveBuffer[1] & 0x7F);//register address 3
timestamp->tm_hour = bcd_to_byte(I2CSlaveBuffer[2] & 0x3F);//register address 4
timestamp->tm_mday = bcd_to_byte(I2CSlaveBuffer[3] & 0x3F);//register address 5
timestamp->tm_wday = bcd_to_byte(I2CSlaveBuffer[4] & 0x07);//register address 6
timestamp->tm_mon = bcd_to_byte(I2CSlaveBuffer[5] & 0x1F);//register address 7
timestamp->tm_year = bcd_to_byte(I2CSlaveBuffer[6]);//register address 8
}
#ifndef PCF8563_H_
#define PCF8563_H_
#ifdef __cplusplus
extern "C" {
#endif
typedef struct tm tm_t;
#define PCF8563_ADDR_READ 0xA3
#define PCF8563_ADDR_WRITE 0xA2
extern void pcf8563Init(void);
extern void pcf8563Set(tm_t *timestamp);
extern void pcf8563Get(tm_t *timestamp);
#ifdef __cplusplus
}
#endif
#endif /* PCF8563_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment