int main(void) { // (Using SDA1 and SCL1 // Configure I2C Port i2cInit(1,157); delay_ms(500); while(1) { // Start bit + Address + read or write bit i2c1StartAddress(I2Caddr, 0); //(7-seg address, write (0)) // Data + Ack i2c1WriteByte(clrDisplay); i2c1WriteByte('1'); i2c1WriteByte('2'); i2c1WriteByte('3'); i2c1WriteByte('4'); // Stop Bit i2c1Stop(); delay_ms(1000); // Start bit + Address + read or write bit i2c1StartAddress(I2Caddr, 0); //(7-seg address, write (0)) // Data + Ack i2c1WriteByte(clrDisplay); i2c1WriteByte('a'); i2c1WriteByte('5'); i2c1WriteByte('d'); i2c1WriteByte('b'); // Stop Bit i2c1Stop(); delay_ms(1000); } return (EXIT_SUCCESS); }
void __attribute__((__interrupt__, no_auto_psv)) _MI2C1Interrupt(void){ switch (i2c1State) { case CONFIG_START: if (!I2C1CONbits.SEN){ // change the state i2c1State = CONFIG_IDTX; // Send the address to write i2c1Write(MAG_WRITE); } break; case CONFIG_IDTX: if (!I2C1STATbits.ACKSTAT){ // change the state i2c1State = CONFIG_REG_TX; // send the register address i2c1Write(reg2Config); } break; case CONFIG_REG_TX: if (!I2C1STATbits.ACKSTAT){ i2c1State = CONFIG_VAL_TX; if (reg2Config==REGISTER_A){ // send the actual configuration for 50Hz i2c1Write(MODE_50_HZ); } else { // send the configuration for continuous sampling i2c1Write(MODE_CONTINUOS); } } break; case CONFIG_VAL_TX: if (!I2C1STATbits.ACKSTAT){ i2c1State = CONFIG_DONE; // Send a bus Stop i2c1Stop(); } break; case CONFIG_STOP: if (!I2C1CONbits.PEN){ if (reg2Config!=MODE_REGISTER){ // Set the Config to idle, wait for next config i2c1State = CONFIG_IDLE; } else { // Ready to read magnetometer data i2c1State = READ_IDLE; } } break; case READ_START: if (!I2C1CONbits.SEN){ // change the state i2c1State = READ_IDTX; // Send the address to write i2c1Write(MAG_READ); } break; case READ_IDTX: //if (!I2C1CONbits.RSEN){ if (!I2C1STATbits.ACKSTAT){ // change the state i2c1State = READ_DATA; // Start Clocking Data I2C1CONbits.RCEN = 1; } break; case READ_DATA: if (I2C1STATbits.RBF){ //printToUart2("br:%d wr:%d dc:%d\n\r",byteRead, wordRead,byteCount); byteCount++; // read the received Data if (byteCount < 7) { currentMag.chData[byteRead] = (unsigned char)I2C1RCV; //printToUart2("byte:%d\n\r", I2C1RCV); } else { wordRead= (unsigned char)I2C1RCV; byteRead = 1; //printToUart2("ignored:%d\n\r", I2C1RCV); } // if we just read a word (i.e. byteRead == 1) if(byteRead == 0) { //printToUart2("%u\n\r",currentMag.usData); readMag(wordRead,currentMag.shData); wordRead++; } // flip the byte byteRead = byteRead == 0? 1 : 0; //if (wordRead >= 3){ if (byteCount >= 7 ){ // Done Generate a NACK I2C1CONbits.ACKDT = 1; I2C1CONbits.ACKEN = 1; } else { // Generate the AKN I2C1CONbits.ACKDT = 0; I2C1CONbits.ACKEN = 1; } } else if (!I2C1STATbits.ACKSTAT){ //if (wordRead < 3){ if (byteCount < 7){ // Enable reading I2C1CONbits.RCEN = 1; } else { // change the state i2c1State = READ_DONE; i2c1Stop(); } } break; case READ_STOP: if (!I2C1CONbits.PEN){ // Ready to read next magnetometer data i2c1State = READ_IDLE; } break; } // IFS1bits.MI2C1IF = 0; // Clear the master interrupt I2C1STATbits.IWCOL = 0; // Clear the collision flag just in case }