• AVR Freaks

Hot!PIC16LF1509 I2C not working / SDA and SCL same signal

Author
Azruine
lol
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/04/16 20:11:01
  • Location: 0
  • Status: offline
2019/05/14 00:51:10 (permalink)
0

PIC16LF1509 I2C not working / SDA and SCL same signal

Hello everyone.
I'm facing problems with I2C protocol.
 
This is my code.
 

#include <xc.h>
#define _XTAL_FREQ 16000000
#define XC_HEADER_TEMPLATE_H
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#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 = ON // CLKOUT enabled *16MHz clk?
#pragma config IESO = OFF // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not 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 (High-voltage on MCLR/VPP must be used for programming)
int main()
{
    SSP1CON1 = 0x28;
    SSP1CON2 = 0x00;
    SSP1CON3 = 0x00;
    SSP1ADD = 255;
    TRISBbits.TRISB4 = 1;
    TRISBbits.TRISB6 = 1;
    TRISC = 0;
    TRISAbits.TRISA2 = 0;
    ANSELB = 0;
    OSCCON = 0b01111010;
    LATAbits.LATA2 = 0;

    SSP1CON2bits.SEN = 1;
    while(SSP1CON2bits.SEN);
    SSP1BUF = (0x0D << 1) + 1;
    while(SSP1CON2bits.ACKSTAT);
    LATAbits.LATA2 = 1;

    while(1);
    return 1;
}

#1

5 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: PIC16LF1509 I2C not working / SDA and SCL same signal 2019/05/14 22:17:20 (permalink)
    +2 (2)
    Some more comments in your code, explaining what you are trying to do, would be VERY useful.
    This line is just plain wrong.
        while(SSP1CON2bits.ACKSTAT);
    The correct procedure is to wait for the transfer to finish (check the R/W bit), then read the ACKSTAT bit once.
    It will be low for an ACK, or high for a NAK.
     
     
    Also note,
    int main()
    should be
    void main(void)
     
    and "return 1" should NOT be at the end of your main function.
     
    What is the point of "#define XC_HEADER_TEMPLATE_H" ?
     

    Nearly there...
    #2
    Azruine
    lol
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/04/16 20:11:01
    • Location: 0
    • Status: offline
    Re: PIC16LF1509 I2C not working / SDA and SCL same signal 2019/05/15 00:35:07 (permalink)
    0
    Thank you qhb
     
    I've modified my code as you said but no luck.
     

    SSP1CON2bits.SEN = 1;
    while(SSP1CON2bits.SEN);
    SSP1BUF = (0x0D << 1) + 0;
    while(SSP1STATbits.R_nW);
    confirmACK();

     
    confirmACK function
     

    void confirmACK(void)
    {
        if(SSP1CON2bits.ACKSTAT)
        {
            while(1)
            {
                LATAbits.LATA2 = 1;
                __delay_ms(10);
                LATAbits.LATA2 = 0;
                __delay_ms(10);
            }
        }
    }
     

     
    Signals from SDA/SCL
     

     
    Yellow is SDA and blue is SCL. I can't find start signal :(

    Note) Define line should come from MPLAB header generation. I've generated my own i2c header then copy to my main code. Now removed :)
     
    qhb
    Some more comments in your code, explaining what you are trying to do, would be VERY useful.
    This line is just plain wrong.
        while(SSP1CON2bits.ACKSTAT);
    The correct procedure is to wait for the transfer to finish (check the R/W bit), then read the ACKSTAT bit once.
    It will be low for an ACK, or high for a NAK.
     
     
    Also note,
    int main()
    should be
    void main(void)
     
    and "return 1" should NOT be at the end of your main function.
     
    What is the point of "#define XC_HEADER_TEMPLATE_H" ?
     




    #3
    stenakew
    Starting Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2013/09/04 10:54:16
    • Location: 0
    • Status: offline
    Re: PIC16LF1509 I2C not working / SDA and SCL same signal 2019/05/15 09:50:02 (permalink)
    0
    Hej.
    Using your code from post #1.
    (Modified to be able to use a Curiosity board and avoid compiler error, as noted with "SÅW" comments.)
     

    #include <xc.h>
    #define _XTAL_FREQ 16000000
    #define XC_HEADER_TEMPLATE_H
    // CONFIG1
    #pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin)
    #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
     
    // SÅW #pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
    #pragma config MCLRE = ON
     
    #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 = ON // CLKOUT enabled *16MHz clk?
    #pragma config IESO = OFF // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled)
    #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
    // CONFIG2
    #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
    #pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not 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)
     
    // SÅW #pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
    #pragma config LVP = ON
     
    int main()
    {
    SSP1CON1 = 0x28;
    SSP1CON2 = 0x00;
    SSP1CON3 = 0x00;
    SSP1ADD = 255;
    TRISBbits.TRISB4 = 1;
    TRISBbits.TRISB6 = 1;
    TRISC = 0;
    TRISAbits.TRISA2 = 0;
    ANSELB = 0;
    OSCCON = 0b01111010;
    LATAbits.LATA2 = 0;

    SSP1CON2bits.SEN = 1;
    while(SSP1CON2bits.SEN);
    SSP1BUF = (0x0D << 1) + 1;
    while(SSP1CON2bits.ACKSTAT);
    LATAbits.LATA2 = 1;

    while(1);

    // SÅW return 1;
    }

     
    Recording and decoding signals with a logic analyzer the START condition can be seen in attaced image.
     
    Comments made by "qhb" still applies!
     
    PIC16F1509 (I dont have a "L" chip)
    Microchip Curiosity board
    MPLAB X 5.15
    XC8 2.00
    Win 10
     
    /Sten-Åke
     

    Attached Image(s)

    #4
    Azruine
    lol
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/04/16 20:11:01
    • Location: 0
    • Status: offline
    Re: PIC16LF1509 I2C not working / SDA and SCL same signal 2019/05/22 23:15:59 (permalink)
    0
    Thanks for your reply stenakew, but mine never works :(
     
    Sad but no signal at all
    #5
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: PIC16LF1509 I2C not working / SDA and SCL same signal 2019/05/22 23:20:12 (permalink)
    0
    Please post what your entire C file looks like now.
    A photo of your test board would be useful too. :)

    Nearly there...
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5