Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interface of Pic18f8722 with RTC DS1307

Status
Not open for further replies.

engineerfaisal

New Member
//************************************************************************************//
//************************************************************************************//
// Project: //
// Header: 8722 Project //
// PIC used: PIC18F8722 //
// Start Date: 1st Dec 2009 //
// Function for I2C2 //
// the interface of DS1307 with Pic18f8722 //
// Backup power by lithium battery(3.3v) //
// Microcontroller is in master mode, I2c has been designed for 100Khz, //
// I have connected pullup resistors(2.2 Kohm Each) with the SDA and SCl lines, //
// 32Khz Crystal oscillator for DS1307 //
//************************************************************************************//
//************************************************************************************//
//** HEADER FILES ********************************************************************/
#include <p18F8722.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <usart.h>
#include <timers.h>
#include <delays.h>
#include <i2c.h>
//** CONFIGURATION BITS **************************************************************/
#pragma config OSC = HSPLL
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = ON
//** FUNCTION PROTOTYPES *************************************************************/
void Init_Ports(void);
void Configure_I2C2(void);
void Day_Decision (void);
//** DEFINITIONS *********************************************************************/
#define ControlByte 0xD0 //Control is always D0 for reads (& D1 for writes)
#define CLEAR 0
#define SET 1
#define BRGValue 0x63
//** GLOBALS *************************************************************************/
unsigned char Second =0x00, Minutes=0x59, Hours=0x23, Days=0x7, Date=0x31, Month=0x12, Year=0x09;
unsigned char Day,Sec,Min,Hr,Dy,Dat,Mon,Ye;
unsigned char Monday,Tuesday,Wenesday,Thursday,Friday,Saturday,Sunday;
//** MAIN CODE ***********************************************************************/
void main(void)
{
Init_Ports();
Configure_I2C2 ();
EEByteWrite2( ControlByte, 0x07 , 0x00); // control byte 0x00;
EEByteWrite2( ControlByte, 0x00 , Second);
EEByteWrite2( ControlByte, 0x01 , Minutes);
EEByteWrite2( ControlByte, 0x02 , Hours);
EEByteWrite2( ControlByte, 0x03 , Days);
EEByteWrite2( ControlByte, 0x04 , Date);
EEByteWrite2( ControlByte, 0x05 , Month);
EEByteWrite2( ControlByte, 0x06 , Year);

while(SET)
{
Sec = EERandomRead2( 0xD0, 0x00 );
Min = EERandomRead2( 0xD0, 0x01 );
Hr = EERandomRead2( 0xD0, 0x02 );
Days= EERandomRead2( 0xD0, 0x03 );
Dat= EERandomRead2( 0xD0, 0x04 );
Mon = EERandomRead2( 0xD0, 0x05 );
Ye= EERandomRead2( 0xD0, 0x06 );
Day_Decision ();
}
}
//*************************************************************************************
void Init_Ports(void)
{
PORTD = CLEAR;
LATD = 0b01100001;
TRISD = 0b01100001; // intialized RD6(SCL2) AND RD5(SDA2).
}
//*************************************************************************************
void Configure_I2C2 (void)
{
SSP2STAT = 0x80; //Disable SMBus &
SSP2CON1 = 0x28; //Enable MSSP Master
SSP2CON2 = 0x00; //Clear MSSP Conrol Bits
SSP2ADD = 0x63;
}
///********************************************************************
* Function Name: WriteI2C2 *
* Return Value: Status byte for WCOL detection. *
* Parameters: Single data byte for I2C2 bus. *
* Description: This routine writes a single byte to the *
* I2C2 bus. *
********************************************************************/
unsigned char WriteI2C2( unsigned char data_out )
{
SSP2BUF = data_out; // write single byte to SSP2BUF
if ( SSP2CON1bits.WCOL ) // test if write collision occurred
return ( -1 ); // if WCOL bit is set return negative #
else
{
while( SSP2STATbits.BF ); // wait until write cycle is complete
return ( 0 ); // if WCOL bit is not set return non-negative #
}
}
///*********************************************************************
* Function Name: EERandomRead2 *
* Return Value: error condition status and/or data byte *
* Parameters: EE memory control byte with R/W set to 1 *
* Description: Reads 1 byte from passed address to EE memory*
* device. This routine can be used for any I2C *
* EE memory device, which only uses 1 byte of *
* address data as in the 24LC01B/02B/04B/08B. *
* *
*********************************************************************/
unsigned int EERandomRead2( unsigned char control, unsigned char address )
{
IdleI2C2(); // ensure module is idle
StartI2C2(); // initiate START condition
while ( SSP2CON2bits.SEN ); // wait until start condition is over
if ( PIR3bits.BCL2IF ) // test for bus collision
{
return ( -1 ); // return with Bus Collision error
}
else
{
if ( WriteI2C2( control ) ) // write 1 byte
{
return ( -3 ); // return with write collision error
}

IdleI2C2(); // ensure module is idle
if ( !SSP2CON2bits.ACKSTAT ) // test for ACK condition, if received
{
if ( WriteI2C2( address ) ) // WRITE word address for EEPROM
{
return ( -3 ); // return with write collision error
}

IdleI2C2(); // ensure module is idle
if ( !SSP2CON2bits.ACKSTAT ) // test for ACK condition, if received
{
RestartI2C2(); // generate I2C bus restart condition
while ( SSP2CON2bits.RSEN );// wait until re-start condition is over
if ( PIR3bits.BCL2IF ) // test for bus collision
{
return ( -1 ); // return with Bus Collision error
}

if ( WriteI2C2( control+1 ))// write 1 byte - R/W bit should be 1
{
return ( -3 ); // return with write collision error
}

IdleI2C2(); // ensure module is idle
if ( !SSP2CON2bits.ACKSTAT )// test for ACK condition, if received
{
SSP2CON2bits.RCEN = 1; // enable master for 1 byte reception
while ( SSP2CON2bits.RCEN ); // check that receive sequence is over
NotAckI2C2(); // send ACK condition
while ( SSP2CON2bits.ACKEN ); // wait until ACK sequence is over
StopI2C2(); // send STOP condition
while ( SSP2CON2bits.PEN ); // wait until stop condition is over
if ( PIR3bits.BCL2IF ) // test for bus collision
{
return ( -1 ); // return with Bus Collision error
}
}
else
{
return ( -2 ); // return with Not Ack error
}

}
else
{
return ( -2 ); // return with Not Ack error
}
}
else
{
return ( -2 ); // return with Not Ack error
}
}
return ( (unsigned int) SSP2BUF ); // return with data
}
//************************************************************************************
void Day_Decision (void)
{
if (Days ==1) {Dy = Monday;}
if (Days ==2) {Dy = Tuesday;}
if (Days ==3) {Dy = Wenesday;}
if (Days ==4) {Dy = Thursday;}
if (Days ==5) {Dy = Friday;}
if (Days ==6) {Dy = Saturday;}
if (Days ==7) {Dy = Sunday;}
}
 

Attachments

  • RTC Schematic.JPG
    RTC Schematic.JPG
    43.3 KB · Views: 403
Status
Not open for further replies.

Latest threads

Top