+ Post New Thread
Page 2 of 2 FirstFirst 1 2
Results 21 to 27 of 27
  1. #21
    Super Moderator
    Points: 264,129, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,155
    Helped
    14036 / 14036
    Points
    264,129
    Level
    100

    Re: How to write data ds1307 for 8051

    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.



    •   AltAdvertisement

        
       

  2. #22
    Member level 1
    Points: 522, Level: 4

    Join Date
    Feb 2018
    Posts
    35
    Helped
    0 / 0
    Points
    522
    Level
    4

    Re: How to write data ds1307 for 8051

    Quote Originally Posted by FvM View Post

    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 by ansh11; 22nd December 2019 at 15:21.



    •   AltAdvertisement

        
       

  3. #23
    Super Moderator
    Points: 81,029, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,431
    Helped
    3730 / 3730
    Points
    81,029
    Level
    69

    Re: How to write data ds1307 for 8051

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



    •   AltAdvertisement

        
       

  4. #24
    Super Moderator
    Points: 264,129, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,155
    Helped
    14036 / 14036
    Points
    264,129
    Level
    100

    Re: How to write data ds1307 for 8051

    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);
    }



  5. #25
    Member level 1
    Points: 522, Level: 4

    Join Date
    Feb 2018
    Posts
    35
    Helped
    0 / 0
    Points
    522
    Level
    4

    Re: How to write data ds1307 for 8051

    Quote Originally Posted by KlausST View Post
    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 - - -

    Quote Originally Posted by FvM View Post
    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



  6. #26
    Super Moderator
    Points: 264,129, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,155
    Helped
    14036 / 14036
    Points
    264,129
    Level
    100

    Re: How to write data ds1307 for 8051

    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.



    •   AltAdvertisement

        
       

  7. #27
    Super Moderator
    Points: 81,029, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,431
    Helped
    3730 / 3730
    Points
    81,029
    Level
    69

    Re: How to write data ds1307 for 8051

    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
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



--[[ ]]--