• AVR Freaks

Hot!Help with MSSP configured as I2C Master

Author
NirVM
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2018/12/26 07:48:00
  • Location: 0
  • Status: offline
2020/05/10 05:33:39 (permalink)
0

Help with MSSP configured as I2C Master

Hello,
 
I am trying to read data from a I2C peripheral. I am using the PIC16F1779. I have used the MCC and included the MSSP module, configuring it as an I2C Master. RC3 and RC4 are selected as the clock and data lines and are each pulled high by an external 10K pull-up resistor.
 
I have used the sample code generated by the MCC under the "i2c_master_example.c" file and have included the following line
in my code:
 
i2cRead = I2C_Read1ByteRegister(0x6B,9);   // 0x6B is the slave address, 9 is the register address within the slave.
 
When running the code, the programs hangs. When probing both the SDA and CLK lines, they are dead (high from the pull up, but no pulses appear).
 
Any ideas?
 
Thank you,
 
Nir.
 
 
 
#1

18 Replies Related Threads

    upand_at_them
    Super Member
    • Total Posts : 585
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 12:17:44 (permalink)
    0
    Post your program.  My guess would be that your program ends immediately after that command.
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11936
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 12:37:41 (permalink)
    0
    0x6B

     
    Did you forget to left shift the address by one bit?
    #3
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 13:19:00 (permalink)
    0
    jtemples
    0x6B

    Did you forget to left shift the address by one bit?

    I have a suspicion MCC doesn't want you to.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11936
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 13:40:55 (permalink)
    0
    I have a suspicion MCC doesn't want you to.

     
    I've never used it, but I suppose it would be expected that MCC would prefer generating the extra code to do a run-time shift...
    #5
    NirVM
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2018/12/26 07:48:00
    • Location: 0
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 22:44:08 (permalink)
    0
    Guys
     
    Thank you for your answers but I think you have missed the main point - nothing is being sent from the processor. It is not an issue of wrong address etc - that I can handle. The problem is deeper - the PIC does not even initiate the start bit nor does it sends any byte out.
     
    @upand - my program does not end after this command but the watchdog (4 seconds) I have does reset the PIC because of this command. The other code I have running is not related to the I2C.
     
    If someone knows why on the very basic hardware level this does not work I will be glad to learn.
     
    Thank you,
     
    Nir.
     
    #6
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 22:46:54 (permalink)
    0
    Really hard to help when you just describe what you think your program does, rather than showing the real code.
    My guess is you have not called the appropriate SPI_Open function.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    NirVM
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2018/12/26 07:48:00
    • Location: 0
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 22:48:31 (permalink)
    0
     
    These are the 3 lines of code I have for handling the I2C:

    I2C_Initialize();
    I2C_Open(0x6B);
    i2cRead = I2C_Read1ByteRegister(0x6B,9);   
     
    #8
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 23:27:35 (permalink)
    0
    Double check if MCC has switched the SDA and SCL pins from analog mode to digital mode.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    NirVM
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2018/12/26 07:48:00
    • Location: 0
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 23:38:26 (permalink)
    0
    Just checked it. They are both digital.
     
     
    #10
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 23:47:36 (permalink)
    0
    Bear with me, as I am not an MCC user. I would use the peripheral directly.
    What does the I2C_Open() function expect as a parameter?
    You appear to be passing the slave address, but I would have expected it to be some flags saying what speed you wanted etc.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    NirVM
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2018/12/26 07:48:00
    • Location: 0
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 23:52:40 (permalink)
    0
    Speed, etc was already set inside MCC. The function needs the slave address.
     
    But hey, I am an old school guy also so if you have some code that directly init and runs the I2C as a master, I will be happy to use it. I tried it at first but it failed.
     
     
    #12
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/10 23:58:01 (permalink)
    0
    Here's some untested code I wrote for an older device.
    http://picforum.ric323.com/viewtopic.php?f=76&t=755
    You may find some ideas there.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    NirVM
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2018/12/26 07:48:00
    • Location: 0
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/11 00:00:48 (permalink)
    0
    Thank you Richard I will look into it.
     
    Nir
    #14
    NirVM
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2018/12/26 07:48:00
    • Location: 0
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/13 03:56:15 (permalink)
    +1 (1)
    Hello again Richard,
     
    Thank you. Your code runs great. It seems my problem was that I forgot to set the PPS output register
    for both RC3,RC4 (clock + data)
     
    Best,
     
    Nir.
     
     
    #15
    NirVM
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2018/12/26 07:48:00
    • Location: 0
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/17 10:38:39 (permalink)
    0
    Hi again @ric,
     
    I just tried to use your code to communicate with a serial eeprom (the 24CW1280) but it failed. After a closer look, I found out that this eeprom requires a two byte word address and not one like in your code. Would you be so kind
    and let me know how can I convert your code to support both single address byte and two address bytes devices?
     
    Nir.
     
    #16
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/17 13:13:20 (permalink)
    0
    There's nothing magic about it all, just make the logical changes.
    i.e. if the "register" is now two bytes instead of one, change the "start_reg" parameter in i2c_writeblock from "unsigned char" to "unsigned int", then extend this code
        //send the device register index
        if (i2c_sendbyte(start_reg))
        {
            i2c_stop(); //if register was NAKed, terminate the cycle
            return 3;   //and return error code
        }

    to send both bytes of the variable, one byte at a time.
    Then, make the same changes to i2c_readblock()
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #17
    suspicion
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2020/05/19 11:14:02
    • Location: 0
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/05/27 07:49:39 (permalink)
    0
    NirVM
    Hi again @ric,
     
    I just tried to use your code to communicate with a serial eeprom (the 24CW1280) but it failed. After a closer look, I found out that this eeprom requires a two byte word address and not one like in your code. Would you be so kind
    and let me know how can I convert your code to support both single address byte and two address bytes devices?
     
    Nir.
     


    I have the same issue but I suppose it would be expected that MCC would prefer generating the extra code to do a run-time
    #18
    bkamen
    Super Member
    • Total Posts : 835
    • Reward points : 0
    • Joined: 2010/01/26 12:39:27
    • Location: Central Illinois, USA
    • Status: offline
    Re: Help with MSSP configured as I2C Master 2020/06/05 08:52:54 (permalink)
    0
    I just saw this now and one of my first thoughts was "Was the PPS set?"

    Don't forget to change your post subject to include "[SOLVED]" to let  others know you figured it out.

    Cheers,

     -Ben
    #19
    Jump to:
    © 2020 APG vNext Commercial Version 4.5