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.

RTS ds1307 how to read and write the data by using i2c

Status
Not open for further replies.

jaya krishna

Member level 1
Joined
May 10, 2012
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
India,coimbatore
Activity points
1,770
pls any body tell how to write and read the data by using I2C and how to assign the time date for time keeper register(DS1307).i don't understand the program any body help me.... thanks......


include "16F877A.h"
#use delay(clock=4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "ds1307.c"

void main(void)
{
int8 sec,min,hour,day,date,month,year;

delay_ms(50);
init_ds1307(); // initial DS1307
sec=read_ds1307(0);
write_ds1307(0,sec & 0x7F); // enable oscillator(bit 7 =0) [not understand 0,sec&0x7f]


while(true)
{
sec=read_ds1307(0); // read second
min=read_ds1307(1); // read minute
hour=read_ds1307(2); // read hour
day=read_ds1307(3); // read day
date=read_ds1307(4); // read date
month=read_ds1307(5); // read month
year=read_ds1307(6); // read year
putc(0x0c);
printf("Time : %X:%X:%X\r\n",hour,min,sec);
printf("Day : %02X\r\n",day);
printf("Date : %X/%X/%X\r\n",date,month,year);
delay_ms(500);
}
}




#define DS1307_SDA PIN_C4
#define DS1307_SCL PIN_C3
#use i2c(master, sda=DS1307_SDA, scl=DS1307_SCL)




//==========================
// initial DS1307
//==========================
void init_DS1307()
{
output_float(DS1307_SCL);
output_float(DS1307_SDA);
}
//==========================
// write data one byte to
// DS1307
//==========================
void write_DS1307(byte address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xd0); [not uderstand,0xd0 its constant for all i2c? ]
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xd0);
while(status==1)
{
i2c_start();
status=i2c_write(0xd0);
}
}
//==========================
// read data one byte from DS1307
//==========================
BYTE read_DS1307(byte address)
{
BYTE data;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0);
i2c_stop();
return(data);
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top