/** * Version IO Expander MCP23018 **/ #include "MCP23008.h" #define USB_BUFFER_LEN 4 #define NB_MCP 4 unsigned char readbuff[USB_BUFFER_LEN] absolute 0x500; unsigned char writebuff[USB_BUFFER_LEN] absolute 0x540; int reset_mcp23017 = 1; void interrupt() { USB_Interrupt_Proc(); // USB servicing is done inside the interrupt USBIF_bit = 0; } // define callback function void I2C1_TimeoutCallback(char errorCode) { if (errorCode == _I2C_TIMEOUT_RD) { //LED0 = 1; // do something if timeout is caused during read } if (errorCode == _I2C_TIMEOUT_WR) { //LED1 = 1; // do something if timeout is caused during write } if (errorCode == _I2C_TIMEOUT_START) { //LED0 = 1; // do something if timeout is caused during start } if (errorCode == _I2C_TIMEOUT_REPEATED_START) { //LED1 = 1; // do something if timeout is caused during repeated start } reset_mcp23017 = 1; } void main() { int i = 0; int cpt = 0; uint16_t temp; ANSELA = 0; ANSELB = 0; ANSELC = 0; SLRCON = 0; ADCON1 |= 0x0F; // Configure all ports with analog function as digital CM1CON0 |= 7; // Disable comparators TRISB = 0xff; TRISB.f2 = 0; LATB.f2 = 1; I2C1_Init(400000); // initialize I2C communication // set timeout period and callback function I2C1_SetTimeoutCallback(40000, I2C1_TimeoutCallback); reset_mcp23017 = 1; FSEN_bit = 1; UPUEN_bit = 1; HID_Enable(&readbuff,&writebuff); // Enable USB device interrupt IPEN_bit = 1; USBIP_bit = 1; USBIE_bit = 1; GIEH_bit = 1; while (1) { if (reset_mcp23017) { LATB.f2 = 0; //RESET MCP23017 Delay_ms(250); LATB.f2 = 1; Delay_ms(250); for (i=0; i<NB_MCP; i++) { MCPreset(i); Delay_ms(5); } reset_mcp23017 = 0; } memset(writebuff, 0x00, USB_BUFFER_LEN); for (i=0; i<NB_MCP; i++) { temp = MCPread(i, GPIO); writebuff[i] = ~temp; Delay_ms(5); if(reset_mcp23017) break; } HID_Write(writebuff, USB_BUFFER_LEN); Delay_ms(5); } }