problems with I2C.... and ideas why dosent work?


Closed Thread
Results 1 to 26 of 26
  1. #1
    snood's Avatar
    snood Guest

    Default problems with I2C.... and ideas why dosent work?

    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
    Last edited by snood; - 23rd April 2005 at 16:06.

  2. #2
    @sda's Avatar
    @sda Guest


    Did you find this post helpful? Yes | No

    Default

    Spelling mistake?
    SCL instead of SLK for the I2C variable.

    sda

  3. #3
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Hi snood,
    Isn't "LCDOUT" a reserved word in PBP? Don't know if that might cause problems.

    Arch

  4. #4
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Archilochus
    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

  5. #5
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    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
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  6. #6
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    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

  7. #7
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    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

  8. #8
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by snood
    ogg dear i never noticed the 4k7 resistor...
    No pullUp, No Success
    Quote Originally Posted by snood
    what does the other end of the resistor connect to? +5v?
    what else could you connect a PulUp to?

    Quote Originally Posted by snood
    ...and yes ive searched for 1307.
    Well, then everything should be pretty clear.
    (if you have taken the time to read through the threads)
    Last edited by NavMicroSystems; - 24th April 2005 at 00:25.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  9. #9
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by snood; - 24th April 2005 at 11:28.

  10. #10
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by snood
    ...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

    Another thread about RTC's with a lot of useful information can be found here

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

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  11. #11
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    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

  12. #12
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    could anyone confirm the above.... i kinda need to know quite quickly.

    Snood

  13. #13
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    That'll work fine to set bit.7 assuming your I2C write is working properly.
    Program the chip and give it a go.

    Arch

  14. #14
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default still not working

    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?
    Last edited by snood; - 25th April 2005 at 19:00.

  15. #15
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    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!
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  16. #16
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    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

  17. #17
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Code:
    ITCADDRESS VAR WORD
    the RTC requires a BYTE address, not WORD !
    Code:
    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 that
    without 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.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  18. #18
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    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

  19. #19
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    This DOES work:
    Code:
    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...
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  20. #20
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by Archilochus; - 25th April 2005 at 22:04.

  21. #21
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    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?
    Last edited by snood; - 25th April 2005 at 22:22.

  22. #22
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

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

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  23. #23
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    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

  24. #24
    snood's Avatar
    snood Guest


    Did you find this post helpful? Yes | No

    Default

    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

  25. #25
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    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.
    Last edited by NavMicroSystems; - 26th April 2005 at 00:27.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  26. #26
    Warrier's Avatar
    Warrier Guest


    Did you find this post helpful? Yes | No

    Default

    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/show...ght=non-eeprom

    -warrier

Similar Threads

  1. I2C problems
    By Mad_Labs in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th September 2005, 05:22

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts