Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

DS1307 interface with PIC18F4520 using C18

Status
Not open for further replies.

vijayakumar1981

Newbie level 1
Joined
Dec 8, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,310
Hi Friends,

Iam trying to interface a DS1307 with PIC18F4520. I initialized the RTC sucessfully but The porblem is while reading it suddenly stops working. here with i attached the code . Iam using C18 compiler.

#include <p18f4520.h>
#include <i2c.h>
#include <stdio.h>
//#define DS1307

void initialize()
{
unsigned char sec, min, hr, mon, year, date, day;

StartI2C();
IdleI2C();

if (!DataRdyI2C())
WriteI2C(0b11010000); //address of DS1307.
IdleI2C();
if (!DataRdyI2C())
WriteI2C(0x00); // Position the address pointer to 0.
IdleI2C();
if (!DataRdyI2C())
WriteI2C(0x00); // Clear seconds register and CH bit.
IdleI2C();

StopI2C();
//**************** Date and Time ********************//
hr = 0b01100010; // Set to 12 hour format.
min = 0b00110101;
sec = 0b00000000; // Oscillator enabled.
day = 0b00000001; // Set 1 = Sunday.
date= 0b00011001; // Date set to 1st.
mon = 0b00010001; // Month set to January.
year= 0b00010010; // Year set to 2008.
//**************** Transmit Time and Date ********************//

StartI2C();
IdleI2C();

if (!DataRdyI2C())
WriteI2C(0b11010000); //address of DS1307.
IdleI2C();

if (!DataRdyI2C())
WriteI2C(0x00); // Position the address pointer to 0.
IdleI2C();

if (!DataRdyI2C())
WriteI2C(sec);
IdleI2C();

if (!DataRdyI2C())
WriteI2C(min);
IdleI2C();

if (!DataRdyI2C())
WriteI2C(hr);
IdleI2C();

if (!DataRdyI2C())
WriteI2C(day);
IdleI2C();
if (!DataRdyI2C())
WriteI2C(date);
IdleI2C();
if (!DataRdyI2C())
WriteI2C(mon);
IdleI2C();
if (!DataRdyI2C())
WriteI2C(year);
IdleI2C();
if (!DataRdyI2C())
WriteI2C(0x10); // Sets SQWE and selects 1Hz freq of Sqr Wave output.
IdleI2C();
StopI2C();
}
void read()
{
unsigned char Sec, Min, Hrs, Mon, Year, Date, Day;

StartI2C();
IdleI2C();

if (!DataRdyI2C())
WriteI2C(0b11010000); //address of DS1307.
IdleI2C();

if (!DataRdyI2C())
WriteI2C(0x00); // Position the address pointer to 0.
IdleI2C();
StartI2C();
IdleI2C();

if (!DataRdyI2C())
WriteI2C(0b11010000 | 1); // Direction bit set to read.
IdleI2C();
//*********************** Reception *********************//
if (DataRdyI2C())
Sec = ReadI2C();
IdleI2C();
AckI2C();
IdleI2C();
if (DataRdyI2C())
Min = ReadI2C();
IdleI2C();
AckI2C();
IdleI2C();
if (DataRdyI2C())
Hrs = ReadI2C();
IdleI2C();
AckI2C();
IdleI2C();
if (DataRdyI2C())
Day = ReadI2C();
IdleI2C();
AckI2C();
IdleI2C();
if (DataRdyI2C())
Date = ReadI2C();
IdleI2C();
AckI2C();
IdleI2C();
if (DataRdyI2C())
Mon = ReadI2C();
IdleI2C();
AckI2C();
IdleI2C();
if (DataRdyI2C())
Year = ReadI2C();
IdleI2C();
NotAckI2C();
IdleI2C();
StopI2C();
}
void bcdtoascii(unsigned char myvalue)
{
unsigned char highvalue,lowvalue;
unsigned char tmp = myvalue;
tmp = tmp & 0xF0;
tmp = tmp >> 4;
tmp = tmp | 0x30;
highvalue = tmp;
tmp = myvalue;
tmp = tmp & 0x0F;
tmp = tmp | 0x30;
lowvalue = tmp;
}
void main (void)
{
unsigned int i;
unsigned char Sec, Min, Hrs, Mon, Year, Date, Day;

//DDRBbits.RB0 = 1;
//DDRBbits.RB1 = 1;
TRISCbits.RC3 = 1;
TRISCbits.RC4 = 1;

OpenI2C(MASTER, SLEW_OFF);
SSPADD = 39;

initialize();

//
while(1)
{
Delay10KTCYx(100);

read();
bcdtoascii(Sec);
bcdtoascii(Min);
bcdtoascii(Hrs);
bcdtoascii(Mon);
bcdtoascii(Year);
bcdtoascii(Date);
bcdtoascii(Day);
}
//CloseI2C();
//return;
//while(1);


}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top