Electrical – Couldn’t read the correct values of DS1302 RTC Registers

proteusrtc

I am trying to read register values of DS1302 RTC using a PIC MCU in proteus environment. I have made two simple functions for that purpose, one is for the transfer of DATA to the RTC and other is for receiving from it.

I can only get the correct value of seconds register but all other values (min, days, months etc) are either 0xFF or 0x00. Couldn't find where the problem lies.

I have been tracking the bytes transfer to the RTC using an OSCILLOSCOPE which shows that the data is correctly transferring on the RISING EDGE of the clock. I am not initializing RTC values (because the RTC won't LATCH my data) so I initialize it to the system calendar (option available in proteus).

enter image description here

Here I am sending the address of the second's register (0x81) and receiving the second's value (correct value) from RTC.
enter image description here

Here I am sending the value of minutes and getting (0xFF) everytime.
enter image description here

And for Hour register, I get 0x00. For DATE register I get 0xFF and vice versa.

Here are the two functions through which I am sending and receiving data.

char value=0x00;

void SPI_SEND_DATA(char ADDRESS,char DATA){
           delay_us(1);
           EN=1;
           SSPBUF = ADDRESS;            // Transmit ADDRESS
           while(!SPI_BUSY_FLAG);
           value=SSPBUF;               // Clear Bit flag
           SSPBUF = DATA;              // Transmit DATA
           while(!SPI_BUSY_FLAG);
           value=SSPBUF;               // Clear BF flag
           EN=0;
}

char SPI_RCV_DATA(char ADDRESS){
            delay_us(1);
            EN=1;
            SSPBUF = ADDRESS;
            while(!SPI_BUSY_FLAG);
            value=SSPBUF;               // Clear Bit flag
            SSPBUF=0x00;                // Start the SCLK
            while(!SPI_BUSY_FLAG);      // Recieve data
            value=SSPBUF;               // Store the recieved Byte
            EN=0;                       // Fininsh Reading
            return value;
}

Here is how I am initializing the clock in proteus automatically and the clock ticks quite fine.
enter image description here

I have also attached the full project code and simulation file of proteus, in case anyone want to try it. Kindly find in the attachment herewith.
Full Project files

Any help/suggestion would be appreciated.

EDIT: I tried the same code on MAX6902 [the only thing I changed is; I have inverted the ENABLE pin because according to the MAX6902 datasheet, its active low] and it worked like charm. The thing is, proteus may have some problem in simulating DS1302.

Best Answer

Did you set the command byte properly ?. I couldn't see that in your code. The RD/WR bit of command byte should be set properly according to the data direction