• AVR Freaks

Hot!I2C on pic16f1619

Page: 12 > Showing page 1 of 2
Author
abbassi
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2019/10/22 05:53:36
  • Location: 0
  • Status: offline
2020/02/14 06:45:12 (permalink)
0

I2C on pic16f1619

Hello,
I'm working with pic16f1619, I'm trying to use I2C bus to configure a DS1631 THERMOMETRE to read the temperature , but it deosn't work.
my question is the pic16f1619 support the I2C communication to read temparture
thank you!
 
#1

22 Replies Related Threads

    paulfjujo
    paulfjujo
    • Total Posts : 81
    • Reward points : 0
    • Joined: 2011/03/08 05:33:46
    • Location: France 01700
    • Status: offline
    Re: I2C on pic16f1619 2020/02/14 07:13:47 (permalink)
    +1 (1)
    hello,
     
    Yes, 16F1619 can handle I2C bus
    but this PIC uses remappable pins   (PPS)
     example : I2C1 Remappable Hardware =>t   RB4=SDA  RB6=SCL 
    i tested it , sucessfuly with an OLED SSD1306 LCD 128x32
    #2
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/14 07:21:55 (permalink)
    0
    Thank you for your reponse ! 
    How the remappable pins work, this part is missing in my code
    #3
    ric
    Super Member
    • Total Posts : 27655
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C on pic16f1619 2020/02/14 14:19:07 (permalink)
    +3 (3)
    abbassi
    my question is the pic16f1619 support the I2C communication to read temparture

    Wrong sort of question.
    The PIC16F1619 does support I2C, it says so right on page #1 of the datasheet.
    What you use it for depends upon YOUR CODE.
     
    abbassi
    How the remappable pins work, this part is missing in my code

    Pin assignments are controlled by the "PPS" module in the chip.
    There is a whole chapter in the datasheet documenting this.
    http://ww1.microchip.com/...40001770D.pdf#page=178
     
    Pay particular attention to section "13.3 Bidirectional Pins"
     
    If you are using the default I2C pins, then the input assignments are already done for you, but it is still your job to set the output assignments for those pins.
     
    Your complaint "it doesn't work" gives us absolutely no information about what you tried, and what happened when you did try. Did you put ANY code in your program to manage the MSSP/I2C peripheral?
     
    post edited by ric - 2020/02/14 14:20:56

    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
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 00:34:45 (permalink)
    0
    unsigned char i2c_Read(unsigned char ack)
    {
     

     // Read data from slave
     // ack should be 1 if there is going to be more data read
     // ack should be 0 if this is the last byte of data read

     
     
     unsigned char temp;

        SSP1CON2bits.ACKDT = ack;
       PIR1bits.SSP1IF = 0;
      SSP1CON2bits.RCEN = 1;
        
      while(!PIR1bits.SSP1IF);
        temp = SSP1BUF;
       PIR1bits.SSP1IF = 0;
        SSP1CON2bits.ACKEN = 1;
      while(!PIR1bits.SSP1IF);
        return temp;

    }
    //
    //
    //
    void I2CBitDly(void)
    {
    unsigned int time_end = 800;
    unsigned int index;
    for (index = 0; index < time_end; index++);
    return;
    }
    //
    void temp_config(char data) {
     
        StartI2C2();
        WriteI2C2(0x90);
        WriteI2C2(0xAC);
        WriteI2C2(data);
         StopI2C2();
         I2CBitDly();
     
    }

    bit WriteI2C2(unsigned data)
    {
      
      SSP1BUF = data;
      while (!PIR1bits.SSP1IF);
      PIR1bits.SSP1IF=0;
        return ACKSTAT;
    }

       


    void init_temp() {
         StartI2C2();
         WriteI2C2(0x90);
         WriteI2C2(0x51);
         StopI2C2();
         I2CBitDly();
    }
    int read_int_temp() { //read the high byte
    int datah=0;
     int datal=0;
       int data;
      int temp_c;
      
        StartI2C2();
        WriteI2C2(0x90);
        WriteI2C2(0xAA);
       // StartI2C2();
         RestartI2C2();
         WriteI2C2(0x91);
                datah=i2c_Read(0);
                datal==i2c_Read(1);
                //data=i2c_Read(0);
        StopI2C2();
     

    temp_c = ((datah)+ datal) ;


        
        
        
       
              return(temp_c);

        
          
    }



    void InitI2C(void)
    {
        TRISB4=1;
        TRISB6=1;
        SSP1ADD = (_XTAL_FREQ/4000/I2C_SPEED)-1;
        SSP1STAT=0x80;
        SSP1CON1=0x28;
    }
    void StartI2C2(void)
    {
        SSP1CON2bits.SEN=1;
        while(!PIR1bits.SSP1IF);
        PIR1bits.SSP1IF=0;
                
    }
    void RestartI2C2(void)
    {
        SSP1CON2bits.RSEN=1;
        while(!PIR1bits.SSP1IF);
        PIR1bits.SSP1IF=0;
        
    }

    void StopI2C2(void)
    {
        SSP1CON2bits.PEN=1;
        while(!PIR1bits.SSP1IF);
        PIR1bits.SSP1IF=0;
    }

    void I2C_Send_ACK(void)
    {
     ACKDT = 0; // 0 means ACK
     ACKEN = 1; // Send ACKDT value
     while(!PIR1bits.SSP1IF); // Wait for it to complete
     PIR1bits.SSP1IF = 0; // Clear the flag bit
    }
    void I2C_Send_NACK(void)
    {
     ACKDT = 1; // 1 means NACK
     ACKEN = 1; // Send ACKDT value
     while(!PIR1bits.SSP1IF); // Wait for it to complete
     PIR1bits.SSP1IF = 0; // Clear the flag bit
    }

     
    post edited by abbassi - 2020/02/17 00:36:13
    #5
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 00:39:09 (permalink)
    0
    Here is my code but without the PPS module
    #6
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 02:17:57 (permalink)
    0
    I add the PPS module like this :
    ODCONBbits.ODB4 = 1; // 1=open drain output SDA
      ODCONBbits.ODB6 = 1; // 1=open drain output SCL
      RB4PPS=0x11;
        RB6PPS=0x10;
        
        SSPCLKPPS=0b00001110;
        SSPDATPPS=0b00001100;

    #7
    ric
    Super Member
    • Total Posts : 27655
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C on pic16f1619 2020/02/17 05:25:15 (permalink)
    +1 (1)
    Note in your code here:
     // Read data from slave
     // ack should be 1 if there is going to be more data read
     // ack should be 0 if this is the last byte of data read

    The comments are the wrong way around.
    ACKDT must be low to send an ACK cycle, high sends a NAK.
    You must send an ACK after reading the intermediate bytes, and a NAK after the last byte.
     
    Your WriteI2C2() function neglects to clear SSPIF before writing to SSPBUF.
    It does correctly return the ACK/NAK status sent by the slave, but your code calling WriteI2C2() is ignoring this return value, which is silly.
    That return value tells you if the slave is responding. It is the FIRST thing you should be verifying to see if your slave is working.
     

    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
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 05:57:51 (permalink)
    0
    I call this function( read_int_temp()) in my main code , I have another question , I want to read the temperature with this function  what I'm supposed to read?
    thank you
    temperature=read_int_temp();

    #9
    ric
    Super Member
    • Total Posts : 27655
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C on pic16f1619 2020/02/17 06:10:36 (permalink)
    +2 (2)
    This line
    temp_c = ((datah)+ datal) ;

    should be
    temp_c = ((datah << 8)+ datal) ;

     

    I want to read the temperature with this function  what I'm supposed to read?

    What exactly are you asking?
    Once you correctly receive a 16 bit value from the chip, you need to convert it to a temperature as described in the DS1731 datasheet.
    See "Operation—Measuring Temperature" and "Table 4. 12-Bit Resolution Temperature/Data Relationship" in the datasheet.

    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!
    #10
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 06:22:52 (permalink)
    0
    I've changed the line.
     
    when I did a break point in the read_int_temp() function , I have always (0x00)
    #11
    Howard Long
    Super Member
    • Total Posts : 796
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 06:30:49 (permalink)
    +1 (1)
    I am not sure where your code is failing, is it hanging, or just returning an unexpected value?
     
    In the meantime here is some I2C initialisation code I have used in the past on a PIC16F1619:
     

    #define _XTAL_FREQ 32000000
    #define I2C_BAUD_RATE 100000
    .
    .
    .
    #define I2C_TRIS_SCL TRISCbits.TRISC0
    #define I2C_TRIS_SDA TRISCbits.TRISC1
    #define I2C_ANSEL_SCL ANSELCbits.ANSC0
    #define I2C_ANSEL_SDA ANSELCbits.ANSC1
    #define I2C_BRG ((_XTAL_FREQ/4)/I2C_BAUD_RATE-1)
    #define I2C_PPSOUT_SCL_REG RC0PPS
    #define I2C_PPSOUT_SCL_SET 0b10000 // SCK/SCL
    #define I2C_PPSOUT_SDA_REG RC1PPS
    #define I2C_PPSOUT_SDA_SET 0b10001 // SDO/SDA
    #define I2C_PPSIN_SCL_REG SSPCLKPPS
    #define I2C_PPSIN_SCL_SET 0b10000 // RC0
    #define I2C_PPSIN_SDA_REG SSPDATPPS
    #define I2C_PPSIN_SDA_SET 0b10001 // RC1
    .
    .
    .
    void I2CInit(void)
    {
    SSP1CON1=0;
    SSP1STAT=0;

    I2C_TRIS_SCL=1;
    I2C_TRIS_SDA=1;
    I2C_ANSEL_SCL=0;
    I2C_ANSEL_SDA=0;
    I2C_PPSOUT_SCL_REG=I2C_PPSOUT_SCL_SET;
    I2C_PPSOUT_SDA_REG=I2C_PPSOUT_SDA_SET;
    I2C_PPSIN_SCL_REG=I2C_PPSIN_SCL_SET;
    I2C_PPSIN_SDA_REG=I2C_PPSIN_SDA_SET;
     
    SSP1STATbits.SMP=1; /* Slew rate disabled */
    SSP1CON1bits.SSPM=8; // = 0x28; /* SSPEN = 1, I2C Master mode, clock = _XTAL_FREQ/(4 * (SSPADD + 1)) */
    SSP1ADD=I2C_BRG;
    SSP1CON1bits.SSPEN=1;
    }

    #12
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 06:37:22 (permalink)
    0
    for the PPS module I did the assignement like this: is the same thing ?
    RB4PPS=0x11;//0b10001;
        RB6PPS=0x10;//0b10000;
        
        SSPCLKPPS=0b00001110;
        SSPDATPPS=0b00001100;

    #13
    Howard Long
    Super Member
    • Total Posts : 796
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 08:01:37 (permalink)
    +1 (1)
    abbassi
    for the PPS module I did the assignement like this: is the same thing ?
    RB4PPS=0x11;//0b10001;
        RB6PPS=0x10;//0b10000;
        
        SSPCLKPPS=0b00001110;
        SSPDATPPS=0b00001100;





    No, it's not.
     
    In my code I set both TRIS bits, clear any relevant ANSEL bits, configure BOTH PPSOUT and PPSIN for BOTH SCL and SDA.
     
    Have you wired up a scope or LA?
     
    Does your code hang?
     
    What are the symptoms? Just saying "It doesn't work" doesn't help much.
    #14
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 08:06:12 (permalink)
    0
    Yes I wired up a scope in SCL and SDA pin
    I mean by it doesn't work when I did a break point in my function to read the temperature I have always 0
    #15
    Howard Long
    Super Member
    • Total Posts : 796
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 08:19:09 (permalink)
    0
    What do you see on the scope?
    #16
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 08:20:58 (permalink)
    0
    in the SDA AND SCL line I see nothing but in Tout tHE outPut of the DS1631 I have 5V
    #17
    Howard Long
    Super Member
    • Total Posts : 796
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 08:54:47 (permalink)
    0
    Have you cleared ANSELBbits.ANSB4? (There is no analogue function on RB6 to clear)
    #18
    abbassi
    Starting Member
    • Total Posts : 35
    • Reward points : 0
    • Joined: 2019/10/22 05:53:36
    • Location: 0
    • Status: offline
    Re: I2C on pic16f1619 2020/02/17 08:56:38 (permalink)
    0
    Yes I cleared all the ANSELB pin just to be sure
    ANSELB=0x0;

    #19
    ric
    Super Member
    • Total Posts : 27655
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: I2C on pic16f1619 2020/02/17 12:18:06 (permalink)
    +1 (1)
    abbassi
    I've changed the line.
     
    when I did a break point in the read_int_temp() function , I have always (0x00)

    Did you address my other concerns in post#8?
    In particular, are you checking the the ACK status returned by the slave when you address it?
    If not, you're just wasting time do any further tests. That is the FIRST thing you should be checking.
     

    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!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5