The Puma
Full Member level 2
pcf8583 mikroc
How can i read the time and date from this chip.
Day month and year??
I have CCS-C
Did anyone have sample code to do this.
I have this code so far
How can i read the time and date from this chip.
Day month and year??
I have CCS-C
Did anyone have sample code to do this.
I have this code so far
Code:
#include <18F4620.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOLVP,DEBUG,NOSTVREN,NOPROTECT
#include <flex_lcd420.h>
#include <pcf8583.h>
void main() {
lcd_init();
while(TRUE) {
Gettime();
lcd_gotoxy(1,1);
printf(lcd_putc,"Time: %01u%01u:%01u%01u:%01u%01u",time[4],time[5],time[2],time[3],time[0],time[1]);
}
}
Code:
#ifndef PCF8583_SDA
#define PCF8583_SDA PIN_C4
#define PCF8583_SCL PIN_C3
#endif
#use i2c(master,sda=PCF8583_SDA,scl=PCF8583_SCL,FORCE_HW,RESTART_WDT)
// Defines address for Read and Write ON I2C BUS
#define PCF8583_ADDR_WRITE 0xA0
#define PCF8583_ADDR_READ 0xA1
// Defines internal registers
#define CONTROL_STATUS_REG 0x00
#define HUNDREDTH_OF_SEC_REG 0x01
#define SECONDS_REG 0x02
#define MINUTES_REG 0x03
#define HOURS_REG 0x04
#define YEAR_DATE_REG 0x05
#define WEEKDAYS_MONTHS_REG 0x06
#define TIMER_REG 0x07
#define ALARM_CONTROL_REG 0x08
#define ALARM_HUNDREDTH_OF_SEC 0x09
#define ALARM_SECONDS 0x0A
#define ALARM_MINUTES 0x0B
#define ALARM_HOURS 0x0C
#define ALARM_DATE 0x0D
#define ALARM_MONTH 0x0E
#define ALARM_TIMER 0x0F
#define FREE_RAM_START 0x10
#define FREE_RAM_END 0xFF
// Defines Control/Status Register (RESET STATE: 0000 0000)
typedef struct {
byte TIMER_FLAG :1; // 50% duty factor seconds flag if ALARM_ENABLE bit is 0
byte ALARM_FLAG :1; // 50% duty factor minutes flag if ALARM_ENABLE bit is 0
byte ALARM_ENABLE :1; // 0 -> alarm disabled, 1 -> enable alarm
byte MASK_FLAG :1; // 0 -> read locations 05 to 06 unmasked, 1 -> read date and month count directly
byte FUNCTION_MODE :2; // 00 -> clock mode 32.768 kHz, 01 -> clock mode 50 Hz, 10 -> event-counter mode, 11 -> test modes
byte HOLD_COUNT :1; // 0 -> count, 1 -> store and hold last count in capture latches
byte STOP_COUNT :1; // 0 -> count pulses, 1 -> stop counting, reset divider
} CTRLSTATUSREG;
// <CtrlStatusReg> bit settings
// Alarm enable bit settings
#define ALARM_OFF 0x00
#define ALARM_ON 0x01
// Mask flag settings
#define MASK_FLAG_OFF 0x00
#define MASK_FLAG_ON 0x01
// Function mode settings
#define CLOCK_32_768KHZ 0x00
#define CLOCK_50HZ 0x01
#define EVENT_COUNTER 0x02
#define TEST_MODES 0x03
// Hold last count settings
#define COUNT 0x00
#define STORE_HOLD_LAST_COUNT 0x01
// Stop count settings
#define COUNT_PULSES 0x00
#define STOP_COUNTING 0x01
// Used in 'byte PCF8583_Counter(byte Status)'
#define COUNTER_OFF 0x00
#define COUNTER_ON 0x01
// Defines seconds register (RESET STATE: ???? ????)
struct {
byte UNITS_BCD :4; // Unit seconds BCD format
byte TENS :3; // Ten seconds, 0 to 5 binary
byte NOT_UNUSED :1; // NOT USED
} SECONDSREG;
// Defines minutes register (RESET STATE: ???? ????)
struct {
byte UNITS_BCD :4; // Unit minutes BCD format
byte TENS :3; // Ten minutes, 0 to 5 binary
byte NOT_UNUSED :1; // NOT USED
} MINUTESREG;
// Defines hours register (RESET STATE: 0000 0000)
struct {
byte UNITS_BCD :4; // Unit hours BCD format
byte TENS :2; // Ten hours, 0 to 2 binary
byte AM_PM_FLAG :1; // 0 -> AM, 1 -> PM
byte FORMAT :1; // 0 -> 24h format, 1 -> 12h format
} HOURSREG;
// Defines yeardate register (RESET STATE: 0000 0001)
struct {
byte UNIT_DAYS_BCD :4; // Unit days BCD format
byte TEN_DAYS :2; // Ten days, 0 to 3 binary
byte YEAR :2; // 0 to 3 binary, reads as 0 if the mask flag is set
} YEARDATEREG;
// Defines weekdaysmonths register (RESET STATE: 0000 0001)
struct {
byte UNIT_MONTHS_BCD:4; // Unit months BCD format
byte TEN_MONTHS :1; // Ten months
byte WEEKDAYS :3; // Weekdays, 0 to 6 binary, reads as 0 if mask flag is set
} WEEKDAYSMONTHS;
// Defines Alarm control register, clock mode (RESET STATE: 0000 0000)
typedef struct {
byte TIMER_FUNCTION :3; // 000 -> timer, 001 -> hundr.of seconds, 010 -> seconds, 011 -> minutes, 100 -> hours, 101 -> days
byte TIMER_INTERRUPT_ENABLE :1; // 0 -> timer flag (no interrupt), 1 -> timer flag (interrupt)
byte CLOCK_ALARM_FUNCTION :2; // 00 -> no alarm clock, 01 -> daily alarm, 10 -> weekday alarm 11 -> dated alarm
byte TIMER_ALARM_ENABLE :1; // 0 -> no timer alarm, 1 -> timer alarm
byte ALARM_INTERRUPT_ENABLE :1; // 0 -> alarm flag (no interrupt), 1 -> alarm flag (interrupt)
} ALARMCTRLREG;
// <AlarmCtrlReg> bit settings
// Timer Function settings
#define NO_TIMER 0x00
#define HUNDREDTHS_OF_SECONDS 0x01
#define SECONDS 0x02
#define MINUTES 0x03
#define HOURS 0x04
#define DAYS 0x05
// Timer Interrupt settings
#define TIMER_FLAG_NO_INTERRUPT 0x00
#define TIMER_FLAG_INTERRUPT 0x01
// Clock Alarm Function settings
#define NO_CLOCK_ALARM 0x00
#define DAILY_ALARM 0x01
#define WEEKDAY_ALARM 0x02
#define DATED_ALARM 0x03
// Timer Alarm Enable settings
#define NO_TIMER_ALARM 0x00
#define TIMER_ALARM 0x01
// Alarm Interrupt Enable settings
#define ALARM_FLAG_NO_INTERRUPT 0x00
#define ALARM_FLAG_INTERRUPT 0x01
// Alarm WeekDays settings
#define SUNDAY 0x01
#define MONDAY 0x02
#define TUESDAY 0x04
#define WEDNESDAY 0x08
#define THURSDAY 0x10
#define FRIDAY 0x20
#define SATURDAY 0x40
byte time[6];
byte decD2hexD(byte value) {
return(value%10+16*(value/10));
}
byte hexD2decD(byte value) {
return(value%16+10*(value/16));
}
byte pcf8583_read(byte address) {
byte data;
i2c_start();
i2c_write(PCF8583_ADDR_WRITE);
i2c_write(address);
i2c_start();
i2c_write(PCF8583_ADDR_READ);
data=i2c_read(0);
i2c_stop();
return(data);
}
void Gettime() {
SECONDSREG=pcf8583_read(SECONDS_REG);
MINUTESREG=pcf8583_read(MINUTES_REG);
HOURSREG=pcf8583_read(HOURS_REG);
time[0]=hexd2decD(SECONDSREG.TENS);
time[1]=hexd2decD(SECONDSREG.UNITS_BCD);
time[2]=hexd2decD(MINUTESREG.TENS);
time[3]=hexd2decD(MINUTESREG.UNITS_BCD);
time[4]=hexd2decD(HOURSREG.TENS);
time[5]=hexd2decD(HOURSREG.UNITS_BCD);
}