Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
On most I2C devices, once that you have sent the device's unique address, and will send the register's address that you would like to write into.
If more than one byte will be written, you can start to sequentially send bytes which will be written in register addresses n, n+1, n+2, and so forth all the way to n+x
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 main() { Address = 0xD0 second = 0x00 minute = 0x01 hour = 0x02 start(); //strat communication Write_Time_DS1307(Address, second, minute, hour) } start(); // start communication stop(); // stop communication send(data);// send 8 bit data void Write_Time_DS1307(0xD0, 0x00, 0x01, 0x02) { send(0xD0) slave send A/N if(A) // check A/N { } }
Hi,
Btw: especially for DS1307 there are more than enough descriptions, examples, even videos...
Address = 0xD0
second = 0x00
minute = 0x01
hour = 0x02
correctionHi,
But your code is weird
In line 10 you call the function with variables ... which is OK.
But in line 17 you use fix values...which makes no sense.Klaus
main()
{
Address = 0xD0
second = 0x00
minute = 0x01
hour = 0x02
start(); //strat communication
Write_Time_DS1307(0xD0, 0x00, 0x01, 0x02)
}
start(); // start communication
stop(); // stop communication
send(data);// send 8 bit data
void Write_Time_DS1307(Address, second, minute, hour)
{
send(0xD0)
slave send A/N
if(A) // check A/N
{
}
Hi,I assume you confuse yourself:
Do you want second (minute, hour..) to be defined as address or as values?Code:Address = 0xD0 second = 0x00 minute = 0x01 hour = 0x02
Klaus
//Address
Address = 0xD0
second = 0x00
minute = 0x01
hour = 0x02
//Value
secondValue = 60
minuteValue = 30
hourValue = 12
This means that we send some data to each address
Hi,
Yes, what else? ...if you want to set the clock.
Btw: "secondValue = 60" is not allowed .... read the datasheet.
Klaus
Write_Time_DS1307(SecondAddress, secondValue, minuteValue, hourvalue)
Write_Time_DS1307(SecondAddress, minuteAddress, hourAdress, secondValue, minuteValue, hourvalue)
{
for (loop)
{
SecondAddress = secondValue;
minuteAddress = minuteValue;
hourAdress = hourvalue;
}
}
There are many way. But your idea isn't bad.What should function pass from main ?
Write_Time_DS1307(SecondAddress, secondValue, minuteValue, hourvalue)
Hi,
From figure6 of the datasheet you should transmit:
* device address
* word address
* data (as much as you like)
Klaus
Write_Time_DS1307(device address, word address, data)
{
device address;
for (n = 0; n <3, n++)
{
word address = data // n = 0, write 10 to word address, n = 1, write 20 to word address, and n = 3, write 30 to word address,
}
}
* device address = 0x0d
* word address = 0x00
* data = 10, 20, 30 // three data bytes
Hi,
Your code example makes no sense.
I can not see wow it follows figure6 of the datasheet.
Klaus
start(); // start communication
stop(); // stop communication
send(Data);// send 8 bit data
main()
{
device Address = 0xD0
word address = 0x00
data[3] = (10, 20, 30)
start(); //strat communication
Write_Time_DS1307(0xD0, 0x00, data[i] )
}
void Write_Time_DS1307(device Address, word address, data
{
send(0xD0)
slave send A/N
if(A) // check A/N
{
}
}
I'm not the friend of this. The more often you knowingly repeat this wrong lines, the more it will be burned into your brain .... and the more difficult it will be later recognize them as wrong.if you seen I am just focusing my attention on function flow. please ignore them for now. I'll fix them later
Why did you chage it now?void Write_Time_DS1307(device Address, word address, data
Hi,
Tell me where do you see the difficulty in figure6?
It tells ... from left to right
* send I2C_START
* send I2C_DEVICE_ADDRESS (with the LSB = 0 to tell you want to write data into the Device
* send ACK = 1 (slave will acknowledge with pulling ACK = 0)
* send WORD_ADDRESS (to tell the slave at wich address you want to put your first data)
* send ACK = 1 (slave will acknowledge with pulling ACK = 0)
main()
{
Address = 0xD0
second = 0x00
data[3] = (59, 49, 39)
start(); //strat communication
send(0xD0)
ReceiveACK_NAk(); //receive ACK/NAK from DS1307
if(ReceiveACK_NAk()= ACK)
send(0x00)
else
stop();
}
Refer to the 8051 datasheet. I'm not familiar with 8051.How to check ACK after sending word address 0x00 ?
]main()
{
[COLOR="#FF0000"]devAddr[/COLOR] = 0xD0
[COLOR="#FF0000"]addrSecond[/COLOR] = 0x00
data[3] = ([COLOR="#FF0000"]0x[/COLOR]59, [COLOR="#FF0000"]0x[/COLOR]49, [COLOR="#FF0000"]0x[/COLOR]39) // 59 is not allowed, since it's binary value is 0b0011[COLOR="#FF0000"]1011[/COLOR]
start(); //strat communication
send([COLOR="#FF0000"]devAddr[/COLOR])
ReceiveACK_NAk(); //receive ACK/NAK from DS1307
if(ReceiveACK_NAk()= ACK)
send([COLOR="#FF0000"]addrSecond[/COLOR])
else
stop();
}
Hi,
KlausCode:]main() { [COLOR="#FF0000"]devAddr[/COLOR] = 0xD0 [COLOR="#FF0000"]addrSecond[/COLOR] = 0x00 data[3] = ([COLOR="#FF0000"]0x[/COLOR]59, [COLOR="#FF0000"]0x[/COLOR]49, [COLOR="#FF0000"]0x[/COLOR]39) // 59 is not allowed, since it's binary value is 0b0011[COLOR="#FF0000"]1011[/COLOR] }
1. The data type is imposed by the argument type of send(). If you had presented complete code instead of snippets, we all would know the answer.0xD0, 0x00 Is it integer byte or char byte?
1. The data type is imposed by the argument type of send(). If you had presented complete code instead of snippets, we all would know the answer.
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 #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 () { InitI2c(); StartI2c(); ACK = write_i2c(unsigned char DS1307_SECONDS);; }