revamp
Newbie level 1
hi,
I am trying to interface ds1307 with p89v51rd2. I am using keil compiler. I have interfaced at2402(I2C eeprom) earlier, i tried to interface rtc using the eeprom code, but its not working. this is the code i used
the delay header
the main is here
i dont have any errors, only one warning for not calling a function
this is the code i used for eeprom and its working fine.
Jai Hind
I am trying to interface ds1307 with p89v51rd2. I am using keil compiler. I have interfaced at2402(I2C eeprom) earlier, i tried to interface rtc using the eeprom code, but its not working. this is the code i used
Code:
#ifndef __vrtc_h__
#define __vrtc_h__
#include<reg51.h>
#include<Vdelay.h>
sbit sda=P1^0;
sbit scl=P1^1;
void start_bit()
{
sda=1;
scl=1;
sda=0;
scl=0;
}
void stop_bit()
{
scl=0;
sda=0;
scl=1;
sda=1;
}
void write_mem(unsigned char m )
{
unsigned char i;
unsigned char b;
b=m;
for(i=0;i<8;i++)
{
b=(b & 0x80);
if(b!=0)
sda=1;
else
sda=0;
b=m<<1;
m=b;
scl=1;
scl=0;
}
scl=1;
scl=0;
}
unsigned char read_mem()
{
unsigned char i,a1,b1=0;
sda=1;
for(i=0;i<8;i++)
{
scl=0;
delay();
scl=1;
delay();
if(sda==1)
b1=(b1 | 0x01);
a1=b1;
b1=b1<<1;
}
scl=1;
scl=0;
// val = a1;
return a1;
}
unsigned char read_eprom(unsigned char addr)
{
unsigned char a1=0xd0,a2=0xd1,dat;
start_bit();
write_mem(a1);
write_mem(addr);
start_bit();
write_mem(a2);
dat = read_mem();
stop_bit();
delay1();
return dat;
}
void write_eprom(unsigned char addr,unsigned char b)
{
unsigned char ad=0xd0;
start_bit();
write_mem(ad);
write_mem(addr);
write_mem(b);
stop_bit();
delay1();
}
#endif
Code:
#ifndef __Vdelay_H__
#define __Vdelay_H__
void delay_ms(int a)
{
int i;
while(a)
for(i = 0;i < 36;i++)//25micro * 40 = 1 ms,but due to overhead of for loop,we use 36
{
TMOD = 0x02;//timer 0,mode 2
TH0 = -23;//for getting 25 micro seconds
TR0 = 1;
TF0 = 0;
while(!TF0);
TR0 = 0;
TF0 = 0;
}
}
void delay1()
{
int x=900;
while(x--);
}
void delay()
{ int x=40;
while(x--);
}
#endif
Code:
//#include<reg51.h>
#include<vrtc.h>
void main()
{
// int j;
unsigned char i;
P0 = 0;
P1 = 0;
// dat = 0x1e;
for(i = 0;i < 10;i++)
write_eprom(i,i+10);
P0 = read_eprom(3);
/* {
for(j=0;j<20000;j++);
} */
while(1);
}
this is the code i used for eeprom and its working fine.
Jai Hind