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.

Problem with ds1307 interface with pic16f877a

Status
Not open for further replies.

rangerskm

Full Member level 4
Joined
Jan 23, 2013
Messages
199
Helped
0
Reputation
2
Reaction score
0
Trophy points
1,296
Activity points
2,663
I AM NEW TO INTERFACE OF DS1307 WITH PIC.
can any provide the links for these interface and what is iic protocol ??
 

it's very easy if you are using MIKROC compiler.
 

Problem in rtc interface with pic16f877a and communicate to pc

COMPILED USING MPLAB IDE HI TECH C COMPILER
i am having little trouble with the rtc interface .i simulated in isis i got the right time in it.when i implemented in hardware.i got some unknown characters in PC when taken the real time values from controller to the PC terminal using UART.


Code:
#include<pic.h>       //Define PIC Registers

#include<stdio.h>                  

 

__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[i]);                          

   printf("\033[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++);

   }

}

check the code and say me solution for viewing the correct time in terminal and also please help me to modify the code to display the time in LCD (2*16)DISPLAY
 

Re: Problem in rtc interface with pic16f877a and communicate to pc

whats the correct baud rate for this .where it can be edited in the program ??
 

Re: Problem in rtc interface with pic16f877a and communicate to pc

why did u mention baudrate as 9.6 ?


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

You have mentioned both pins as input ( if they are connected to UART ) one should be for RX and One for Tx
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top