1

so I am currently trying to Read and Write the time to the RTC and these methods will just not work.

this is what I have: so it sends the address byte to determine where it would like to read from then flips the pin to input and reads each bit out until the byte is complete.

 char ReadByte(char ByteToRead)
 {
    RB0 = 0; // ensure CLK low
    RB5 = 1;
    char received = 0;
    int i;

    for(i =0;i < 8 ;i++)
    {   
        RB4 = ByteToRead & 1;
        RB0 = 0;
        ByteToRead >>= 1;       
        RB0 = 1;

    }
    TRISB4 = 1;//B4 = input port

    for(i =0;i < 8 ;i++)
    {
        received |= RB4;
        RB0 = 1;
        received <<= 1;
        RB0 = 0;
    }

    RB5=0;

    TRISB4 = 0;

    return DecimalToBCD(received);
}

the second sample is my write method:

void WriteByte(char ClockReg ,char data)

{
    RB0 = 0; // ensure CLK low
    RB5 = 1; // raises RST bit
    int i;
    for(i =0;i < 8 ;i++)
    {   
        RB4 = ClockReg & 1;
        RB0 = 0;
        ClockReg >>= 1;     
        RB0 = 1;
    } 

    for(i =0;i < 8 ;i++)
    {   

        RB4 = data & 1;
        RB0 = 0;
        data >>= 1;     
        RB0 = 1;

    }
    RB5 = 0;
    __delay_us(1);
    RB5 = 1;
}

this does the same for the first bit to determine the register the write the value you give it.

the only help I can find online are links to a 2 year old page where there is no code or help just people asking to be PMed. So please if you can help

Thanks in advance

  • So, what's the question? What's not working exactly? What parts are you having trouble with? – Nocturno Dec 06 '12 at 02:19
  • well I cannot determine whether either the read or the write methods are working, as I need the read to work out the write and to find if the read is working i need to write. – Chef Dan Burns Dec 06 '12 at 15:12
  • This is an instance where having a USB-based logic probe can save great deals of time. You can monitor the actual output lines to see what's happening, and many can decode SPI and I2C signalling for you. – tomlogic Dec 06 '12 at 18:04
  • I'd agree it would be useful but I have a small amount of time to do this and not one to hand, but I may look in to getting on for future projects. any recommendations ? – Chef Dan Burns Dec 06 '12 at 18:45

2 Answers2

0

I'd work on the ReadByte method before you work on the WriteByte because then you can validate the write by reading it back. So, looking at the ReadByte routine, I can quickly see one problem.

The datasheet for the DS1302 says "data bits are output on the falling edge of clock" but in your ReadByte routine, the input on RB4 is read and then later you set the clock low. Also "Note that the first data bit to be transmitted occurs on the first falling edge after the last bit of the command byte is written." but your first time through the loop, you are reading RB4 before the first falling edge.

Martin
  • 2,442
  • 14
  • 15
  • I realised after posting that this is the code i played about with, I had it like this before ' RB4 = ByteToRead & 1; ByteToRead >>= 1; RB0 = 1; RB0 = 0; ' so It would read the falling edge, but tried to loop it so there was a smaller delay than 1 Us. sorry about the confusion. – Chef Dan Burns Dec 06 '12 at 15:14
0

To be confimed !

I believe it is because I had not disabled the Write protect bit also for future reader the Clock Halt flag (CH) is to be cleared not set.

Hope this helps anyone looking for it Dan