• 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.

8051 with real time clock

Parth86

Member
Your code is not correct... The write to the I2C bus is a specific sequence..

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 )

Without this the device won't respond....
I am trying to learn basic programming of ds1307
setup a start condition ( if bus is ready )
Code:
void I2CStart()
{
    SDA = 0;
    SCL = 0;
}
send stop ( to free bus )
Code:
void I2CStop()
{
    SCL = 0;
    SDA = 0;
    SCL = 1;
    SDA = 1;
}
send nack
Code:
void I2CNak()
{
    SDA = 1;
    SCL = 1;
    SCL = 0;
    SDA = 1;
 

Parth86

Member
Your code is not correct... The write to the I2C bus is a specific sequence..

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 )

Without this the device won't respond....

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

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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.....
 

Parth86

Member
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.....
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 ?
 

koolguy

Active Member
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 ?
I think that not big task it will be just using cond if else all, main is how to read and write properly!
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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 ?
I don't even know what your LED does????
 

Parth86

Member
I don't even know what your LED does????
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:
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;
}
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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
}
This makes no sense whatsoever!!!!
 

Parth86

Member
This makes no sense whatsoever!!!!
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?
 

koolguy

Active Member
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.
 

Latest threads

EE World Online Articles

Loading

 
Top