Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Ds1307 RTC IN PIC16F690

Status
Not open for further replies.

sagar2993

Newbie level 3
Newbie level 3
Joined
Jul 16, 2018
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
26
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

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, SEC1;
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,0x01);
   //  LCD_STRING("S");
}

void STOP_I2C()
{
    SDA=0;
    SCL=1;
    SCL=1;
     __delay_ms(20);
     SDA=1;
     
    // LCD_POS(1,0x08);
    // LCD_STRING("p");
      
}
void WRITE_I2C_DATA(char d)
{
    char i;
    SCL=0;
    SDA=0;
    
    __delay_ms(20);
    for(i=0;i<=7;i++)
    {
        if((d & 0x80) ==0x80)
        {
           
            SDA=1;
          //  LCD_POS(2,0x03);
            //LCD_STRING("P");
        }
        else
        {
            SDA=0;
           // LCD_POS(2,0x03);
          // LCD_STRING("Q");
        }
        
        d=d<<1;
        __delay_us(20);
        SCL=1;
        SCL=1;
         __delay_us(20);
         SCL=0;
         __delay_us(20);
         
    }    
         i=SDA;
        // LCD_POS(2,0x05);
      //   LCD_STRING("S");
         SCL=1;
         __delay_us(20);
         SCL=0;
         __delay_us(20);
        
     /*  for(i=0;i<=7;i++)
         {
        SDA=((d&0x80)?1:0);
        d=d<<1;
        LCD_POS(2,0x05);
         LCD_STRING("S");
        __delay_us(20);
        SCL=1;
        SCL=1;
        __delay_us(20);
        SCL=0;
        __delay_us(20);

        }
       i=SDA;
       LCD_POS(1,0x03);
           LCD_STRING("P");
       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(void)
{
    char i;
    char j;
    SADDR=1;
    SCL=0;
    j=0x00;
    
    for(i=0;i<=7;i++)
    {
       // LCD_POS(2,0x01);
      //  LCD_STRING("s1");
        
        j=j<<1;
        
      //  LCD_POS(2,0x01);
      //  LCD_DATA(j);
        
        SCL=1;
        j |=SDA;
        SCL=0;
        
    }
    
       // LCD_POS(2,0x04);
       // LCD_STRING("s2");
    SADDR=0;
    SDA=1;
    SCL=1;
    SCL=1;
    SCL=0;
    return j;
    
       // LCD_POS(2,0x07);
       // LCD_STRING("s3");
    
}
char READ_I2C(char daddr)
{
    char s1;
   
    START_I2C();
    WRITE_I2C_DATA(0xD0);// write
    //LCD_POS(2,0x07);
    // LCD_STRING("S3");
    WRITE_I2C_DATA(daddr);
   // WRITE_I2C_DATA(0x07);
  //  WRITE_I2C_DATA(0x10);
    
    START_I2C();
    WRITE_I2C_DATA(0xD1);// read
   
    s1= READ_I2C_DATA();
    
   // LCD_POS(2,0x01);
  //  LCD_DATA(s1);
  
   
    STOP_I2C();
    return s1;
}
/*
char  CONVERT(int fdata)
{
    fdata=(fdata>>4);
    return fdata;
}
char  CONVERT1(int gdata)
{
    gdata=(gdata>>4);
    return gdata;
}
 */
int main(void)
{  
  
  long int s=0;
  OSCCONbits.IRCF=0x7;
 // char a=0x12;
  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,0x01);
 __delay_us(20);
 LCD_CMD(0x80);
 LCD_STRING("SAGAR");
 LCD_CMD(0xE0);
 
 

 
  while(1)
  { 
     
   //   s++;
   
      SEC=READ_I2C(0x00);
      // SEC1=CONVERT(SEC);
       ///SEC1=CONVERT1(SEC);
     // LCD_POS(2,0x01);
   //   LCD_DATA(SEC);
      MIN=READ_I2C(0x01);
    //  LCD_POS(2,0x03);
   //   LCD_DATA(MIN);
      HOUR=READ_I2C(0x02);
     // LCD_POS(2,0x05);
     /// LCD_DATA(HOUR);
      DAY=READ_I2C(0x03);
     // LCD_POS(2,0x07);
     // LCD_DATA(DAY);
     /* DATE=READ_I2C(0x04);
      MONTH=READ_I2C(0x05);
      YEAR=READ_I2C(0x06);*/
     
      
      LCD_CLEAR();
      LCD_POS(1,0x01);
      
     // LCD_DATA(a);
      
   /*   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[i]);
      }
      */
       
     // HOUR=0x05;
   // LCD_DATA(HOUR);
      LCD_DATA(HOUR/0x10+0x30);
      LCD_DATA(HOUR%0x10+0x30);
      
     // __delay_ms(1);
      // LCD_DATA(MIN=0x10);
      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,0x01);
      
      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();

}
 
Last edited by a moderator:

Hi,

Show us your hardware.
Did you properly install the pull up resistors.
Isn't there an I2C periferal in the PIC?
It's urgent that you read the I2C specification with all necessary setup and hold times.
What SCL frequency do you expect?
Use a scope to verify the signals, show us scope pictures. (I expect timing problems)
Don't test a whole software at once. Test it piece by piece.
What pieces of code do work properly and what pieces do not work as expected.

I2C:
What you need to verify at first: does the slave respond with an ACK after addressing?

There are many, many threads about PIC and I2C.
There are many threads about DS1307.
Go through them.

Klaus
 

If the code is working in the simulation environment, you could at least clean the code, as there are unreadable snippets due to the exagerated number of commented lines. By the way, the delay that you inserted after each change of state of the output pins, although in my opinion is too high, in some parts of the code you did not implement, which may be affecting the operation in the actual circuit, such as for example in the START_I2C() and STOP_I2C() functions in which you forgot to insert it after the last line of each of these functions; there may have more cases like this, so check it.
 

SDA is basically not operated according to I2C spec. SDA should be only pulled high by external pull-up resistor, never pulled actively as done in the code.

Your bit bang I2C implementation is using the same pins as 16F690 hardware I2C interface, so you can easily switch to hardware controller.
 

Hint: change SDA so it controls the TRIS bits instead of the PORT bits. Then make the SDA port bit = 0. That way, SDA can be pulled low (TRIS = 0) or will be pulled high externally (TRIS = 1).

Brian.
 

Rtc-ds1307-PIC16F690

i need c code for rtc ds1307 for pic16f690
 

Re: Rtc-ds1307-PIC16F690

Hi,

(Merged your new thread with the existing thread)

If you want someone to write code for you, then you need to pay him.
But this forum is not meant that others do your job, but you write the code and we are here to help you to make it running.

If you want code you need to tell it's function, the expected language and the used compiler.
(I call it laziness not to give this essential information)

Because there already are many threads here in this forum about PIC and DS130x communication, I must assume you did not do a single forum search. There are even threads with code - maybe the code needs to be adjusted for your needs which you need to do on your own.
The same applies to informations in the internet...
(Again I have to call it laziness)

--> Do your job, show your effort, show what you have done so far...then you will get plenty of assistance.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top