DS1307 with 16f877

Discussion in 'Programmer's Corner' started by sirch, May 19, 2008.

  1. sirch

    Thread Starter New Member

    May 19, 2008
    5
    0
    Hello friends !
    I'm interfacing DS1307 RTC with 16f877 using I2C communication. RTC is acknowledging but data received is not correct .Please help me. Its urgent!!!:(:(:(
    MY code is
    //RTC program
    void serial();
    int data;
    void ssp_ack();
    unsigned char receive(unsigned char addr);
    unsigned char parameter[15],tx;
    void transformm();
    void main()
    { //DS1307 INIT STARTS

    delay_ms(20);
    STATUS.RP0=1;
    //INTCON=0xD0;
    TRISC.F3=1; //chng
    TRISC.F4=1; //chng
    SSPSTAT=0x80;//CKE=0 and smp=1; chng
    SSPADD=10;
    STATUS.RP0=0;
    SSPCON=0X2E; //SSPEN ENABLE, MODE=1000(8)

    //WRITE CYCLE STARTS
    STATUS.RP1=0;
    STATUS.RP0=0;
    PIR1.SSPIF=0;
    STATUS.RP0=1;
    SSPCON2.SEN=1;
    STATUS.RP0=0;
    ssp_ack();

    SSPBUF=0xD0; //CONTROLIN
    ssp_ack();
    STATUS.RP0=0;
    STATUS.RP1=0;
    SSPBUF=0x00; //reg pointer
    ssp_ack();
    SSPBUF=0x06; //set seconds
    ssp_ack();
    SSPBUF=0x06; //set minutes
    ssp_ack();
    SSPBUF=0x06; //set hr
    ssp_ack();
    SSPBUF=0x06; //set day
    ssp_ack();
    SSPBUF=0x06; //set date
    ssp_ack();
    SSPBUF=0x06;
    ssp_ack();
    SSPBUF=0x06;
    ssp_ack();
    SSPCON2.PEN=1;//STOP bit transmit
    while(!PIR1.SSPIF);
    PIR1.SSPIF=0;
    delay_ms(20);
    receive(0);
    parameter[0]=receive(0); //sec
    parameter[1]=receive(1); //min
    parameter[2]=receive(2); //hr
    parameter[3]=receive(3); //day
    parameter[4]=receive(4); //date
    parameter[5]=receive(5); //month
    parameter[6]=receive(6); //year
    SSPCON2.ACKDT=1;//Set NACK value
    SSPCON2.ACKEN=1;
    SSPCON2.RCEN=1;
    ssp_ack();
    delay_ms(20);
    transformm();
    serial();
    }

    unsigned char receive(unsigned char addr)
    { unsigned char x;
    a=2;
    tx=0;
    STATUS.RP1=0;
    STATUS.RP0=0;
    PIR1.SSPIF=0;
    STATUS.RP0=1;
    SSPCON2.RSEN=1; //start bit transmit
    STATUS.RP0=0;
    ssp_ack();
    SSPBUF=0xD0; //CONTROLIN
    ssp_ack();
    STATUS.RP0=0;
    STATUS.RP1=0;
    SSPBUF=0x00; //reg pointer
    ssp_ack();
    STATUS.RP0=1;
    SSPCON2.RSEN=1; //start bit transmit
    STATUS.RP0=0;
    ssp_ack();
    SSPBUF=0xD1; //CONTROLOUT
    ssp_ack();
    STATUS.RP0=1;
    SSPCON2.RCEN=1;
    ssp_ack();
    x=SSPBUF;
    SSPCON2.PEN=1; //STOP bit transmit

    while(!SSPIF);
    PIR1.SSPIF=0;
    return(x);
    }
    void ssp_ack()
    {
    while(!PIR1.SSPIF); //CHNG
    //serial(a);
    PIR1.SSPIF=0;
    }
    //serial comm.
    void serial()
    { int i=0;
    //TRISC=0x80;//SELECT TX(RC6) AS INPUT AND RX(RC7) AS OUTPUT
    for(i=0;i<7;i++)
    {
    SPBRG=0x19;// 25 DECIMAL FOR 9600 BAUD RATE
    delay_ms(10);
    TXSTA.F2=1; //BRGH=1 HIGH SPEED
    delay_ms(10);
    TXSTA.F4=0;// ASYNCRONOUS TRANSMIT
    delay_ms(10);
    RCSTA.F7=1;// SPEN SERIAL PORT ENABLE
    delay_ms(10);
    INTCON=0x90;// DISABLE TRANSMIT INTERRUPTS
    delay_ms(10);
    TXSTA.F6=0;// DISABLE 9BIT TRANSMISSION
    delay_ms(10);
    TXSTA.F5=1; // TRANSMIT ENABLE
    delay_ms(10);
    TXSTA.F0=0;// 9TH BIT OF DATA
    delay_ms(10);
    TXREG=parameter;
    while(TXSTA.F1==0);
    delay_ms(10);

    }
    }

    void transformm()
    { parameter[0] = ((parameter[0] & 0xF0) >> 4)*10 + (parameter[0] & 0x0F);
    parameter[1] = ((parameter[1] & 0xF0) >> 4)*10 + (parameter[1] & 0x0F);
    parameter[2] = ((parameter[2] & 0xF0) >> 4)*10 + (parameter[2] & 0x0F);
    parameter[6] = ( parameter[6] & 0xC0) >> 6;
    parameter[3] = ((parameter[3] & 0x30) >> 4)*10 + (parameter[3] & 0x0F);
    parameter[5] = ((parameter[5] & 0x10) >> 4)*10 + (parameter[5] & 0x0F);
    }
     
Loading...