• AVR Freaks

Hot!Cannot detect 16f18854 as I2C slave

Author
ronpetit
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2020/07/23 21:03:14
  • Location: 0
  • Status: offline
2020/07/28 07:57:19 (permalink)
0

Cannot detect 16f18854 as I2C slave

Hello, I have a couple days trying to config an I2C slave on a PIC16f18854, right now, I'm just using a Raspberry Pi as Master, but later I will use a nRF52832, I just want to make sure my slave works first, but I think I'm missing something as if I do i2cdetect -y 1 command from the i2ctools linux package I cannot detect the chip as a I2C device.

I do not have and schematic right now, but I'm using the GPIO 3 from the RPi as SDA and GPIO 5 as SCL, both of them have an internal pull up resistor to 3.3v of 1.8K, I checked those resistors and they are fine, those two lines are connected to my chip over the pins RB6 (SCL) and RB7 (SDA), and the chip is getting 3.3V as power source from the raspberry pi itself.
 
I used the MCC to generate the basic code, as far as I know, the MSSP1 will interrupt the on a read request, and a code section will be executed, I will place here my main.c and will attach the whole project.
 
main.c
#include <xc.h>
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/i2c1_slave.h"
#include "main.h"

void main(void) {
        
    // Initialize the device
    SYSTEM_Initialize();
    I2C1_Close();
    I2C1_Open();
    GIE = 1; //Global interrupt enable
    PEIE = 1; //Peripheral interrupt enable
    SSP1IF = 0; //Clear interrupt flag
    SSP1IE = 1;

    while(1);
    I2C1_Close();
}

void I2C1_ReadRequest() {
    volatile uint8_t data;
    I2C1_SendAck();
    SSP1CON1bits.CKP = 0;
    if ((SSP1CON1bits.SSPOV) || (SSP1CON1bits.WCOL)) //If overflow or collision
    {
      data = I2C1_Read(); // Read the previous value to clear the buffer
      SSP1CON1bits.SSPOV = 0; // Clear the overflow flag
      SSP1CON1bits.WCOL = 0; // Clear the collision bit
    }
    if (I2C1_IsRead()) {
        data = I2C1_Read();
        BF1 = 0;
        I2C1_Write(data);
        while(SSP1STATbits.BF);
    }
    SSP1CON1bits.CKP = 1;
    SSP1IF = 0;
}

 
The functions SYSTEM_Inititialize() and I2C1_* were created by MCC, and the I2C1_ReadRequest() is called by the interrupt manager, I will post that part too.
 
interrupt_manager.c
#include "interrupt_manager.h"
#include "mcc.h"
#include "../main.h"

void __interrupt() INTERRUPT_InterruptManager (void)
{
    // interrupt handler
    if(INTCONbits.PEIE == 1)
    {
        if(PIE3bits.BCL1IE == 1 && PIR3bits.BCL1IF == 1)
        {
            //MSSP1_InterruptHandler();
        }
        else if(PIE3bits.SSP1IE == 1 && PIR3bits.SSP1IF == 1)
        {
            //MSSP1_InterruptHandler(); --> will be handled here
            I2C1_ReadRequest();
        }
        else
        {
            //Unhandled Interrupt
        }
    }
    else
    {
        //Unhandled Interrupt
    }
}

 
Attached you will find the rest of the files, as I said, I cannot detect the PIc16f18854 with the command i2cdetect, and I cannot also detect it through python smbus, I tried a lot of things, but with no results, can anyone help me out on what I'm missing?
 
 
post edited by ronpetit - 2020/07/28 08:34:00
#1

3 Replies Related Threads

    ronpetit
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2020/07/23 21:03:14
    • Location: 0
    • Status: offline
    Re: Cannot detect 16f18854 as I2C slave 2020/08/02 16:04:43 (permalink)
    0
    BUMP
    #2
    XorLor
    Junior Member
    • Total Posts : 73
    • Reward points : 0
    • Joined: 2009/10/23 00:17:05
    • Location: 0
    • Status: online
    Re: Cannot detect 16f18854 as I2C slave 2020/08/02 23:48:38 (permalink)
    +1 (1)
    ronpetit
    Hello, I have a couple days trying to config an I2C slave on a PIC16f18854, right now, I'm just using a Raspberry Pi as Master, but later I will use a nRF52832, I just want to make sure my slave works first, but I think I'm missing something


    I would make sure the master is working first. How is I2C configured on the Rpi?

    Do you have a scope to verify the SDA and SCL pins are toggling as expected?

    Why bother with the PIC when the nRF52832 is already configured as a slave device? It seems like more work to have to configure both master and slave devices, rather than just the master.

    [edit]
    After looking at the datasheet, I see the nRF52832 needs to be configured for slave mode, but this seems straight forward:

    To secure correct behaviour of the TWI slave, PSEL.SCL, PSEL.SDA, CONFIG and the ADDRESS[n] registers, must be configured prior to enabling the TWI slave through the ENABLE register. Similarly, changing these settings must be performed while the TWI slave is disabled. Failing to do so may result in unpredictable behaviour.

    [edit]
    post edited by XorLor - 2020/08/03 00:05:53
    #3
    ronpetit
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2020/07/23 21:03:14
    • Location: 0
    • Status: offline
    Re: Cannot detect 16f18854 as I2C slave 2020/08/03 06:16:25 (permalink)
    0
    XorLor
    ronpetit
    Hello, I have a couple days trying to config an I2C slave on a PIC16f18854, right now, I'm just using a Raspberry Pi as Master, but later I will use a nRF52832, I just want to make sure my slave works first, but I think I'm missing something


    I would make sure the master is working first. How is I2C configured on the Rpi?

    Do you have a scope to verify the SDA and SCL pins are toggling as expected?

    Why bother with the PIC when the nRF52832 is already configured as a slave device? It seems like more work to have to configure both master and slave devices, rather than just the master.

    [edit]
    After looking at the datasheet, I see the nRF52832 needs to be configured for slave mode, but this seems straight forward:

    To secure correct behaviour of the TWI slave, PSEL.SCL, PSEL.SDA, CONFIG and the ADDRESS[n] registers, must be configured prior to enabling the TWI slave through the ENABLE register. Similarly, changing these settings must be performed while the TWI slave is disabled. Failing to do so may result in unpredictable behaviour.

    [edit]



    I'm not sure how the master is working over the RPi, as i2cdetect is a linux package already compiled, however I'm thinking of not testing anything over the RPi, I do have an oscilloscope to test the flow, but I think I'm just losing time with that.

    The nRF52832 is not configured as a slave right now, I have done nothing on that side, but thanks for your suggestions over the I2C initialization over there, I will try to run the nRF528232 as master, but first I will use proteus to test my i2c slave code
    #4
    Jump to:
    © 2020 APG vNext Commercial Version 4.5