Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #10610
    Anthony820
    Participant

    Salve,
    I'm new to the group, and for some time I have been trying to make a DS1302 RTC communicate with a PIC but with little success.
    Could someone help me out, in order to understand where the error may be.
    Thank you.

    I insert a piece of code for reading the seconds.

    #define RB1 RST   //enable del DS1302
    #define RB2 SCLK // clock  DS1302
    #define RB3 IO  // input/output DS1302
    
    Main(){
        ANSEL=0b00000000;
        TRISB=0b00000000;
        unsigned char=secondi;
        RST=0;
        Init_DS1302();
        Set_time();
        while(1){
        Secondi=read_time();}
    }
    
    Init_DS1302(){
        Write_time(0xBE);  //cmd per dissattivare la protezione
        Write_time(0x00); //codice disattivamento
    }
    
    Set_time(){
        Write_time(0x80);  //cmd per scrivere i secondi
        Write_time(0x00); // valore secondi
    }
    
    Write_time(unsigned char comando){
        Int i;
        RST=1;
        for(i=0; i<8; i++){
            SCLK=0;
            If((comando&0x80))
                IO=1;
            else 
                IO=0;
            SCLK=1;
        }
        SCLK=0;
        RST=0;
    }
    
    Read_time(){
        int i, valore, valore_finale;
        valore=0;
        write_time(0x81);
        TRISB=0b00001000;
        RST=1;
        for(i=0;i<8;i++){
            SCLK=1;
            If(IO&0x01)
                valore=valore+0x01;  
            else
                valore=valore +0x00;
            valore_finale=valore;  //salva su valore finale prima dello shift
            valore=valore <<1;
            SCLK=0;
        }
        TRISB=0b00000000;
        RST=0;
        return valore_finale;
    }
    #10614
    Picmicro675
    Moderator

    It is not clear which compiler you use. Then it depends on which micro you use. In my opinion it is not worth buying the 16F8xx family, better the more recent ones like 16F1xxx that maybe you already find in the hardware the necessary to transmit.
    Then I think it's timing issues, the data sheet refers to a maximum clock of half a MHz. So try to introduce some NOP or delay of a few cycles.
    As a last resort you should have a logic analyzer to see the signal produced, at most a double trace oscilloscope. If you use the DS 1307 you should stick to the I2C specifications.
    Last tip try MikroC and its libraries.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.