• AVR Freaks

Hot!PIC16LF1503 i2c

Author
rogersw8n
Starting Member
  • Total Posts : 74
  • Reward points : 0
  • Joined: 2015/04/27 11:30:22
  • Location: Georgia, USA
  • Status: offline
2020/07/10 13:18:47 (permalink)
0

PIC16LF1503 i2c

I am attempting to connect to a peripheral device and issue writes and reads via i2c.  I have done this in the past... once so I am sort of new at it.  The board that I was given to work with is very small, 1.25 cm x 1.25 cm.  I do not have connectors that will allow me to connect an o'scope.  I have reduced my code down to the very minimum in order to just read and write via i2c.  However, the START command does not complete successfully.  I get a Bus Collision interrupt thrown as soon as I enable the Start Enable bit.  Below is the code that I am running.  Its just the main function.  The buffer never empties because the buffer collision flag stops the module, I believe.  However, the SEN bit is cleared by the hardware.  Looking for things that may be my issue both RC0 and RC1 have secondary functions.  The i2c is secondary.  I disabled NCO1SEL on RC1.  RC0 is Configurable Cell Logic which is set to zero, disabled.  RC0 and RC1 are set to as inputs.  Do you see anything in my code below that would cause a start bus collision?
 
void main(void) {

    OSCCON = 0x7A; //16 Mhz

    while(!OSCSTATbits.HFIOFR){
        __delay_us(100);
    }
       
    CLRWDT();
    
    APFCONbits.NCO1SEL = 1;
    
    PORTCbits.RC0 = HIGH;
    PORTCbits.RC1 = HIGH;
    
    TRISCbits.TRISC0 = INPUT;
    TRISCbits.TRISC1 = INPUT;
    
    SSP1CON1bits.SSPM = 8;
    SSP1CON1bits.SSPEN = 1;
    
    SSP1ADD = 0x27;
    
    SSP1CON2bits.SEN = 1;
    while(!SSP1CON2bits.SEN){}
    SSP1BUF = 0x30;
    while(!SSP1STATbits.BF){}

        
    SSP1CON2bits.PEN = 1;
    
}

#1

19 Replies Related Threads

    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/10 14:13:08 (permalink)
    0
    Just in case this matters, the board is being powered from the PICKit3.
    #2
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16LF1503 i2c 2020/07/10 14:53:16 (permalink)
    +2 (2)
    You missed the biggest "secondary function".
    Both those pins can be analog inputs, so they power up in analog mode.
    It is your job to write to the ANSELC register to switch them to digital mode.
    Also, do you have pullups on SDA and SCL? You must.
     

    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!
    #3
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16LF1503 i2c 2020/07/10 14:55:02 (permalink)
    +1 (1)
    n.b., NEVER let your main function exit.
    You did not show your config bits. Make sure WDT is turned off. A single call to CLRWDT() in your code won't stop it from triggering.
     
     

    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
    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 05:16:43 (permalink)
    0
    Hi Ric.  Thanks for the reply.  To clarify what I am doing, this is not the full application.  In an effort to reduce the points of code failure, I put together this bare bones app just to see if a start condition was successful in i2c.  My original code is all bit banging, no SSP.  I would never let main return... but thank you.  I am setting break points in the code checking SFR's for status.  Before I go back to the board developer and tell them that there is an issue with the board, I was trying to confirm I am not missing something.  Thank you for the ANSEL pin configuration.  I have set that now as you see below.  I set a break point just before SEN is set.  Hardware clears it but I believe due to the buffer collision.  PIR2bits.BCL1IF gets set immediately after SEN is set.  
     
    // CONFIG1
    #pragma config FOSC = INTOSC // Oscillator Selection Bits (ECH, External Clock, High Power Mode (4-20 MHz): device clock supplied to CLKIN pin)
    #pragma config WDTE = OFF // Watchdog Timer Enable (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
    #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
    #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
    #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)

    // CONFIG2
    #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
    #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
    #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
    #pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled)
    #pragma config LVP = OFF // Low-Voltage Programming Enable (Low-voltage programming enabled)


    void main(void) {

        OSCCON = 0x7A; //16 Mhz

        while(!OSCSTATbits.HFIOFR){
            __delay_us(100);
        }
            
        CLRWDT();
        
        APFCONbits.NCO1SEL = 1;
        CLC2CONbits.LC2EN = 0;
        
        ANSELCbits.ANSC0 = 0;
        ANSELCbits.ANSC1 = 0;
        
        TRISCbits.TRISC0 = 1;
        TRISCbits.TRISC1 = 1;

        CLRWDT();
        
        SSP1CON1bits.SSPM = 8;
        SSP1ADD = 0x27;
        __delay_us(1);
        SSP1CON1bits.SSPEN = 1;
        
        SSP1CON2bits.SEN = 1;
        while(!SSP1CON2bits.SEN){}
        
        CLRWDT();
        
        SSP1BUF = 0x30;
        while(!SSP1STATbits.BF){}
            
        SSP1CON2bits.PEN = 1;
        
    }

     
     
    #5
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16LF1503 i2c 2020/07/11 06:12:53 (permalink)
    +2 (2)
    Still no mention if there are pullups on SDA and SCL.
    Did you try measuring those pins after setting SSPEN to see if they are high or low?
     

    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!
    #6
    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 06:22:09 (permalink)
    0
    Ah... sorry for missing the pull ups.  I do not see them in the diagram.  I'll mention that to the person that designed the board.  Floating could be the issue.
     
    Regarding measurement of the pins, the board is tiny.  I have the equipment to measure but do not believe I could ensure I was reading only RC1 and RC0.  Any recommendations there?  I have an o'scope and multimeter.
    #7
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16LF1503 i2c 2020/07/11 06:24:49 (permalink)
    0
    I assume you are talking about those two pins on the PIC.
    What about on the device they are connected 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!
    #8
    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 06:46:14 (permalink)
    0
    Yes I was referring to the two pins on the PIC.  The device the PIC is connected to is also on the same board and an even smaller chip.
    #9
    Mark Yampolsky
    Super Member
    • Total Posts : 122
    • Reward points : 0
    • Joined: 2009/04/03 18:50:36
    • Location: Russia Fryazino Moskow reg
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 06:50:34 (permalink)
    +2 (2)
    rogersw8n
    the board is tiny

    To work with tiny boards, you should buy a special soldering iron (recommend OKi/Metcal PS900) and a microscope camera. The necessary conclusions for the oscilloscope can be soldered. Even if it’s DFN ...
    #10
    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 06:55:00 (permalink)
    -1 (1)
    DFN?
    #11
    Mark Yampolsky
    Super Member
    • Total Posts : 122
    • Reward points : 0
    • Joined: 2009/04/03 18:50:36
    • Location: Russia Fryazino Moskow reg
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 07:06:31 (permalink)
    +2 (2)
    DFN & QFN - smallest cases for PIC12...16.

    post edited by Mark Yampolsky - 2020/07/11 07:12:26

    Attached Image(s)

    #12
    ric
    Super Member
    • Total Posts : 28009
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16LF1503 i2c 2020/07/11 07:15:13 (permalink)
    +7 (7)
    rogersw8n
    Floating could be the issue. .

    Not "could be". "Will be".
    I2C is an open collector bus. The pull ups are a REQUIRED part of the bus. They are not optional. Whoever thought they were needs to read the basics about I2C again.

    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
    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 07:30:26 (permalink)
    0
    Thank you very much.
    #14
    Mark Yampolsky
    Super Member
    • Total Posts : 122
    • Reward points : 0
    • Joined: 2009/04/03 18:50:36
    • Location: Russia Fryazino Moskow reg
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 07:32:01 (permalink)
    +1 (1)
    ric
    Whoever thought they...

    I guess, that he was counting on an internal pullup, but there is no pullup on the PORTC ...
    We will not talk about the pullup resistance value ... )))
    #15
    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 07:37:05 (permalink)
    0 (2)
    I am relaying that to the designer now.  I would bet he thought there would be internal pullups as well.
     
    Thank you both.
    #16
    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 11:15:04 (permalink)
    +1 (1)
    Confirmed, he did not check to see if the PIC contained internal pull ups for port C.  Thanks again!
    #17
    Mark Yampolsky
    Super Member
    • Total Posts : 122
    • Reward points : 0
    • Joined: 2009/04/03 18:50:36
    • Location: Russia Fryazino Moskow reg
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 11:24:12 (permalink)
    +4 (4)
    It should also be noted that the internal pullup has too much resistance for I2C (50...100 kOhm). Therefore, in any case, an external 4.7 kOhm pullup is needed.
    post edited by Mark Yampolsky - 2020/07/11 11:26:39
    #18
    rogersw8n
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2015/04/27 11:30:22
    • Location: Georgia, USA
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 11:28:34 (permalink)
    0
    Thank you.
    #19
    RISC
    Super Member
    • Total Posts : 5776
    • Reward points : 0
    • Status: offline
    Re: PIC16LF1503 i2c 2020/07/11 15:46:59 (permalink)
    +2 (2)
    Hi,
    Pull-ups on I/O ports are meant for Input mode, not output mode....hence their high value...
    Regards

    For support make sure to check first here : http://microchipdeveloper.com
    There are hundreds of PIC, AVR, SAM...which one do YOU use ?
    #20
    Jump to:
    © 2020 APG vNext Commercial Version 4.5