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.

How to write data ds1307 for 8051

Status
Not open for further replies.
Code:
void main ()
{
    InitI2c();
    StartI2c();
    ACK = write_i2c(unsigned char DS1307_SECONDS);;
}
After start, you'll send the device address first.

No type cast needed for DS1307_SECONDS. The correct type cast syntax would be
Code:
write_i2c((unsigned char) DS1307_SECONDS);

write_i2c() might need a delay to keep I2C timing, depending on the processor speed.
 

After start, you'll send the device address first.

write_i2c() might need a delay to keep I2C timing, depending on the processor speed.

It would something this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <reg51.h>
 
//Define the Pin for the I2c 
sbit SDA_BUS = P2^0;
sbit SCL_BUS = P2^1;
 
#define I2C_DELAY    50         //Delay for I2c
#define DS1307_ADDRESS  0xD0    //  the device address
#define DS1307_SECONDS  0x00    //  DS1307 register addresses
 
void InitI2c(void);
void StartI2c(void);
void RepeatedStartI2c(void);
void StopI2c(void);
void delay(unsigned int);
unsigned char write_i2c(unsigned char);
 
/* This function provide the delay which is used in clock generation.*/
void delay(unsigned int d)
{
    unsigned int i;
    for(i=0; i<d; i++);
}
 
/*This function  use to make the data line and clock line idle to put the both line high
*/
void InitI2c(void)
{
    SDA_BUS =1;
    SCL_BUS =1;
}
 
/*This function performs the start operation to initiate the communication.
*/
void StartI2c(void)
{
    SDA_BUS  = 1;
    SCL_BUS  = 1;
    delay(I2C_DELAY);
    SDA_BUS  = 0;
    delay(I2C_DELAY);
}
 
/*When master want to stop the communication then it will assert the stop condition to the i2c bus.*/
 
void StopI2c(void)
{
    SCL_BUS  = 0;
    delay(I2C_DELAY/2);
    SDA_BUS  = 0;
    delay(I2C_DELAY/2);
    SCL_BUS  = 1;
    delay(I2C_DELAY/2);
    SDA_BUS  = 1;
    delay(I2C_DELAY);
}
 
/*When master does not want to relaese the control from the bus then it assert the repeated
start condition on the i2c bus*/
 
void RepeatedStartI2c()
{
    SCL_BUS  = 0;
    delay(I2C_DELAY/2);
    SDA_BUS  = 1;
    delay(I2C_DELAY/2);
    SCL_BUS  = 1;
    delay(I2C_DELAY/2);
    SDA_BUS  = 0;
    delay(I2C_DELAY);
}
 
//send a byte and get ack 
unsigned char write_i2c(unsigned char byte);
{
     unsigned char i, ack_bit;
    
     for (i = 0; i < 8; i++) {
        if ((byte & 0x80) == 0)
            SDA_BUS = 0;
        else
            SDA_BUS = 1;
        SCL_BUS = 1;
        SCL_BUS = 0;
        byte<<=1;
     }
 
     SDA_BUS = 1;
     SCL_BUS = 1;
     ack_bit = SDA_BUS;
     SCL_BUS = 0;
 
     return ack_bit;
}
void main ()
{
    char Time_Seconds = 0x00;
    InitI2c();
    StartI2c();
 
    ACK = write_i2c(unsigned char DS1307_ADDRESS );
    ACK = write_i2c(unsigned char DS1307_SECONDS);;
}



i want to send some value to DS1307_SECONDS

char Time_Seconds = 0x00;

Now how to send this value?
 
Last edited:

Hi,

An edaboard forum search for "DS1307 8051" gives about 100 hits. Many thousands if you do an internet search.
May I ask why you don't refer to one of the available codes?
It's how a forum is meant to work: One starts a thread with a problem ... many others gain from the information.

I see nothing in your thread that isn't already discussed in the existing threads.

Klaus
 

i want to send some value to DS1307_SECONDS

char Time_Seconds = 0x00;

Now how to send this value?
I believe, the question has been already answered along this thread, e.g. in post #14.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
StartI2c();
ACK = write_i2c(DS1307_ADDRESS );
if (ACK == 1)
{
  StopI2C();
  ...
}
else
{
  ACK = write_i2c(DS1307_SECONDS);
  ACK = write_i2c(Time_Seconds);


}
 

Hi,

An edaboard forum search for "DS1307 8051" gives about 100 hits. Many thousands if you do an internet search.
May I ask why you don't refer to one of the available codes?
It's how a forum is meant to work: One starts a thread with a problem ... many others gain from the information.

I see nothing in your thread that isn't already discussed in the existing threads.

Klaus

It's not that i haven't seen the code I have seen, but I do not understand the whole code. I though that I would have to go one step at a time to get to my destination

- - - Updated - - -

I believe, the question has been already answered along this thread, e.g. in post #14.

}

No you misunderstood

DS1307_SECONDS 0x00 //address

char Time_Seconds = 0x00; // value

I want to store 0x00 value at DS1307_SECONDS register
 

No you misunderstood
Not at all. The code does what you want.

Plenty of explanation now. I think it's time to start coding and debugging.
 

Hi,

No you misunderstood

DS1307_SECONDS 0x00 //address

char Time_Seconds = 0x00; // value

I want to store 0x00 value at DS1307_SECONDS register
...this exactly is what's written in post#14 ... or in figure6 of the datasheet.

It's not that we misunderstand you....

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top