PDA

View Full Version : problems with I2C.... and ideas why dosent work?



snood
- 23rd April 2005, 11:19
okay, i'm using a PIC16F876A and a DS1307 RTC, i have written a short program which should set the time, then loop reading the time to confirm that it is infact set. the problem is when i go to run this in the device... the LCD screen just shows up values with everything reading "00". this to me suggests that i have something wrong with the write commands. here's the offending code:


TRISC = %11111111
TRISB = %11111111

second var byte
minute var byte
hour var byte
day var byte
date var byte
month var byte
year var byte

ITCDEVICE var byte
ITCADDRESS var byte

SDA var PORTC.4
CLK Var PORTC.3

LCDOUT var PORTB.0

ITCDEVICE = $D0
ITCADDRESS = $00

second = 0
minute = 0
hour = 12
day = 2
date = 25
year = 5

pause 1000

I2CWRITE SDA, CLK, ITCDEVICE, ITCADDRESS, [second, minute, hour, day, date, year]

second = 255 'change variables to make sure it recieves correctly'
minute = 255
hour = 255
day = 255
date = 255
year = 255

loop:

pause 500

I2CREAD SDA, CLK, ITCDEVICE, ITCADDRESS, [second, minute, hour, day, date, year]

serout LCDout, 6,[254,1] 'clears screen'
serout LCDout, 6,[254,128] 'sets position'
serout LCDout, 6,[#hour,":",#minute,":",#second] 'displays the read time
serout LCDout, 6,[254,192] 'next line'
serout LCDout, 6,["month:",#month," year:",#year] 'displays the read date

goto loop

@sda
- 23rd April 2005, 14:15
Spelling mistake?
SCL instead of SLK for the I2C variable.

sda

Archilochus
- 23rd April 2005, 16:45
Hi snood,
Isn't "LCDOUT" a reserved word in PBP? Don't know if that might cause problems.

Arch

snood
- 23rd April 2005, 17:00
Hi snood,
Isn't "LCDOUT" a reserved word in PBP? Don't know if that might cause problems.

Arch

well i have just actually re-written that for the purpose of the forum, i dont currently have access to my one with the problem... LCDOUT is just what i called it here, it's called "screen" on my actual version, either way, it's not the screen i'm having trouble with, thats working fine, it's just not displaying the "correct" information from the RTC.. i just read in another post something about when the DS1307 is brand new you have to do something to it to make it work, can someone explain this to me? as the above code is being run on a fresh 1307.

thankyou

Paul

NavMicroSystems
- 23rd April 2005, 20:05
Snood,

if you search the Forum for "DS1307" you'll find a number of examples.
(be honest, you haven't really searched)

Do you have 4k7 Pullup Resistors on SDA and SCL ?

For additional Information see: DataSheet (http://pdfserv.maxim-ic.com/en/ds/DS1307.pdf)

snood
- 23rd April 2005, 21:06
ogg dear i never noticed the 4k7 resistor... how vital is this to have? i connect the resistor to the SDA only according to the datasheet, what does the other end of the resistor connect to? +5v?

and yes ive searched for 1307.

paul

Archilochus
- 23rd April 2005, 23:46
Hi Snood,
Ohh... yeah, those pullups are important! Hook them from the SDA and SCL lines to your positive rail that powers the PIC and 1307.

Here's a bit from the data sheet about starting up a 'new' 1307:

"Bit 7 of register 0 [seconds] is the clock halt (CH) bit. When this bit is set to a 1, the oscillator is disabled. When cleared to a 0, the oscillator is enabled."

Since you set 'seconds' to 0, the clock should be enabled, assuming the the I2C write command is working properly.

Arch

NavMicroSystems
- 24th April 2005, 00:22
ogg dear i never noticed the 4k7 resistor...
No pullUp, No Success

what does the other end of the resistor connect to? +5v?
what else could you connect a PulUp to?


...and yes ive searched for 1307.
Well, then everything should be pretty clear.
(if you have taken the time to read through the threads)

snood
- 24th April 2005, 11:14
please..... i didnt come here to be put down. can you explain to me what the purpose of a pullup resistor is and why it is needed in this instance.

Snood

edit:

also... as far as initialising the chip goes.... apparently i have to change bit 7 of the seconds byte to 0 for it to "turn on"..... corect me if i'm wrong but surely i'm already doing this by writing "0" as the byte?

Snood

NavMicroSystems
- 24th April 2005, 12:39
...can you explain to me what the purpose of a pullup resistor is and why it is needed in this instance.


OK, I'll try it in brief:

A PullUp is required when you have an output pin that is open Drain (or open Collector)
An open collector output is "open" if not set "LOW"
The PullUp pulls this open Pin "HIGH"

The SDA pin on an I2C device is such an open Drain output as it is bi-directional.

Melanie has written an excellent example for the DS1307, see: MN1307.txt (http://www.microengineeringlabs.com/resources/samples/submitted/MN1307.txt)

Another thread about RTC's with a lot of useful information can be found here (http://www.picbasic.co.uk/forum/showthread.php?t=250&highlight=ds1994)

Take some time to read it from the beginning, you can really learn a lto from it.

snood
- 24th April 2005, 14:54
so would not having the resistors in explain why i'm getting "00" displaying on my LCD for all the values afrer trying to read?

i ask again.... would i be right in just writing "0" for the seconds value in order to initialise it. i figure this cos surely BCD for 0 is %00000000 and the 7th bit is 0 so this should work... right?

Snood

snood
- 24th April 2005, 21:21
could anyone confirm the above.... i kinda need to know quite quickly.

Snood

Archilochus
- 24th April 2005, 23:01
That'll work fine to set bit.7 assuming your I2C write is working properly.
Program the chip and give it a go.

Arch

snood
- 25th April 2005, 18:16
okay, ive added in the pullup resistors, ive ensured everything is connected to the correct things, ive made sure that i am addressing correctly, as far as i can see, i'm doing everything as i should.... now when i try set the time/dats as above and then read it back i get the values "16"

this is also true of the i2C EEPROM i am using on the same bus.

any ideas?

Snood

edit: i just thought that the value 16 is a hex "F" any ideas?

NavMicroSystems
- 25th April 2005, 19:07
Snood,
this is a perfect time to have a look at the datasheet and the examples you have got.

If you are expecting any further assistance post your schematic and code.
(please cut the code down to the minimum required for reading writing the RTC / EEPROM)

But before posting please do some reading!

snood
- 25th April 2005, 19:20
please..... i have read the data sheet over and over again, ive read various places about I2C and i have studied the sample codes that i have been pointed to and still not a clue


TRISB = %11111111

second VAR BYTE
minute VAR BYTE
hour VAR BYTE
day VAR BYTE
date VAR BYTE
month VAR BYTE
year VAR BYTE

ITCDEVICE VAR BYTE
ITCADDRESS VAR WORD

SDA VAR PORTC.4
SCL VAR PORTC.3

ITCDEVICE = $D0
ITCADDRESS = $00

second = 0
minute = 0
hour = 12
day = 2
date = 25
year = 5

I2CWrite SDA, SCL, ITCDEVICE, ITCADDRESS, [second, minute, hour, day, date, month, year]

Pause 500

I2CRead SDA, SCL, ITCDEVICE, ITCADDRESS, [second, minute, hour, day, date, month, year]

at this point i dend the variables to my LCD and what comes up on the screen is "16"

thanks

Snood

edit: it's been suggested to me that as i'm recieving "16" or a single "f" that maybe something is holding the data line up? still clueless other than that

NavMicroSystems
- 25th April 2005, 20:06
ITCADDRESS VAR WORD
the RTC requires a BYTE address, not WORD !


second = 0
minute = 0
hour = 12
day = 2
date = 25
year = 5

I2CWrite SDA, SCL, ITCDEVICE, ITCADDRESS, [second, minute, hour, day, date, month, year]


1. there is no explicit value in "month"

2. the RTC uses BCD format, the decimal values you are writing to the RTC don't make much sense.
-----
Now it's up to you to find out what the correct format would look like.


it's been suggested to me that as i'm recieving "16" or a single "f" that maybe something is holding the data line up? still clueless other than thatwithout a schematic we can't help
You have already got more information than you would ever need to solve this problem.

Do some work on your own.

snood
- 25th April 2005, 21:11
the pic will automaticaly transmit the numbers in BCD.... even mel uses just regular numbers... and the missing month value is just a typing error, it is infact in my program.

a direct quote from the DS1307 datasheet:

"After the DS1307 acknowledges the slave address + write bit, the master transmits a word address to the DS1307."

WORD address

as fror the schematic... it's connected in the same fassion as the typical operating citcuit in the datasheet with 4k7 pullups... the Square wawe pin is just left hanging as i'm not using it.

please stop telling me to do my own work... the questions i ask are simply what i cant find myself so i'm calling upon the expertese of other people. if you dont know how to solve my problem then just say so.

Snood

NavMicroSystems
- 25th April 2005, 21:53
This DOES work:


I2CWrite SDA, SCL, $D0,0, [second, minute, hour, day, date, month, year]

Pause 500

I2CRead SDA, SCL, $D0, 0, [second, minute, hour, day, date, month, year]what more do you want?

we can't fix your hardware remotely.


the pic will automaticaly transmit the numbers in BCD.... WOW, that's great, so I can save a lot of codespace by removing all those conversions from my programs and let the PIC do it on it's own.
But how do you tell the PIC what format you want/need?
(or is there some kind of "Do What I Mean" Wizzard I didn't know about yet?)



please stop telling me to do my own work...
OK, there will be silence on this thread now...

Archilochus
- 25th April 2005, 22:01
Hi snood,
Odd... You've got this:
a direct quote from the DS1307 datasheet:
"After the DS1307 acknowledges the slave address + write bit, the master transmits a word address to the DS1307."

I can't find that quote at all in my 1307 data sheet. Mine says this:
"The DS1307 operates as a slave device on the serial bus. Access is obtained by implementing a START condition and providing a device identification code followed by a register address."

"device identification code" and "register address" are both bytes.
My code works fine with bytes for both items.

Are you certain you're reading a 1307 data sheet - sounds more like the sheet for the 1340?

Arch

snood
- 25th April 2005, 22:18
i got the datasheet here: http://pdfserv.maxim-ic.com/en/ds/DS1307.pdf

under the "Slave Receiver Mode (Write Mode)" section of page 11

also on page 12 the data diagram shws a word address.

this is highly confusing.

Snood

edit: equaly it says: The DS1307 operates as a slave device on the I2C bus. Access is obtained by implementing a START
condition and providing a device identification code followed by a register address.

not "serial" but "i2c"

are you looking at an old version maybe?

NavMicroSystems
- 25th April 2005, 22:44
Hi Arch,

Snood has got more than one tested and working examples.

If one doesn't accept that 8 bits make a byte
and 16 bit make a word
and decimal 25 is different from hex 25
there is nothing we can do in addition.

Archilochus
- 25th April 2005, 22:54
Just down-loaded the sheet in your link, and it is different than the one I got from Maxim's site on March 19 of this year.

The "I2C" vs "serial" wording I think was just due to some patent / copyright issues.

Since the 1307's register map only goes to $3F, writing a WORD for the register address probably causes problems. Use a BYTE instead - works for me!

Arch

snood
- 25th April 2005, 23:35
thanks Arch

where did hex come into the equation ralph? the calender is written in BCD so what's wrong with using 25 in the variable as PBP will just convert it into 00011001 if i defined the data as $25 then yes... i would be trying to use hex which would of course give 100101 or 37 decimal. but i AM using decimal so thats no problem

who said that i was denying that 8bits = byte and 16bits = word? i didnt

please refrain from being so scathing and be polite like Arch

thankyou

Paul

NavMicroSystems
- 26th April 2005, 00:05
The RTC works in BCD Format.

If you want to set the date to the 25th. you would have to write $25 not a decimal 25 (which is $19)

If you write a decimal 25 for the 25th of a month you are in trouble:
at midnight the RTC will increment the decimal 25 ($19) to $20 (NOT $1A, because we are talking BCD!),
which results in decimal 32, which is an invalid date in your decimal coding!

If you want decimal numbers for easier editing/calculation you have got to convert them.

See the examples you have got.

Warrier
- 26th April 2005, 12:54
Hi Sood:

Just thought I might ask the obvious... your PIC is running at 4MHz or less, correct? DS1307 has the standard clock frequency and may not run above 4MHz.

Try making the I2C clock line bipolar by DEFINE I2C_SCLOUT 1.

You need to allow some time for the write to I2C completion - 10msec may be enough.

For initialization of a virgin DS1307 take a look at Ms. Neuman's discussion at http://www.picbasic.co.uk/forum/showthread.php?t=490&highlight=non-eeprom

-warrier