1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

8051 with real time clock

Discussion in '8051/8951' started by Parth86, Apr 3, 2015.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    511
    Likes:
    5
    I am trying to learn basic programming of ds1307
    setup a start condition ( if bus is ready )
    Code (text):
    void I2CStart()
    {
        SDA = 0;
        SCL = 0;
    }
     
    send stop ( to free bus )
    Code (text):
    void I2CStop()
    {
        SCL = 0;
        SDA = 0;
        SCL = 1;
        SDA = 1;
    }
     
    send nack
    Code (text):
    void I2CNak()
    {
        SDA = 1;
        SCL = 1;
        SCL = 0;
        SDA = 1;
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    511
    Likes:
    5

    If you want to write to the device you have to:

    setup a start condition ( if bus is ready )
    write device address ( wait for ack )
    write register address ( wait for ack )
    write device data ( wait for ack )
    send nack ( to stop device )
    send stop ( to free bus )


    If you want to read from the device you have to:

    setup a start condition ( if bus is ready )
    write device address ( wait for ack )
    write register address ( wait for ack )
    send re-start( wait for ack )
    read device ( send ack if more data needed )
    send nack ( to stop device )
    send stop ( to free bus )
    ok I saw that tread , now I am trying to make some code myself

    Code (text):
    void I2C_Init()         //
    {
        SDA = 1;
        SCL = 1;
    }

    void I2C_Start()       // start I2C
    {
        SDA = 0;
        SCL = 0;
    }

    I2C_write()               //  send data to Ds1307
    {
    I2C_write(0xD0);      // address of the DS1307 )
    I2C_write(0x00);      // address of seconds register
    I2C_write(0x10);       // 10 data as 10 second
    }

    void I2C_Ack()
    {
        SDA = 0;
        SCL = 1;
        SCL = 0;
        SDA = 1;

    void I2C_Stop()    // stop I2C
    {
        SCL = 0;
        SDA = 0;
        SCL = 1;
        SDA = 1;
    }

    void I2C_Start()
    {
        SDA = 0;
        SCL = 0;
    }

    I2C_read()
    {
    I2C_write(0xD1);      // address of the DS1307 )
    I2C_write(0x00);      // address of seconds register
    I2C_write(0x10);       // 10 data as 10 second
    }

    void I2C_Nak()
    {
        SDA = 1;
        SCL = 1;
        SCL = 0;
        SDA = 1;
    }

    void I2C_Stop()    // stop I2C
    {
        SCL = 0;
        SDA = 0;
        SCL = 1;
        SDA = 1;
    }

     
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK

    Right!!! The reason we write low level routines is so we can use them in "wrapper " functions

    ack, nack, start, and stop need EVER be accessed again...

    ReadRtc( addr , data )
    start
    wite dev addr
    write reg addr
    restart
    read data
    nack
    stop


    Now you only ever call ReadRtc......

    I gave you an I2C library.... All you need to do is configure the pins then call RTC read and RTC write

    Job done.....
     
    • Like Like x 1
  6. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    511
    Likes:
    5
    write to device
    setup a start condition ( if bus is ready )
    write device address ( wait for ack )
    write register address ( wait for ack )
    write device data ( wait for ack )
    send nack ( to stop device )
    send stop ( to free bus )

    read from device
    setup a start condition ( if bus is ready )
    write device address ( wait for ack )
    write register address ( wait for ack )
    send re-start( wait for ack )
    read device ( send ack if more data needed )
    send nack ( to stop device )
    send stop ( to free bus )

    what about LED , where is function for LED?. how LED will work ?how LED will know that alarm set for second or minute or hours ?
     
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    I think that not big task it will be just using cond if else all, main is how to read and write properly!
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    I don't even know what your LED does????
     
  9. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    511
    Likes:
    5
    ok ,look at this in another way

    1. mirocontroller send data to RTS DS1307.
    2.mirocontroller read the data (alarm Time ) from the ds1307.
    3.Then microcontroller send alarm time to active LED

    I think I have written code for line 1, and Line 2 . how to write code for line 3 ?
    code
    Code (text):
    void I2C_Init()         //
    {
        SDA = 1;
        SCL = 1;
    }

    void I2C_Start()       // start I2C
    {
        SDA = 0;
        SCL = 0;
    }

    I2C_write()               //  send data to Ds1307
    {
    I2C_write(0xD0);      // address of the DS1307 )
    I2C_write(0x00);      // address of seconds register
    I2C_write(0x10);       // 10 data as 10 second
    }

    void I2C_Ack()
    {
        SDA = 0;
        SCL = 1;
        SCL = 0;
        SDA = 1;

    void I2C_Stop()    // stop I2C
    {
        SCL = 0;
        SDA = 0;
        SCL = 1;
        SDA = 1;
    }

    void I2C_Start()
    {
        SDA = 0;
        SCL = 0;
    }

    I2C_read()
    {
    I2C_write(0xD1);      // address of the DS1307 )
    I2C_write(0x00);      // address of seconds register
    I2C_write(0x10);       // 10 data as 10 second
    }

    void I2C_Nak()
    {
        SDA = 1;
        SCL = 1;
        SCL = 0;
        SDA = 1;
    }

    void I2C_Stop()    // stop I2C
    {
        SCL = 0;
        SDA = 0;
        SCL = 1;
        SDA = 1;
    }
     
     
    • Like Like x 1
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    This makes no sense whatsoever!!!!
     
  11. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    511
    Likes:
    5
    Ok I will remove that portion
    just for example:

    1. mirocontroller send data to RTS DS1307.
    Mcu send data ( 10 seconds ,15 minutes , 1 hours) RTS DS1307

    2.mirocontroller read the data (alarm Time ) from the ds1307.
    Mcu read data ( 10 seconds ,15 minutes , 1 hours) from RTS DS1307

    3.Then microcontroller send alarm time to active LED
    now mcu send time alarm ( 10 seconds ,15 minutes , 1 hours) to activate LED
    I have time array
    10 seconds
    15 minutes
    1 hours
    I need to tell led using c program how to write code for point 3?
     
  12. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hello Vead,
    Just wait if you want to do thsi you can perform with simple delay to.
    anyway, just use compare method to perform the 10 seconds routine.
    if(readsec==10)
    portb=1;
    }
    else
    portb=0;

    like this.
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    vead... Look at the other thread by koolguy.... Its more or less what you are doing!!!
     

Share This Page