Jump to content

Recommended Posts

We are trying to develop an I2C slave function on a PIC16F876A to communicate with the master device a PIC18F452. The master is successfully talking to other slaves on the bus, but we are struggling to get our own slave working. We have encountered the following conditions:

 

-The slave matches the address from the master and enters the interrupt routine.

-However the slave is not sending an ACK on reception of the address.

-It is also not clearing the buffer upon reading the address. Therefore SSPBUF is never anything but the address of the slave

 

Initially we had the master waiting for an ACK that was never returned, so instead we tried sending data after the address, which causes an overflow in the slave as you would expect.

 

We are at a loss to understand why reading 'sspbuf'is not clearing the register, and why the automatic acknowledge from the MSSP module is not being sent on an address match?

 

See our attached code:

 

//initial state
               //Configure port A
    trisa = 0x35;
    //Configure port B
    trisb = 0xBF;
    //Configure port C
    trisc = 0xFF;
    
    //Configure to OFF A/D pins 
    adcon1 = 0x06;

    //Initialize port A
    porta = 0x00;
    //Initialize port B
    portb = 0x00;
    //Initialize port C
    portc = 0x00;
    
    //Enable interrupts (Global,SSP,External)
    intcon = 0xD0;
    
    //Enable Serial Port
    set_bit(pie1,SSPIE);

    //Configure for I2C Slave Mode
    sspcon = 0b00110110;
    sspcon2 = 0b00000000;
    sspstat = 0b10000000;
    dummy= sspbuf; //clear buffer
    sspadd = 0x10; //Slave Address

//Handle I2C interrupt
    if (pir1.SSPIF == 1)
    {
        if (sspstat.R_W == 0) //Slave read data
        {
            address = sspbuf;   //read address from buffer
            pir1.SSPIF = 0;     //clear interrupt
        
            while (!sspstat.BF);   //wait for next data loaded
            HV = sspbuf;     //read buffer voltage high byte    
            pir1.SSPIF = 0;  //clear interrupt

            while (!sspstat.BF);  //wait for next data loaded
            HC = sspbuf;     //read buffer current high byte
            pir1.SSPIF = 0;  //clear interrupt

            while (!sspstat.BF);  //wait for next data loaded
            LVC = sspbuf;     //read buffer voltage & current low byte
        
        } 
    }

Edited by Panvadee
Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...