EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / RTC

RTC

|

Microcontroller › PIC › RTC

  • This topic has 2 replies, 3 voices, and was last updated 6 years, 10 months ago by Ashutosh Bhatt.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • August 17, 2018 at 5:33 am #4959
    Rian
    Participant

    controller:PIC16F690
    RTC:DS1307

    It is working fine for Proteus but it dose not work for my actual hardware please advice
    please check my code

     

    ////////////////////////////////////////////////////////////////////////////

     

    #include <xc.h>
    #include "lcd.h"
     
     
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
     
    // CONFIG
    #pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
    #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
    #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
    #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
    #pragma config BOREN = ON       // Brown-out Reset Selection bits (BOR enabled)
    #pragma config IESO = ON        // Internal External Switchover bit (Internal External Switchover mode is enabled)
    #pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
     
    #define _XTAL_FREQ 8000000
    #define LED PORTAbits.RA0
     
    #define SDA PORTBbits.RB4
    #define SCL PORTBbits.RB6
    #define SADDR TRISBbits.TRISB4
    char SEC,MIN,HOUR,DATE,MONTH,YEAR,DAY;
    int a,b,c,d,e,f,g,h;
    char DAYS[8][4]={"SUN","MUN","TUE","WED","THU","FRI","SAT"};
    char arr[7];
    char time[8];
    void START_I2C()
    {
        SDA=1;
        SCL=1;
        __delay_ms(20);
        SDA=0;
         __delay_ms(20);
         SCL=0;
       //  LCD_POS(2,0×01);
      //   LCD_STRING("S");
    }
     
    void STOP_I2C()
    {
        SDA=0;
        SCL=1;
        SCL=1;
         __delay_ms(20);
         SDA=1;
         
        // LCD_POS(1,0×08);
        // LCD_STRING("p");
          
    }
    void WRITE_I2C_DATA(char d)
    {
        char i;
        SDA=0;
        SCL=0;
        __delay_ms(20);
        for(i=0;i<=7;i++)
        {
            if((d & 0x80) ==0x80)
            {
                
                SDA=1;
            }
            else
            {
                SDA=0;
            }
            
            d=d<<1;
            __delay_us(20);
           // SCL=1;
            SCL=1;
             __delay_us(20);
             SCL=0;
             __delay_us(20);
             
        }    
             i=SDA;
             SCL=1;
             __delay_us(20);
             SCL=0;
             __delay_us(20);
            
         /*   for(i=0;i<=7;i++)
             {
            SDA=((d&0x80)?1:0);
            d=d<<1;
            __delay_us(20);
            SCL=1;
            SCL=1;
            __delay_us(20);
            SCL=0;
            __delay_us(20);
     
            }
           i=SDA;
           SCL=1;
         __delay_us(20);
           SCL=0;
         __delay_us(20);
      */
    }
    void WRITE_I2C(char data,char caddr)
    {
        START_I2C();
        WRITE_I2C_DATA(0xD0);
        WRITE_I2C_DATA(data);
        WRITE_I2C_DATA(caddr);
        STOP_I2C();
    }
    void SET_CLOCK()
    {
       
        WRITE_I2C(0x00,0x00); //SEC
        __delay_ms(2);
        WRITE_I2C(0x01,0x58); //MIN
        __delay_ms(2);
         WRITE_I2C(0x02,0x16);//HOUR
        __delay_ms(2);
         WRITE_I2C(0x03,0x6);//DAY
        __delay_ms(2);
         WRITE_I2C(0x04,0x18);//DATE
        __delay_ms(2);
         WRITE_I2C(0x05,0x09);//MONTH
        __delay_ms(2);
         WRITE_I2C(0x06,0x15);//YEAR
        __delay_ms(2);
     
        
    }
    char READ_I2C_DATA()
    {
        char i;
        char j;
        SADDR=1;
        SCL=0;
        j=0x00;
        
        for(i=0;i<=7;i++)
        {
            
            j=j<<1;
            SCL=1;
            j |=SDA;
            SCL=0;
            
        }
        SADDR=0;
        SDA=1;
        //SCL=1;
        SCL=1;
        SCL=0;
        return j;
        
    }
    char READ_I2C(char daddr)
    {
        char s1;
        START_I2C();
        WRITE_I2C_DATA(0xd0);
        WRITE_I2C_DATA(daddr);
        START_I2C();
        WRITE_I2C_DATA(0xd1);
        s1= READ_I2C_DATA();
       
        STOP_I2C();
        return s1;
    }
    /*
    char  CONVERT(int fdata)
    {
        fdata=(fdata>>4) + '0';
        return fdata;
    }
    char  CONVERT1(int gdata)
    {
        gdata=(gdata>>4) + '0';
        return gdata;
    }
     */
    int main(void)
    {  
      
      long int s=0;
      OSCCONbits.IRCF=0x7;
      ANSEL=0X00;
      ANSELH=0X00; 
      
      TRISB=0x00;
      
      TRISAbits.TRISA0=0x00;
      TRISAbits.TRISA1=0x00;
      TRISAbits.TRISA2=0x00;
      
      TRISCbits.TRISC0=0x00;
      TRISCbits.TRISC1=0x00;
      TRISCbits.TRISC2=0x00;
      TRISCbits.TRISC3=0x00;
      
     //  INTCONbits.INTF=0;
     //__delay_us(20);
      LCD_PRE_INIT();
      __delay_us(200);
      LCD_INIT();
      __delay_us(10);
      LCD_INIT();
      __delay_us(10);
      LCD_INIT();
      __delay_us(10);
      __delay_ms(200);
      SET_CLOCK();
      __delay_ms(10);
      LCD_POS(1,0×01);
     __delay_us(20);
      LCD_CMD(0x80);
      LCD_STRING("SAGAR");
      LCD_CMD(0xE0);
     
     
     
     
      while(1)
      { 
         
          s++;
          SEC=READ_I2C(0x00);
          MIN=READ_I2C(0x01);
          HOUR=READ_I2C(0x02);
          DAY=READ_I2C(0x03);
        /*  DATE=READ_I2C(0x04);
          MONTH=READ_I2C(0x05);
          YEAR=READ_I2C(0x06);*/
         
          
          LCD_CLEAR();
          LCD_POS(1,0×01);
          
          
       /*   SEC=arr[0];
          MIN=arr[1];
          HOUR=arr[1];
          
          time[6]=CONVERT(SEC);
          time[7]=CONVERT1(SEC);
          time[5]=':';
          time[3]=CONVERT(MIN);
          time[4]=CONVERT1(MIN);
          time[2]=':';
          time[1]=CONVERT(HOUR);
          time[0]=CONVERT1(HOUR);
          int i;
          for(i=0;i<=7;i++)
          {
              LCD_DATA(time);
          }
          */
           
         // HOUR=0x05;
       // LCD_DATA(0x05);
          LCD_DATA(HOUR/0x10+0x30);
          LCD_DATA(HOUR%0x10+0x30);
          
        //  __delay_ms(1);
          
          LCD_DATA(':');
          LCD_DATA(MIN/0x10+0x30);
          LCD_DATA(MIN%0x10+0x30);
         // __delay_ms(1);
          
          LCD_DATA(':');
          
          LCD_DATA(SEC/0x10+0x30);
          LCD_DATA(SEC%0x10+0x30);
          LCD_DATA(' ');
          LCD_DATA(' ');
        //  __delay_ms(1);
          
        //  LCD_STRING(DAY[DAYS-1]);
        //  __delay_ms(1);
          
        /*  LCD_POS(2,0×01);
          
          LCD_DATA(DATE/0x10+0x30);
          LCD_DATA(DATE%0x10+0x30);
         // LCD_STRING('');
          LCD_DATA(' ');
          LCD_STRING("/");
          LCD_DATA(' ');
        //  __delay_ms(1);
          
          LCD_DATA(MONTH/0x10+0x30);
          LCD_DATA(MONTH%0x10+0x30);
          
          LCD_DATA(' ');
          LCD_STRING("/");
          LCD_DATA(' ');
        //  __delay_ms(1);
          
          LCD_DATA(YEAR/0x10+0x30);
          LCD_DATA(YEAR%0x10+0x30);*/
          
         //  __delay_ms(1);
           
           if(s>=20)
           {
               s=0;
           }
          
          
          
        }
     
    LCD_TIME(40);
    LCD_CLEAR();
     
    }
     
    December 26, 2018 at 6:20 am #14984
    selva
    Participant

    check out this –


    #include

    //Define PIC Registers #include

    __CONFIG(0x3f72);

    //Select HS oscillator, Enable (PWRTE,BOREN),

    //Disable (CPD,CP,WDTEN,In-circuit Debugger)

    #define LC01CTRLIN 0xd0

    #define LC01CTRLOUT 0xd1

    #define I2C_FREG 100

    #define FOSC 10000

    #define BAUD_RATE 9.6

    // 9600 Baud rate #define BAUD_VAL (char)(FOSC/ (16 * BAUD_RATE )) - 1;

    //Calculation For 9600 Baudrate @10Mhz unsigned char sec,min,hour,day,date,month,year; unsigned char data[7]={0x45,0x59,0x71,0x04,0x05,0x10,0x06};

    int i;

    void DS1307Write(unsigned char,unsigned char);

    void WaitMSSP();

    unsigned char DS1307Read(unsigned char);

    void i2c_init(void);

    void ds1307_init(void);

    void serial_init(void);

    void DelayMs(unsigned int);

    void main()

    {

    int count=0;

    DelayMs(20);

    ds1307_init();

    serial_init();

    for(i=0;i<7;i++) DS1307Write(i,data);

    printf("33[2J");

    DelayMs(20);

    while(1)

    {

    sec=DS1307Read(0);

    // Read second min=DS1307Read(1);

    // Read minute hour=DS1307Read(2);

    // Read hour day=DS1307Read(3);

    // Read day date=DS1307Read(4);

    // Read date month=DS1307Read(5);

    // Read month year=DS1307Read(6);

    // Read year printf("Time: %x : %x : %x ",(hour&0x1f),min,sec);

    //Display the Hours, Minutes, Seconds(hours is taken from 5 LSB bits printf("Date: %x / %x / %x r",date,month,year);

    //Display the Date, Month, Year DelayMs(150);

    }

    }

    void DS1307Write(unsigned char addr, unsigned char data)

    {

    SEN=1;

    //Initiate Start condition on SDA & SCL pins WaitMSSP();

    SSPBUF=LC01CTRLIN;

    // Slave address + Write command WaitMSSP();

    SSPBUF=addr;

    // Write the location WaitMSSP();

    SSPBUF=data;

    // Write the Data WaitMSSP();

    PEN=1;

    // Enable the Stop bit WaitMSSP();

    }

    unsigned char DS1307Read(unsigned char addr)

    {

    unsigned char x;

    RSEN=1;

    // Enable the repeated Start Condition WaitMSSP ();

    SSPBUF=LC01CTRLIN;

    // Slave address + Write command WaitMSSP ();

    SSPBUF=addr;

    //Write the location (memory address of Hour, minute, etc... WaitMSSP ();

    RSEN=1;

    // Enable the repeated Start Condition WaitMSSP ();

    SSPBUF=LC01CTRLOUT;

    // Slave address + Read command WaitMSSP ();

    RCEN=1;

    // Enable to receive data WaitMSSP ();

    ACKDT=1;

    // Acknowledge the operation (Send NACK) ACKEN=1;

    // Acknowledge sequence on SDA & SCL pins PEN=1;

    // Enable the Stop bit WaitMSSP ();

    x=SSPBUF;

    // Store the Receive value in a variable return (x);

    }

    void WaitMSSP()

    {

    while(!SSPIF);

    // SSPIF is zero while TXion is progress SSPIF=0;

    }

    void ds1307_init()

    {

    TRISC3=1;

    // RC3,RC4 set to I2C Mode(Input) TRISC4=1;

    SSPCON=0x28;

    // Enable the SDA,SCL & I2C Master Mode SSPADD=(FOSC / (4 * I2C_FREG)) - 1;

    // SSP baud rate 100Khz SSPSTAT=0x80;

    // Disable slew Rate control PORTC=0x18;

    DS1307Write(0,0x00);

    }

    void serial_init()

    { TRISC6=1;

    // RC7, RC6 set to USART Mode TRISC7=1;

    TXSTA=0x24;

    // Enable Transmission, Asynchronous mode, High Speed mode SPBRG=BAUD_VAL;

    // 9600 Baud rate selection RCSTA=0x90;

    // Enable Serial Port & Continuous Reception TXIF=1;

    // Enable Transmission

    }

    void putch(unsigned char byte)

    //Required for printf statement

    {

    while(!TXIF);

    // Wait for the Transmit Buffer to be empty TXREG = byte;

    // Transmit the Data

    }

    void DelayMs(unsigned int Ms)

    {

    int delay_cnst;

    while(Ms>0)

    {

    Ms--;

    for(delay_cnst = 0;delay_cnst <220;delay_cnst++);

    }

    }

    December 26, 2018 at 1:09 pm #14986
    Ashutosh Bhatt
    Participant

    in proteus if its working means your code/software is OK

    some times it happens that the project works fine in proteus simulation but wont work on actual hardware due to hardware problems

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Manually actuate fuel tank selector solenoid November 8, 2025
  • JLCPBC are using a different shipping company = less $$$$$$$$ November 8, 2025
  • Help please! BLDC driver circuit using the IR2136s and the STP80NF06 MOSFETS November 8, 2025
  • need two ICs November 8, 2025
  • MLT-8530 Buzzer on ESP32: Why Is the Sound Output Lower Than Expected? November 8, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise