Microcontroller as slave, ACK problem

Status
Not open for further replies.

spartekus

Member level 5
Joined
May 27, 2010
Messages
81
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Duisburg, Germany
Visit site
Activity points
1,918
Hi,

I am trying to use microcontroller as slave.

master i2c code works fine with other sensors, i wanted to add it also other mcu for slave. i can not get ACK from slave.

in slave code, do i have to creat ACK myself, or mcu creat it itself?

Code:
enum commands {
    start=0,
    addr,
    recieve,
    write,
    stop};

void __attribute__((interrupt,no_auto_psv)) _SI2C1Interrupt(void) {

//void i2c(){
    I2C1ADD = SLAVE_ADRESS;
    static unsigned char *d = 0x00;
    unsigned char data_t = 0xFF;
    static unsigned char cmd = start;

    slave:
    switch(cmd) {
        case start:
            if(I2C1STATbits.S)                              //start bit arrived
                cmd = addr;
            else if(I2C1STATbits.P) {                       //stop bit arrived
                cmd = start;
                goto slave;}
 /*****************************************************************************/
        case addr:
            if(!I2C1STATbits.D_A)    {                       // data recieved
                if(!I2C1STATbits.R_W){
                    _TRISB5 = 1;
                    cmd = recieve;
                }
               else if(I2C1STATbits.R_W){
                    _TRISB5 = 0;
                    cmd = write;
                  }
            }
            else cmd = recieve;                            // adress recieved,
            break;
/******************************************************************************/
        case recieve:
            while(!I2C1STATbits.RBF);
            *d = I2C1RCV;
            cmd = start;
            break;
/******************************************************************************/
        case write:
            I2C1TRN = data_t;
            while(I2C1STATbits.TBF);
            cmd = start;
 /*****************************************************************************/
    }
    _SI2C1IF = 0;
    goto slave;
}

thanks for your helps
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…