______________________________________________________________________ > < > MCP23016 driver v1.00 < > for < > PICos18 release 2.05 < > < > PICos18 - Real-time kernel for PIC18 family < > < > < > www.picos18.com www.pragmatec.net < >______________________________________________________________________< This file contains all necessary informations to use properly the Microchip IOexpander driver for PICos18 v2.xx. This driver lets you design a multi-task application using the MCP23016 that can manage 16 bits as input or output. The device has to be access through the I2C interface and this driver lets you have a safe access to the chip without woring about multiple I2C bus access. ______________________________________________________________________ > < > I - Zip file content < >______________________________________________________________________< The drv_MCP23016 zip file contains : - drv_MCP23016.c - drv_MCP23016.h : header file - drv_MCP23016.txt : this file To use the IOexpander driver, first unzip the file. Place the C and H files in your project directory. This driver is supposed to be use with PICos18 and has been tested with MPLAB 7.30 and C18 v3.00. This driver is suitable for PIC18 with the Microchip MCP23016 connected to the I2C bus. ______________________________________________________________________ > < > II - Hardware settings < >______________________________________________________________________< Have a look at the MCP23016 datasheet to connect your device properly to the I2C bus. If you already have a I2C install you don't have to plug additionnal pullup resistors. A basic intallation consists to connect the chip to the power lines (VDD and VSS) and to add an external resistor and capacitor to let the device sample the inputs (cf datasheet), then to connect the SDA and SCL lines to the PIC18 (with pullup resistors if needed). The MCP23016 manages a special interrupt line if you want to wakeup the PIC18 when an new input event occurs. Connect this INT line to a PIC18 external interrupt and write your own ISR fonction to read the IOexpander when the interrupt occurs. ______________________________________________________________________ > < > III - TASCDESC settings < >______________________________________________________________________< The MCP23016 driver needs the I2C driver to be install at first. Have a look at the drv_I2C.txt file to learn more on how to install such a driver. The MCP23016 driver is not exactly a driver but much more a library (a set of funtions) that let you interface the MCP23016 without wasting time to validate your own code during many hours. Then once you have properly install the I2C driver you don't need to do anything else in the tascdesc.c file. ______________________________________________________________________ > < > IV - MCP23016 usage < >______________________________________________________________________< First of all you need to declare a I2C_message structure in your task. It's mandatory for all the task that needs to use the I2C driver : I2C_message_t I2Cmsg; In the TASK0 task of the MyApp directory, include the driver headers: #include "drv_MCP23016.h" And declare a MCP23016 structure to manage IO: MCP23016_t IOexp; See hereafter a basic example to have a correct access to the MCP23016: (the task access the MCP23016 every minute to know the IO values) #include "define.h" #include "drv_MCP23016.h" #define MCP23016_ADRESS 0b01000010 I2C_message_t IOmsg; MCP23016_t IOexp; /********************************************************************** * ----------------------------- TASK0 -------------------------------- * * First task of the tutorial. * **********************************************************************/ TASK(TASK0) { IOExpander_Configuration(); /* Do not forget to declare an alarm in tascdesc.c file (index 0) */ SetRelAlarm(0, 60000, 60000); /* every minute */ while (1) { WaitEvent(ALARM_EVENT); ClearEvent(ALARM_EVENT); IOexp.Register = GP0; MCP23016_Read(&I2Cmsg, &IOexp); } } void IOExpander_Configuration(void) { IOexp.Address = MCP23016_ADRESS; // Direction definition IOexp.Register = IODIR0; IOexp.Data = 0x00; MCP23016_Write(&I2Cmsg, &IOexp); IOexp.Register = IODIR1; IOexp.Data = 0x00; MCP23016_Write(&I2Cmsg, &IOexp); // Polarity definition IOexp.Register = IPOL0; IOexp.Data = 0xFF; MCP23016_Write(&I2Cmsg, &IOexp); IOexp.Register = IPOL1; IOexp.Data = 0xFF; MCP23016_Write(&I2Cmsg, &IOexp); // Outputs set to 0 IOexp.Register = OLAT0; IOexp.Data = 0x00; MCP23016_Write(&I2Cmsg, &IOexp); IOexp.Register = OLAT1; IOexp.Data = 0x00; MCP23016_Write(&I2Cmsg, &IOexp); // INT set to 0 IOexp.Register = GP0; MCP23016_Read(&I2Cmsg, &IOexp); IOexp.Register = GP1; MCP23016_Read(&I2Cmsg, &IOexp); } ______________________________________________________________________ > < > V - Conclusion < >______________________________________________________________________< The MCP23016 driver offers 2 basic functions to have an easy access to the Microchip IOexpander. You can use the device to read or write the 16 IO bits independently. First of all modify the IOExpander_Configuration() to setup the device according to your project specifications. DO not forget to connect the MCP23016 INT pin to the PIC18 if needed and add a new entry to the InterruptVectorL to treat the interrupt. /* End of File : drv_DS1337.txt */