0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ The above two functions are used to send/rcv data. In the main code, Address/Command byte is send before data read/write. Here is the screenshot of the main code to give you an idea of how i am doing it. dropbox.com/s/1raw1nxhtjdlf5h/Capture.PNG?dl=0 \$\endgroup\$ – Nouman Tajik Apr 11 '17 at 12:26
  • \$\begingroup\$ I still don't see the proper setting of R/w bit. Your spi send and receive functions are wrong. You are writing registers like this SPI_SEND_DATA(0x03,0x15);. For write operation the R/W bit should be 0. You are passing 0x03 as address. In datasheet it is clearly said that the msb (7th bit ) should be 1. Then how can you pass 0x03 as address. Rewrite your Send and receive function. You are not modifying RW bits in your function, you are simply copying that value to SSPBUF. You should do bit mapping to set and reset bits properly according to the command. \$\endgroup\$ – Mukesh Apr 11 '17 at 12:53
  • \$\begingroup\$ Ooops !, My mistake, I have to mention that in comment. Kindly view the EDIT section of my above post, the screenshot was for MAX6902, For DS1302, I have tried 0x81,0x83,0x85,0x87 etc... It didn't work for DS1302 so I used the same function for MAX6902 to check it's validity and it worked quite fine on it. \$\endgroup\$ – Nouman Tajik Apr 12 '17 at 5:30
0
\$\begingroup\$

The problem is solved. I was actually transferring the Command/Address byte in MSB first manner, but DS1302 required Command/Address byte in LSB first.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.