we are using external RTC PCF85263A for PIC32MM0256GPM064 microcontroller .we faced issues utilizing PCF85263A.
we tried to updated the time in RTC.it was successfull but the time is not incremented .
we changed the rtc.it was not work.
kindly suggest the solutions for RTC issue.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#define I2Cwrite 0xA2
#define I2CRead 0xA3
void I2C_wait_for_idle(void)
{
while(I2C3CON & 0x1F);
while(I2C3STATbits.TRSTAT);
}
void I2C_start()
{
I2C_wait_for_idle();
I2C3CONbits.SEN = 1;
while (I2C3CONbits.SEN == 1);
}
void I2C_stop()
{
I2C_wait_for_idle();
I2C3CONbits.PEN = 1;
}
void I2C_restart()
{
I2C_wait_for_idle();
I2C3CONbits.RSEN = 1;
while (I2C3CONbits.RSEN == 1);
}
void I2C_ack(void)
{
I2C_wait_for_idle();
I2C3CONbits.ACKDT = 0;
I2C3CONbits.ACKEN = 1;
while(I2C3CONbits.ACKEN);
}
void I2C_nack(void)
{
I2C_wait_for_idle();
I2C3CONbits.ACKDT = 1;
I2C3CONbits.ACKEN = 1;
while(I2C3CONbits.ACKEN);
}
void I2c_write(unsigned char address, char wait_ack)
{
I2C3TRN = address | 0;
while (I2C3STATbits.TBF == 1);
I2C_wait_for_idle();
if (wait_ack) while (I2C1STATbits.ACKSTAT == 1);
}
void I2c_read(unsigned char *value, char ack_nack)
{
I2C3CONbits.RCEN = 1;
while (I2C3CONbits.RCEN);
while (!I2C3STATbits.RBF);
*value = I2C3RCV;
if (!ack_nack)
I2C_ack();
else
I2C_nack();
}
void I2C_Initialize()
{
LATC = 0x00;
ANSELBbits.ANSB13 = 0;
TRISBbits.TRISB13 = 1;
TRISBbits.TRISB7 = 1;
I2C3CONbits.ON = 1;
I2C3CONbits.DISSLW = 1;
I2C3BRG = 7;
I2C3STAT = 0x0;
}
void PCF85263_write(unsigned char reg_address, unsigned char value)
{
I2C_start();
I2c_write(I2Cwrite , 1);
I2c_write(reg_address,1);
I2c_write(value,1);
I2C_stop();
}
void PCF85263_read(unsigned char reg_address, unsigned char *value)
{
I2C_start();
I2c_write(I2Cwrite , 1);
I2c_write(reg_address,1);
I2C_restart();
I2c_write( I2CRead ,1);
I2c_read(value, 1);
I2C_stop();
}
void rtc_read()
{
PCF85263_read(0x2E, &value);
PCF85263_read(0x28, &data);
PCF85263_read(0x01, &sec);
PCF85263_read(0x02, &min);
PCF85263_read(0x03, &hours);
PCF85263_read(0x04, &date);
PCF85263_read(0x05, &day);
PCF85263_read(0x06, &month);
PCF85263_read(0x07, &years);
}
void rtc_write()
{
PCF85263_write(0x2E, 0x01);
PCF85263_write(0x28, 0xA4);
PCF85263_write(0x01, 0x00);
PCF85263_write(0x02, 0x00);
PCF85263_write(0x03, 0x00);
PCF85263_write(0x04, 0x05);
PCF85263_write(0x05, 0x06);
PCF85263_write(0x06, 0x05);
PCF85263_write(0x07, 0x23);
PCF85263_write(0x2E, 0x00);
}
I'm using the PCF85063A, but I thing your device is similar.
The CTRL register has a STOP bit which stops/disables the clock.
See for example my implementation here:
https://github.com/ErichStyger/McuOnEclipseLibrary/blob/master/lib/src/McuPCF85063A.c
So check your data sheet for a STOP bit and check the setting of this bit.
I hope this helps,
Erich