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.

[SOLVED] ds1307 problem using 89v51rd2

Status
Not open for further replies.

Paritoshgiri

Member level 5
Joined
Mar 7, 2010
Messages
83
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Location
Kathmandu
Activity points
1,872
Here i used 89v51rd2 to read and write in ds1307. However i need to modify it so that the user can send inital date and time and the ds1307 starts its clock from the user assigned state. Can anyone plz help me by sending similar code or give me any idea how to modify this code?

#include <P89V51Rx2.H>
#include<stdio.h>
#include "I2C.h"

//sbit SDA = P1^3;
//sbit SCL = P1^2;

char putchar(char c) {
SBUF = c;
while(TI == 0);
TI = 0;
return c;
}

void initserial(void) {
SCON=0X50;
TMOD=0X20;
TH1=0Xfa;
TL1=0X00;
TR1=1;
}

void delay(unsigned int i) {
unsigned int j;

for(j=0;j<i;j++);
}

void rtc_write(unsigned char addr, unsigned char val)
{
I2C_start();

I2C_write(0xD0); // Connect to RTC
I2C_write(addr); // Request RAM address (Low byte)

I2C_write(val); // Write sec on RAM specified address

I2C_stop(); // Stop i2c bus
}

unsigned char rtc_read(unsigned char addr)
{
unsigned char dat;
I2C_start(); // Start i2c bus

I2C_write(0xD0); // Connect to RTC
I2C_write(addr); // Request RAM address (Low byte)

I2C_start(); // Start i2c bus

I2C_write(0xD1); // Connect to RTC for Read
dat = I2C_read(); // Receive data

I2C_noack();

I2C_stop(); // Stop i2c bus

return dat;
}


void main() {
unsigned char Second, Minutes, Hours, Day, Date, Month, Year;
initserial();
printf("Initialization");
//rtc_write(0x07,0x00); //Control register
rtc_write(0x00,0x05); //Control register for second
rtc_write(0x01,0x07); //Control register for minute
rtc_write(0x02,0x06); //Control register for hour

rtc_write(0x03,0x02); //Control register for day

rtc_write(0x04,0x03); //Control register for day
rtc_write(0x05,0x02); //Control register for month
rtc_write(0x06,0x04); //Control register for year

delay(0xFFFF);

Second = rtc_read(0x00);
Minutes = rtc_read(0x01);
Hours = rtc_read(0x02);

Day = rtc_read(0x03);

Date = rtc_read(0x04);
Month = rtc_read(0x05);
Year = rtc_read(0x06);
I2C_stop();
delay(0xFFFF);

printf("Second = %x",Second);
printf("Minutes = %x",Minutes);
printf("Hours = %x",Hours);
printf("Day = %x", Day);
printf("Date = %x", Date);
printf("Month = %x", Month);
printf("Year = %x", Year);
while(1);
}
 

For getting user values, you should have some keys(Buttons).

For example you can assign two buttons, one for increment and other for decrement. And another button to select the hours and minutes
 

For getting user values, you should have some keys(Buttons).

For example you can assign two buttons, one for increment and other for decrement. And another button to select the hours and minutes

So I have to make two functions for increment and decrement? Is it that in ds1307 the value of date and time increases automatically? Can you provide me with a code in C plz?
 

So I have to make two functions for increment and decrement? Is it that in ds1307 the value of date and time increases automatically? Can you provide me with a code in C plz?

ds1307 values dont change by themselves, you have write the values once the setting is completed.
 

#include<reg51.h>
#include<intrins.h>

sbit rs=P0^0;
sbit en=P0^1;

sbit sda=P3^6;
sbit scl=P3^7;

unsigned char rtc_adr[8]={0x00,0x01,0x02,0x03,0x04,0x05,0x06};
unsigned char rtc_dat[8]={0x20,0x58,0x23,0x05,0x31,0x12,0x03};
unsigned char dis[7]={0x20,0x20,0x28,0x08,0x0c,0x06,0x80};
unsigned char a,b,c,d;

void strt_bt();
void stp_bt();
void dta_wr();
void dta_rd();
void cmdwrt();
void datawrt();
void delay();
void rtc_wr();
void rtc_rd();
void display();

main()
{
int i;
for (i=0;i<7;i++)
{
a=dis;
cmdwrt();
delay();
}

P0=0x00;

for(i=0;i<8;i++)
{
b=rtc_adr;
c=rtc_dat;
rtc_wr();
}

while(1)
{
a=0x80;
cmdwrt();
delay();

b=0x04; //date
rtc_rd();
display();

a='/';
datawrt();
delay();

b=0x05; //month
rtc_rd();
display();

a='/';
datawrt();
delay();

b=0x06; //yr
rtc_rd();
display();

a='-';
datawrt();
delay();

b=0x03; //week
rtc_rd();
display();

a=0xc0;
cmdwrt();
delay();

b=0x02; //hr
rtc_rd();
display();

a=':';
datawrt();
delay();

b=0x01; //min
rtc_rd();
display();

a=':';
datawrt();
delay();

b=0x00; //sec
rtc_rd();
display();
}

}

void rtc_wr()
{
strt_bt(); //write

a=0xd0;
dta_wr();

a=b;
dta_wr();

a=c;
dta_wr();

stp_bt();
delay();
}

void rtc_rd()
{
strt_bt(); //dummy write

a=0xd0;
dta_wr();

a=b;
dta_wr();

strt_bt(); //read

a=0xd1;
dta_wr();

dta_rd();


/* sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();

scl=0;
*/
stp_bt();
delay();
}

void strt_bt()
{
sda=1;
scl=1;
_nop_();
_nop_();
sda=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
}

void stp_bt()
{
//scl=0;
sda=0;
scl=1;
_nop_();
_nop_();
//_nop_();
//_nop_();
//_nop_();
sda=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
_nop_();
_nop_();
_nop_();
_nop_();
scl=1;
}

void dta_wr()
{
int j;
char m;
m=a;
sda=1;
for(j=0;j<8;j++)
{
a=m;
a=a & 0x80;
if(a==(0x80))
sda=1;
else
sda=0;
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();
scl=0;
m=m<<1;
}

sda=1;
scl=1;
_nop_();
_nop_();
_nop_();
_nop_();

scl=0;
delay();
}

void dta_rd()
{
int j;
int c=0,b;
sda=1;
for(j=0;j<8;j++)
{
a=a<<1;
scl=1;
c=sda;
if(c)
b=0x01;
else
b=0x00;
scl=0;
a=a|b;
}

sda=1;
//_nop_();
//_nop_();
//_nop_();
//_nop_();
scl=1;
_nop_();
_nop_();
_nop_();
scl=0;
//datawrt();
delay();
}

void display()
{
d=a;
a=(a>>4);
a=a & 0x0f;
a=a + 0x30;
datawrt();
delay();

a=d;
a=a & 0x0f;
a=a + 0x30;
datawrt();
delay();
}


void cmdwrt()
{
unsigned char s=0;
s=a;
a=a & 0xf0;
a=a>>2;
rs=0;
P0=a;
rs=0;
en=1;
en=0;
a=s;
a=(a<<4);
a=a & 0xf0;
a=a>>2;
rs=0;
P0=a;
rs=0;
en=1;
en=0;
delay();
}


void datawrt()
{
unsigned char s=0;
s=a;
a=a & 0xf0;
a=a>>2;
rs=1;
//mdata();
//delay();
P0=a;
rs=1;
en=1;
en=0;
a=s;
a=a<<4;
a=a & 0xf0;
a=a>>2;
rs=1;
//mdata();
P0=a;
rs=1;
en=1;
en=0;
delay();
}


void delay()
{
int i;
for(i=0;i<1000;i++);
}
 
I was able to successfully send date and time to rtc and rtc's clock is running without any problem. The running code is below but now I've to change this code so that I can take a start time from the terminal in the format hh:mm:ss by serial communication and then send it to rtc to set up the time. So can u guys help me in receiving the time hh:mm:ss using ISR and then separating the hour, minute and second from hh:mm:ss so that can i send hour, minute and second individually to rtc as in my code below:
#include <P89V51Rx2.H>
#include<stdio.h>
#include "i2c.h"

code unsigned char Day_Str[7][10] = {{"Sun"},
{"Mon"},
{"Tue"},
{"Wed"},
{"Thu"},
{"Fri"},
{"Sat"}};

code unsigned char Month_Str[12][10] = {{"Jan"},
{"Feb"},
{"Mar"},
{"Apr"},
{"May"},
{"Jun"},
{"Jul"},
{"Aug"},
{"Sep"},
{"Oct"},
{"Nov"},
{"Dec"}};

char putchar(char c) {
SBUF = c;
while(TI == 0);
TI = 0;
return c;
}

void initserial(void) {
SCON=0X50;
TMOD=0X20;
TH1=0Xfa;
TL1=0X00;
TR1=1;
}

void delay(unsigned int i) {
unsigned int j;

for(j=0;j<i;j++);
}

unsigned char rtc_realRd(unsigned char addr) {
unsigned char dat;
I2C_start();
I2C_write(0xD0); //Device Addr for RTC with write oper
I2C_write(addr);

I2C_start();
I2C_write(0xD1); //Device Addr for RTC with Read oper
dat = I2C_read();
delay(20);
I2C_noack();
I2C_stop();
return dat;
}

void rtc_realWr(unsigned char addr, unsigned char val) {

I2C_start();
I2C_write(0xD0);
I2C_write(addr);
I2C_write(val);
delay(20);
I2C_stop();
}

unsigned char BCD2HEX(unsigned char bcd) //Convert BCD 1 byte to HEX 1 byte
{
unsigned char temp;
temp=((bcd>>4)*10)+(bcd&0x0f);
return temp;
}

char * Int2Day(unsigned char day) //Convert BCD to string for day
{
return &Day_Str[BCD2HEX(day)-1][0];
}

char * Int2Month(unsigned char month) //Convert BCD to string for month
{
return &Month_Str[BCD2HEX(month)-1][0];
}


void main() {
unsigned char Second, Minutes, Hours, Day, Date, Month, Year;
initserial();
printf("Initialization\r");
//rtc_realWr(0x07,0x00); //Control register
rtc_realWr(0x00,0x05); //second = 5
delay(20);
rtc_realWr(0x01,0x12); //minute = 12
delay(20);
rtc_realWr(0x02,0x08); //hour = 8
delay(20);

rtc_realWr(0x03,0x02); //day = 2
delay(20);

rtc_realWr(0x04,0x03); //date = 3
delay(20);
rtc_realWr(0x05,0x06); //month = 06
delay(20);
rtc_realWr(0x06,0x10); //year = 10

delay(0xFFFF);

while(1) {
Second = rtc_realRd(0x00);
Minutes = rtc_realRd(0x01);
Hours = rtc_realRd(0x02);

Day = rtc_realRd(0x03);

Date = rtc_realRd(0x04);
Month = rtc_realRd(0x05);
Year = rtc_realRd(0x06);

delay(0xFFFF);
//printf("%X %X %X %X %X %X %X\r\n\r\n",(int)Second,(int)Minutes,(int)Hours,(int)Day,(int)Date,(int)Month,(int)Year);
printf("Day : %s\r\n",Int2Day(Day));
printf("Time : %02d:%02d:%02d\r\n",(int)BCD2HEX(Hours),(int)BCD2HEX(Minutes),(int)BCD2HEX(Second));
printf("Date : %d-%s-20%02d\r\r",(int)BCD2HEX(Date),Int2Month(Month),(int)BCD2HEX(Year));
delay(0xFFFF); // delay
}
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top