A Simple MCP23008 Object
idbruce
Posts: 6,197
Perhaps this may be of use to someone
{ SimpleMCP23008 The name of this object is not intended to imply that the MCP23008 8-Bit I/O Expander is a simple device, but rather that the object does not take full advantage of all the features provided by the chip. In fact, I would consider this a bare essentials object for using the MCP23008. This object is based upon the work of Mike Green, James Burrows, and Dr_Acula Example Usage: PUB Test_MCP23008 | Value 'GP0 Menu Button 'GP1 Select Button 'GP2 Scroll Button 'GP3 Pause Button 'GP4 Back Button 'GP5 Exit Button 'GP6 Yellow LED 'GP7 Red LED Expander.Initialize 'Configure the IODIR register to 2 outputs (LEDs) and 6 inputs (pushbuttons) Expander.Configure_IODIR_Register(%00111111) 'Configure the GPIO register to set GP7 to HIGH to light the red LED Expander.Configure_GPIO_Register(%10000000) REPEAT 'Obtain the value of the GPIO register Value := Expander.GetPinStates IF Value == %10010000 'If GP4 is HIGH 'Set GP7 and GP6 to HIGH to light the red and yellow LEDs Expander.Configure_GPIO_Register(%11000000) ELSE 'Set GP7 to HIGH to light the red LED Expander.Configure_GPIO_Register(%10000000) } CON SCL = 28 SDA = 29 PUB Initialize OUTA[SCL] := 1 DIRA[SCL] := 1 DIRA[SDA] := 0 REPEAT 9 OUTA[SCL] := 0 OUTA[SCL] := 1 IF INA[SDA] QUIT PUB Start OUTA[SCL]~~ DIRA[SCL]~~ OUTA[SDA]~~ DIRA[SDA]~~ OUTA[SDA]~ OUTA[SCL]~ PUB Stop OUTA[SCL]~~ OUTA[SDA]~~ DIRA[SCL]~ DIRA[SDA]~ PUB Write(Data) '' Write i2c data. Data byte is output MSB first, SDA data line is valid '' only while the SCL line is HIGH. Data is always 8 bits (+ ACK/NAK). '' SDA is assumed LOW and SCL and SDA are both left in the LOW state. Data <<= 24 REPEAT 8 OUTA[SDA] := (Data <-= 1) & 1 OUTA[SCL]~~ OUTA[SCL]~ DIRA[SDA]~ OUTA[SCL]~~ OUTA[SCL]~ OUTA[SDA]~ DIRA[SDA]~~ PUB Configure_IODIR_Register(Value) 'This method sets the input and output pins of the MCP23008. 'Set GP7 and GP6 as outputs, and GP5, GP4, GP3, GP2, GP1, and GP0 as inputs. 'E.G.: Expander.Configure_IODIR_Register(%00111111) Start Write($40) Write($0) Write(Value) Stop PUB Configure_GPIO_Register(Value) 'This method sets the state of the input and output pins to HIGH or LOW. 'Assuming that GP7 and GP6 control LEDs and that GP5, GP4, GP3, GP2, GP1, and GP0 'are input from tact switches, the LEDs can be set HIGH and the tact switches can 'be set LOW with the following call. 'E.G.: Expander.Configure_GPIO_Register(%11000000) Start Write($40) Write($09) Write(Value) Stop PUB Read : Data Data := 0 DIRA[SDA]~ REPEAT 8 OUTA[SCL]~~ Data := (Data << 1) | INA[SDA] OUTA[SCL]~ OUTA[SDA] := 1 DIRA[SDA]~~ OUTA[SCL]~~ OUTA[SCL]~ OUTA[SDA]~ RETURN Data PUB GetPinStates : Value Start Write($40 | 0) Write($09) Start Write($40 | 1) Value := Read Stop RETURN Value {{ ******************************************************************************************************************************** * TERMS OF USE: MIT License * ******************************************************************************************************************************** *Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation * *files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, * *modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software* *is furnished to do so, subject to the following conditions: * * * *The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.* * * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * *WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * *COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * *ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ******************************************************************************************************************************** }}