AnsweredHot!Reading Time(Ds1307) using I2C

Page: 1234 > Showing page 1 of 4
Author
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
2018/03/26 05:24:21 (permalink)
0

Reading Time(Ds1307) using I2C

I'm using PIC18F45K22 and XC8.
I have insert MCC library for I2C
 
I need example to read seconds from ds1307 and in next level to change current time.
 
First question is can i read without to write something frist?
Second  question  i2c giving me example below.
 
    
void I2C2_MasterRead(
                                uint8_t *pdata,
                                uint8_t length,
                                uint16_t address,
                                I2C2_MESSAGE_STATUS *pstatus);

 
pdata is the register for seconds? example 0x00?
length is the number of bytes?
adress is the adress from ds1307?
pstatus>?>?what is this?
#1
Gort2015
Klaatu Barada Nikto
  • Total Posts : 2645
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/26 08:18:51 (permalink)
+1 (1)
After [start], write the DS1307 i2c device address with bit 0 clear then write the address of the rtc (0 for seconds).
[restart] then write the DS1307 i2c device address with bit 0 set, read i2c with NACK as final.
[stop]
 
The chip wll increment it's address.  To read more, read with ACK, NACK as final then [stop]
 
 
I don't use MCC.
The pstatus is probably ACK/NACK, collision or overflow.
 
That function is like an i2c_gets, I don't why they use a pointer when the error could be returned.
 
I would have expected uint8_t *pdata to be void *pdata

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#2
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/26 22:07:52 (permalink)
0
How can i find ds1307 adress? in datasheet doesnt find anything about it
Can you give me an example how can i send an adress using mcc write example?

 
void I2C2_MasterWrite(
uint8_t *pdata,
uint8_t length,
uint16_t address,
I2C2_MESSAGE_STATUS *pflag)

 
Or if i can create my own internal clock using a timer...?
post edited by dimis1989 - 2018/03/26 22:26:58
#3
qɥb
Monolothic Member
  • Total Posts : 3282
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: Reading Time(Ds1307) using I2C 2018/03/26 23:04:48 (permalink)
0
dimis1989
How can i find ds1307 adress? in datasheet doesnt find anything about it

I'm looking at the datasheet at https://datasheets.maximi...d.com/en/ds/DS1307.pdf
Yes, they could make it a lot more obvious, but the slave address appears several times on pages 12 and 13.
 
The  slave  address  byte  contains  the  7-bit  DS1307 address, which is 1101000, followed by the direction bit (R/W), which for a write is 0.
 
The slave address byte contains the 7-bit DS1307 address, which is 1101000, followed by the direction bit (R/W), which  is  1  for  a  read.
 
and the same again in figure 4 and figure 5.
 
In 8-bit form, that is 1101 0000, which is 0xD0.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#4
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/26 23:04:49 (permalink)
0
I'm trying with this but is stacked inside while pending
while (status == I2C_MESSAGE_PENDING);

    
I2C_MESSAGE_STATUS status;
uint8_t pdata_write = 0; // 0 to 'seconds' register
uint8_t pdata_read; // will hold 'seconds'

I2C2_MasterWrite(&pdata_write, 1, 0b11010000, &status);
// at this point, your status will probably be I2C2_MESSAGE_PENDING
while (status == I2C_MESSAGE_PENDING); // wait for status to to change
if (status == I2C_MESSAGE_COMPLETE) {
I2C2_MasterRead(&pdata_read, 1, 0b11010001, &status);
while (status == I2C_MESSAGE_PENDING); // again, wait for status to to change
if (status == I2C_MESSAGE_COMPLETE) {
// pdata_read should now be the number of seconds (in binary-coded decimal)
}
}

sprintf(txt,"%f",pdata_read);
LCDPutStr(txt,1);
 

Attachment(s)

Attachments are not available: Download requirements not met
#5
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 00:30:30 (permalink)
0
Any help?
#6
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 01:14:59 (permalink)
0
My pins are configured automatically from mcc, both of them as input, is this right? Maybe mssp handles their position?
#7
qɥb
Monolothic Member
  • Total Posts : 3282
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: Reading Time(Ds1307) using I2C 2018/03/27 01:47:23 (permalink)
0
dimis1989
My pins are configured automatically from mcc, both of them as input, is this right?

Yes, that is correct.
 

Maybe mssp handles their position?

They must be floating until the point where the peripheral wants to drive them low.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#8
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 01:49:33 (permalink)
0
Any idea qɥb why my code is stacked inside while loop? It doesnt get any response
#9
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 01:51:09 (permalink)
0
Can be wrong order? Example should i send first ds1307 adress, then register and then data?Or it doesnt have to do with it?
#10
qɥb
Monolothic Member
  • Total Posts : 3282
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: Reading Time(Ds1307) using I2C 2018/03/27 02:15:45 (permalink)
0
I don't use MCC, so know little about it.
Hopefully someone familiar with MCC will pop in.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#11
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 02:50:56 (permalink)
0
Dont you have any example from i2c how to can i write and read with a peripheral device ?
#12
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 03:54:26 (permalink)
0
Just send me the easiest example for sending-reading trow i2c
#13
rodims
Super Member
  • Total Posts : 1312
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 05:04:32 (permalink)
0
This looks very similar to your problem ...
https://stackoverflow.com...g-timeds1307-using-i2c
 
and some code (google is your friend)
http://www.edaboard.com/showthread.php?t=309498
 
 
 
 
 
#14
katela
Super Member
  • Total Posts : 854
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: online
Re: Reading Time(Ds1307) using I2C 2018/03/27 05:05:39 (permalink)
0
Writing and Reading from the DS1307 slave can be summarized as follow:
 
Master Writes to a Slave
Send a START bit (logic zero)
Send the I²C address of the SLAVE with the R/W bit low (even address)
Send the device register you want to write to
Send the data byte
Send the STOP sequence.

Master Reads from a Slave
Send a START
Send I²C address of the SLAVE with the R/W bit low (even address)
Send device register you want to read from
Send a START sequence again (a repeated start)
Send I²C address of the SLAVE with the R/W bit high (odd address)
Read data byte from SLAVE
Send the STOP sequence.
With Peripheral library this is how you could do it:
 
Write: Set Minutes:

void main(void)
{
OpenI2C( MASTER, SLEW_OFF); // Initialize I2C module
SSPADD = 19; //100kHz Baud clock @8MHz

StartI2C(); // begin I2C communications
IdleI2C();  //This function checks the state of the I2C peripheral and waits for the bus to become available.
WriteI2C( 0xD0 ); // addresses the chip. According to page 12 of datasheet), the 7-bit address is ‘1101000’. So where’s the 8th bit? After every address byte sent by the PIC using I2C, the 8th bit determines if the next byte is written or read by the PIC: 0 to write and 1 to read.  The least significant bit (8th bit) is 0, so the full 8-bit data to write will be 0xD0
IdleI2C();
WriteI2C( 0x01 ); // access register address for minutes check datasheet. According to the Table 2 in datasheet, each register has a hex value address associated with it. Notice that the MINUTES register is 01h (or 0x01). Therefore, to access the register, the PIC writes 0x01.
IdleI2C();
WriteI2C( 0b00010011 ); // write value into minutes register. The MINUTES register is composed of 2 parts: The lower nibble (bit 0 through bit 3) and the upper nibble (bit 4 through bit 6). Both nibbles are used together to represent double-digit numbers. For example, the number “13” is composed of a ‘1’ and a ‘3’. Each digit is stored in a separate nibble. The ‘1’ is stored as a BCD number in the upper nibble while the ‘3’ is stored as a BCD number in the lower nibble. For example, to store the number “13”, the MINUTES register should be set to ‘001 0011’ in binary.The ‘001’ is BCD for ‘1’ and 0011 is BCD for ‘3’
IdleI2C();
StopI2C(); // stop condition I2C on bus
}

 
Read:  Get day of the week

void main()
{

char resultI2C

 

StartI2C();              // Start condition I2C on bus

IdleI2C();

WriteI2C( 0xD0 );    // addresses the chip

IdleI2C();

WriteI2C( 0x03 );     // write register address 0x03 for the day of the week (Table 2 above)

IdleI2C();

StopI2C();              // Stop condition I2C on bus

 

StartI2C();              // Start condition I2C on bus

IdleI2C();

WriteI2C( 0xD1 );    // addresses the chip with a read bit: 7-bit address ‘1101000’ plus a 1 bit to read which gives 0xD1

IdleI2C();

resultI2C = ReadI2C();  // read the value from the RTC and store in resultI2C variable.

IdleI2C();

NotAckI2C();     // Not Acknowledge condition. This will signal the RTC that reading data has completed.

IdleI2C();

StopI2C();                 // Stop condition I2C on bus

}



Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/user/StudentCompanionSA
#15
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 05:12:29 (permalink)
0
how do you create StartI2C and everything else? are they ready library?
#16
mbrowning
Just a Member
  • Total Posts : 1028
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 05:15:03 (permalink)
0
Look in your MCC generated files. Instruction on using MCC functions are generally in the header files

Can't remember. I've slept since then - Mark
#17
katela
Super Member
  • Total Posts : 854
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: online
Re: Reading Time(Ds1307) using I2C 2018/03/27 05:21:58 (permalink)
0
dimis1989
how do you create StartI2C and everything else? are they ready library?


Those are PIC18F peripheral libraries functions in case you want to use them or follow along maybe will assist you with your project.
You can also create your own functions to perform the same functions if MCC seems too complicated.
There are many threads in this forum on I2C.
 

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/user/StudentCompanionSA
#18
dimis1989
Super Member
  • Total Posts : 236
  • Reward points : 0
  • Joined: 2017/11/03 10:29:26
  • Location: 0
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 05:30:44 (permalink)
0
I need to create just an easy code for setting up my rtc clock ds1307 and then to read each value.
Smile: Smile
#19
rodims
Super Member
  • Total Posts : 1312
  • Reward points : 0
  • Joined: 2009/02/10 11:08:59
  • Location: 51.9627, 7.6262
  • Status: offline
Re: Reading Time(Ds1307) using I2C 2018/03/27 05:46:38 (permalink)
+1 (1)
Katela provided code using PLIB (peripheral library) and in my link you also find code for the PLIB with exact matching configuration of PIC and DS1307.
What exactly in your terms is "easy" ?
 
#20
Page: 1234 > Showing page 1 of 4
Jump to:
© 2018 APG vNext Commercial Version 4.5