|
| 1 | +# MCP230XX |
| 2 | + |
| 3 | +Copyright 2017 Moddable Tech, Inc. |
| 4 | + |
| 5 | +Revised: Dec 19, 2017 |
| 6 | + |
| 7 | +The [MCP23008](http://www.microchip.com/wwwproducts/en/MCP23008) device provides 8-bit, general purpose, parallel I/O expansion for I2C bus applications. (Description from MCP23008 product page) |
| 8 | + |
| 9 | +The [MCP23017](http://www.microchip.com/wwwproducts/en/MCP23017) device provides 16-bit, general purpose, parallel I/O expansion for I2C bus applications. (Description from MCP23017 product page) |
| 10 | + |
| 11 | + |
| 12 | +## module "MCP230XX" |
| 13 | + |
| 14 | +The driver module "MCP230XX" exports the following: |
| 15 | + |
| 16 | +```js |
| 17 | +export { |
| 18 | + MCP23008, |
| 19 | + MCP23017 |
| 20 | +}; |
| 21 | +``` |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## class MCP23008 |
| 26 | + |
| 27 | +The `MCP23008` class produces instances that represent a single MCP23008 IC on the I2C bus. The `MCP23008` class extends an internal `Expander` class, which extends the `SMBus` class. `Expander` is not exported. |
| 28 | + |
| 29 | +Instance objects of `MCP23008` contain 8 `Pin` instance object entries. |
| 30 | + |
| 31 | + |
| 32 | +```js |
| 33 | +import Timer from "timer"; |
| 34 | +import {MCP23008} from "MCP230XX"; |
| 35 | + |
| 36 | + |
| 37 | +export default function() { |
| 38 | + let leds = new MCP23008(); // defaults to 0x20! |
| 39 | + let mask = 0x88; |
| 40 | + |
| 41 | + Timer.repeat(() => { |
| 42 | + mask = ((mask << 1) | (mask >> 7)) & 0xFF; |
| 43 | + |
| 44 | + for (let i = 0; i < 8; i++) { |
| 45 | + leds[i].write(mask & (1 << i) ? 1 : 0); |
| 46 | + } |
| 47 | + }, 50); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +### Properties |
| 54 | + |
| 55 | +| Property Name | Description | Read Only | |
| 56 | +|---------------| ----------- | ----------| |
| 57 | +| `length` | Number of pins in collection: `8` | Yes | |
| 58 | +| `offset` | Register offset: `0` | Yes | |
| 59 | +| `IODIR` | `IODIR` register: `0x00` | Yes | |
| 60 | +| `GPIO` | `GPIO` register: `0x06` | Yes | |
| 61 | +| `GPPU` | `GPPU` register: `0x09` | Yes | |
| 62 | +| 0-8 | `Pin` instances | Yes | |
| 63 | + |
| 64 | + |
| 65 | +### Methods |
| 66 | + |
| 67 | +#### constructor({ [address], [hz], [sda], [scl], [inputs], [pullups] }) |
| 68 | + |
| 69 | + | Property | Type | Value/Description | Default | Required | |
| 70 | + |----------|--------|----------------------|---------|----------| |
| 71 | + | `address` | Number | The address of the I2C device | `0x20` | no | |
| 72 | + | `hz` | Number | The clock speed of the I2C device. | 100kHz | no | |
| 73 | + | `sda` | Number | The I2C sda (data) pin. | 4 | no | |
| 74 | + | `scl` | Number | The I2C scl (clock) pin. | 5 | no | |
| 75 | + | `inputs` | Byte | A byte representing the input/output initialization state of the 8 GPIO pins. `1` for input, `0` for output | `0b11111111` | no | |
| 76 | + | pullups | Byte | A byte representing the pullup initialization state of the 8 GPIO pins. `1` for pullup, `0` for default | `0b00000000` | no | |
| 77 | + |
| 78 | +#### bankWrite(byte) |
| 79 | + |
| 80 | +The method will temporarily set the _mode/direction_ of all pins to _output_ mode and write all pins at once. |
| 81 | + |
| 82 | +```js |
| 83 | +let expander = new MCP23008(); // defaults to 0x20! |
| 84 | +expander.bankWrite(0b11111111); // Set all pins to 1 |
| 85 | +``` |
| 86 | + |
| 87 | +#### bankRead() -> byte |
| 88 | + |
| 89 | +The method will temporarily set the _mode/direction_ of all pins to _input_ mode and read all pins at once. |
| 90 | + |
| 91 | +```js |
| 92 | +let expander = new MCP23008(); // defaults to 0x20! |
| 93 | +trace(`${expander.bankRead()}\n`); |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | +## class MCP23017 |
| 98 | + |
| 99 | +The `MCP23017` class produces instances that represent a single MCP23017 IC on the I2C bus. The `MCP23017` class extends an internal `Expander` class, which extends the `SMBus` class. |
| 100 | + |
| 101 | +Instance objects of `MCP23017` contain 16 `Pin` instance object entries. |
| 102 | + |
| 103 | + |
| 104 | +```js |
| 105 | +import Timer from "timer"; |
| 106 | +import {MCP23017} from "MCP230XX"; |
| 107 | + |
| 108 | + |
| 109 | +export default function() { |
| 110 | + let leds = new MCP23017(); // defaults to 0x20! |
| 111 | + let mask = 0x8888; |
| 112 | + |
| 113 | + Timer.repeat(() => { |
| 114 | + mask = ((mask << 1) | (mask >> 15)) & 0xFFFF; |
| 115 | + |
| 116 | + for (let i = 0; i < 16; i++) { |
| 117 | + leds[i].write(mask & (1 << i) ? 1 : 0); |
| 118 | + } |
| 119 | + }, 50); |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +### Properties |
| 126 | + |
| 127 | +| Property Name | Description | Read Only | |
| 128 | +|---------------| ----------- | ----------| |
| 129 | +| `length` | Number of pins in collection: `16` | Yes | |
| 130 | +| `offset` | Register offset: `1` | Yes | |
| 131 | +| `IODIR` | `IODIR` register: `0x00` | Yes | |
| 132 | +| `GPIO` | `GPIO` register: `0x0C` | Yes | |
| 133 | +| `GPPU` | `GPPU` register: `0x12` | Yes | |
| 134 | +| 0-16 | `Pin` instances | Yes | |
| 135 | + |
| 136 | + |
| 137 | +### Methods |
| 138 | + |
| 139 | +#### constructor({ [address], [hz], [sda], [scl], [inputs], [pullups] }) |
| 140 | + |
| 141 | + | Property | Type | Value/Description | Default | Required | |
| 142 | + |----------|--------|----------------------|---------|----------| |
| 143 | + | `address` | Number | The address of the I2C device | `0x20` | no | |
| 144 | + | `hz` | Number | The clock speed of the I2C device. | 100kHz | no | |
| 145 | + | `sda` | Number | The I2C sda (data) pin. | 4 | no | |
| 146 | + | `scl` | Number | The I2C scl (clock) pin. | 5 | no | |
| 147 | + | `inputs` | Word | A byte representing the input/output initialization state of the 8 GPIO pins. `1` for input, `0` for output | `0b1111111111111111` | no | |
| 148 | + | `pullups` | Word | A byte representing the pullup initialization state of the 16 GPIO pins. `1` for pullup, `0` for default | `0b0000000000000000` | no | |
| 149 | + |
| 150 | +#### bankWrite(word) |
| 151 | + |
| 152 | +The method will temporarily set the _mode/direction_ of all pins to _output_ mode and write all pins at once. |
| 153 | + |
| 154 | +```js |
| 155 | +let expander = new MCP23017(); // defaults to 0x20! |
| 156 | +expander.bankWrite(0b1111111111111111); // Set all pins to 1 |
| 157 | +``` |
| 158 | + |
| 159 | +#### bankRead() -> word |
| 160 | + |
| 161 | +The method will temporarily set the _mode/direction_ of all pins to _input_ mode and read all pins at once. |
| 162 | + |
| 163 | +```js |
| 164 | +let expander = new MCP23017(); // defaults to 0x20! |
| 165 | +trace(`${expander.bankRead()}\n`); |
| 166 | +``` |
| 167 | + |
| 168 | +## class Pin |
| 169 | + |
| 170 | +The `Pin` class represents a single pin within a `MCP23008` instance object. `Pin` is not exported. |
| 171 | + |
| 172 | + |
| 173 | +```js |
| 174 | +import Timer from "timer"; |
| 175 | +import { MCP23008 } from "MCP230XX"; |
| 176 | + |
| 177 | + |
| 178 | +export default function() { |
| 179 | + const leds = new MCP23008({ |
| 180 | + inputs: 0b00000000 |
| 181 | + }); |
| 182 | + |
| 183 | + leds[0].write(1); |
| 184 | + leds[1].write(0); |
| 185 | + leds[2].write(1); |
| 186 | + leds[3].write(0); |
| 187 | + leds[4].write(1); |
| 188 | + leds[5].write(0); |
| 189 | + leds[6].write(1); |
| 190 | + leds[7].write(0); |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +## Properties |
| 197 | + |
| 198 | +| Property Name | Description | Read Only | |
| 199 | +|---------------| ----------- | ----------| |
| 200 | +| `pin` | The GPIO pin number | Yes | |
| 201 | +| `expander` | The instance of `Expander` that this `Pin` belongs to | Yes | |
| 202 | + |
| 203 | + |
| 204 | +## Methods |
| 205 | + |
| 206 | +#### constructor({ pin, expander }) |
| 207 | + |
| 208 | + | Property | Type | Value/Description | Default | Required | |
| 209 | + |----------|--------|----------------------|---------|----------| |
| 210 | + | pin | Number | The GPIO pin number | n/a | yes | |
| 211 | + | expander | Number | The instance of `Expander` that created this `Pin` instance | n/a | yes | |
| 212 | + |
| 213 | +#### mode(mode) |
| 214 | + |
| 215 | +Set the _mode/direction_ of the pin by object. |
| 216 | + |
| 217 | +#### read() |
| 218 | + |
| 219 | +Set the _mode/direction_ to _input_ and read the value of the pin object. |
| 220 | + |
| 221 | +#### write(value) |
| 222 | + |
| 223 | +Set the _mode/direction_ to _output_ and write the value to the pin object. |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | +## Manifest Example |
| 228 | + |
| 229 | +``` |
| 230 | +{ |
| 231 | + "include": [ |
| 232 | + "$(MODDABLE)/examples/manifest_base.json", |
| 233 | + "$(MODULES)/drivers/mcp230/manifest.json" |
| 234 | + ], |
| 235 | + "modules": { |
| 236 | + "*": [ |
| 237 | + "./main", |
| 238 | + ] |
| 239 | + }, |
| 240 | + "platforms": { |
| 241 | + "esp": { |
| 242 | + "modules": { |
| 243 | + "*": "$(MODULES)/pins/i2c/esp/*", |
| 244 | + }, |
| 245 | + }, |
| 246 | + "esp32": { |
| 247 | + "modules": { |
| 248 | + "*": "$(MODULES)/pins/i2c/esp32/*", |
| 249 | + }, |
| 250 | + }, |
| 251 | + } |
| 252 | +} |
| 253 | +``` |
0 commit comments