INTERFACING OF DS1307 WITH PIC16F877A WITHOUT USING LIBRARY FUNCTIONS

Home Forums Microcontrollers PIC Microcontroller INTERFACING OF DS1307 WITH PIC16F877A WITHOUT USING LIBRARY FUNCTIONS

Tagged: , , , ,

This topic contains 3 replies, has 3 voices, and was last updated by  Ligo George 1 year, 4 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #11860

    George Mathew
    Participant

    */use pull up resistors of 330k ,crystal freq:32.768/*

    Program:

    #include "uartfun.h" //created myown header file for uart functions
    
    void i2c_intialise(int);
    void i2c_start();
    void i2c_stop();
    void i2c_restart();
    void i2c_ack();
    void i2c_nack();
    void WaitSSP();
    void i2c_send(unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int,unsigned int);
    unsigned int i2c_readx(int);
    void wait();
    
    void main()
    {
      unsigned char time[20]="Time:00:00:00";
      unsigned char dayx[20]="Day:__";
      unsigned char datex[20]="Date:__/__/__";
      short sec;
      short min;
      short hr;
      short days;
      short dates;
      short months;
      short years;
      char seconds[5];
      char minutes[5];
      char hours[5];
      char dayss[5];
      char datess[5];
      char monthss[5];
      char yearss[5];
    
      uart_intialise(31);
      i2c_intialise(151);
      i2c_send(0X00,0X55,0X09,0X01,0X018,0X10,0X15);// write data  for time,day,calendar
    
      delay_ms(100);
    
      while(1)
      {
        sec=i2c_readx(0X00);
        delay_us(100);
        min=i2c_readx(0X01);
        delay_us(100);
        hr=i2c_readx(0X02);
        delay_us(100);
        days=i2c_readx(0X03);
        delay_us(100);
        dates=i2c_readx(0X04);
        delay_us(100);
        months=i2c_readx(0X05);
        delay_us(100);
        years=i2c_readx(0X06);
        delay_us(100);
        
        shorttohex(sec,seconds); //converts short to string
        shorttohex(min,minutes);
        shorttohex(hr,hours);
        shorttohex(days,dayss);
        shorttohex(dates,datess);
        shorttohex(months,monthss);
        shorttohex(years,yearss);
        
        time[5]=hours[0];
        time[6]=hours[1];
        time[8]=minutes[0];
        time[9]=minutes[1];
        time[11]=seconds[0];
        time[12]=seconds[1];
        
        dayx[4]=dayss[0];
        dayx[5]=dayss[1];
    
        datex[5]=datess[0];
        datex[6]=datess[1];
        datex[8]=monthss[0];
        datex[9]=monthss[1];
        datex[11]=yearss[0];
        datex[12]=yearss[1];
    
        //below functions own created to display over uart
        uart_ctransmit('\f');
        uart_stransmit(time);
        uart_ctransmit('\r');
        uart_stransmit(dayx);
        uart_ctransmit('\r');
        uart_stransmit(datex);
        delay_ms(100);
      }
    }
    
    void i2c_intialise(int n)
    {
      TRISC.RC3=1;  // SCL
      TRISC.RC4=1;  // SDA
      SSPSTAT.SMP=1; //slewrate disabled
      SSPCON=0X28; //i2c master mode  SSPEN=1,1000 for master mode baud rate
      SSPADD=n;  //load value to set baudrate ,use formula
    }
    
    void i2c_start()
    {
      SSPCON2.SEN=1; //enabled start condition
      while(SSPCON2.SEN);//wait for start condition to finish,automatically cleared by hardware
    }
    
    void i2c_stop()
    {
      SSPCON2.PEN=1;  //enabled stop condition
      while(SSPCON2.PEN);//wait for stop condition to finish,automatically cleared by hardware
    }
    
    void i2C_restart()
    {
      SSPCON2.RSEN=1;//repeated start enabled
      while(SSPCON2.RSEN);//wait for restart condition to finish,automatically cleared by hardware.
    }
    void i2c_ack()
    {
      SSPCON2.ACKDT=0; //acknowledge data bit as 0,
      SSPCON2.ACKEN=1;//acknowledgement enabled;
      while(SSPCON2.ACKEN);//wait for ack data to send
    }
    
    void i2c_nack()
    {
      SSPCON2.ACKDT=1;  //acknowledge data bit as 1;
      SSPCON2.ACKEN=1; //acknowledgement enabled
      while(SSPCON2.ACKEN);//wait for nack,data to send on bus
    }
    /* below function to write/set the time & date*/
    void i2c_send(unsigned int second,unsigned int  minute, unsigned int hour,unsigned int day,unsigned int date,unsigned int month,unsigned int year)
    {
      i2c_start();   //start
      WaitSSP(); //wait for flag bit to set,cleared manually in software.
      SSPBUF=0XD0;//device address + write command
      WaitSSP();
      SSPBUF=0X00;//write location;
      WaitSSP();
      SSPBUF=second; //move data for second
      WaitSSP();
      SSPBUF=minute; //move data for min
      WaitSSP();
      SSPBUF=hour;//move data for hour
      WaitSSP();
      SSPBUF=day;//move data for day
      WaitSSP();
      SSPBUF=date;//move data for date
      WaitSSP();
      SSPBUF=month;//move data for month
      WaitSSP();
      SSPBUF=year;//move data for year
      WaitSSP();
      i2c_stop();  //stop
      delay_ms(100);
    }
    
    /*below function is used to read data*/
    unsigned int i2c_readx(int addr)
    {
      unsigned int tmp;
      i2c_restart(); //restart
      WaitSSP();
      SSPBUF=0XD0; //slave address + write command
      WaitSSP();
      SSPBUF=addr;
      WaitSSP();
      i2c_restart();
      WaitSSP();
      SSPBUF=0XD1; //slave address command + read command
      WaitSSP();
      SSPCON2.RCEN=1; //enable reception
      WaitSSP();
      i2c_nack();//send acknowledgment as bit 1
      i2c_stop(); //stop
      tmp=SSPBUF;
      return tmp;
    }
    
    void WaitSSP() //function to check interrupt flag for i2c
    {
      while(PIR1.SSPIF==0);
      PIR1.SSPIF=0;
    }
    
    #11861

    Ligo George
    Keymaster

    Hi,

    Thank you for sharing this code.

    #12223

    lalgpt
    Participant

    Dear Ligo George

    Your commitment on this website is highly appreciable.

    let me know that how the hour or minutes or second is compared in this example

    Because it consists of Binary, BCD, and character.  which is compared to trigger a out for a specified time ?

     

     

    #12224

    Ligo George
    Keymaster

    You can compare any of it, just use if statements.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

string(7) "9898001"