|
| 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` are iterable, containing 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 | +| `reg` | Registers used by this IC | Yes | |
| 60 | +| 0-8 | `Pin` instances | Yes | |
| 61 | + |
| 62 | + |
| 63 | +### Methods |
| 64 | + |
| 65 | +#### constructor({ [address], [hz], [sda], [scl], [inputs], [pullups] }) |
| 66 | + |
| 67 | + | Property | Type | Value/Description | Default | Required | |
| 68 | + |----------|--------|----------------------|---------|----------| |
| 69 | + | `address` | Number | The address of the I2C device | `0x20` | no | |
| 70 | + | `hz` | Number | The clock speed of the I2C device. | 100kHz | no | |
| 71 | + | `sda` | Number | The I2C sda (data) pin. | 4 | no | |
| 72 | + | `scl` | Number | The I2C scl (clock) pin. | 5 | no | |
| 73 | + | `inputs` | Byte | A byte representing the input/output initialization state of the 8 GPIO pins. `1` for input, `0` for output | `0b11111111` | no | |
| 74 | + | pullups | Byte | A byte representing the pullup initialization state of the 8 GPIO pins. `1` for pullup, `0` for default | `0b00000000` | no | |
| 75 | + |
| 76 | +#### bankWrite(byte) |
| 77 | + |
| 78 | +The method will temporarily set the _mode/direction_ of all pins to _output_ mode and write all pins at once. |
| 79 | + |
| 80 | +```js |
| 81 | +let expander = new MCP23008(); // defaults to 0x20! |
| 82 | +expander.bankWrite(0b11111111); // Set all pins to 1 |
| 83 | +``` |
| 84 | + |
| 85 | +#### bankRead() -> byte |
| 86 | + |
| 87 | +The method will temporarily set the _mode/direction_ of all pins to _input_ mode and read all pins at once. |
| 88 | + |
| 89 | +```js |
| 90 | +let expander = new MCP23008(); // defaults to 0x20! |
| 91 | +trace(`${expander.bankRead()}\n`); |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +## class MCP23017 |
| 96 | + |
| 97 | +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. |
| 98 | + |
| 99 | +Instance objects of `MCP23017` are iterable, containing 16 `Pin` instance object entries. |
| 100 | + |
| 101 | + |
| 102 | +```js |
| 103 | +import Timer from "timer"; |
| 104 | +import {MCP23017} from "MCP230XX"; |
| 105 | + |
| 106 | + |
| 107 | +export default function() { |
| 108 | + let leds = new MCP23017(); // defaults to 0x20! |
| 109 | + let mask = 0x8888; |
| 110 | + |
| 111 | + Timer.repeat(() => { |
| 112 | + mask = ((mask << 1) | (mask >> 15)) & 0xFFFF; |
| 113 | + |
| 114 | + for (let i = 0; i < 16; i++) { |
| 115 | + leds[i].write(mask & (1 << i) ? 1 : 0); |
| 116 | + } |
| 117 | + }, 50); |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +### Properties |
| 124 | + |
| 125 | +| Property Name | Description | Read Only | |
| 126 | +|---------------| ----------- | ----------| |
| 127 | +| `length` | Number of pins in collection: `16` | Yes | |
| 128 | +| `offset` | Register offset: `1` | Yes | |
| 129 | +| `reg` | Registers used by this IC | Yes | |
| 130 | +| 0-16 | `Pin` instances | Yes | |
| 131 | + |
| 132 | + |
| 133 | +### Methods |
| 134 | + |
| 135 | +#### constructor({ [address], [hz], [sda], [scl], [inputs], [pullups] }) |
| 136 | + |
| 137 | + | Property | Type | Value/Description | Default | Required | |
| 138 | + |----------|--------|----------------------|---------|----------| |
| 139 | + | `address` | Number | The address of the I2C device | `0x20` | no | |
| 140 | + | `hz` | Number | The clock speed of the I2C device. | 100kHz | no | |
| 141 | + | `sda` | Number | The I2C sda (data) pin. | 4 | no | |
| 142 | + | `scl` | Number | The I2C scl (clock) pin. | 5 | no | |
| 143 | + | `inputs` | Word | A byte representing the input/output initialization state of the 8 GPIO pins. `1` for input, `0` for output | `0b1111111111111111` | no | |
| 144 | + | `pullups` | Word | A byte representing the pullup initialization state of the 16 GPIO pins. `1` for pullup, `0` for default | `0b0000000000000000` | no | |
| 145 | + |
| 146 | +#### bankWrite(word) |
| 147 | + |
| 148 | +The method will temporarily set the _mode/direction_ of all pins to _output_ mode and write all pins at once. |
| 149 | + |
| 150 | +```js |
| 151 | +let expander = new MCP23017(); // defaults to 0x20! |
| 152 | +expander.bankWrite(0b1111111111111111); // Set all pins to 1 |
| 153 | +``` |
| 154 | + |
| 155 | +#### bankRead() -> word |
| 156 | + |
| 157 | +The method will temporarily set the _mode/direction_ of all pins to _input_ mode and read all pins at once. |
| 158 | + |
| 159 | +```js |
| 160 | +let expander = new MCP23017(); // defaults to 0x20! |
| 161 | +trace(`${expander.bankRead()}\n`); |
| 162 | +``` |
| 163 | + |
| 164 | +## class Pin |
| 165 | + |
| 166 | +The `Pin` class represents a single pin within a `MCP23008` instance object. `Pin` is not exported. |
| 167 | + |
| 168 | + |
| 169 | +```js |
| 170 | +import Timer from "timer"; |
| 171 | +import { MCP23008 } from "MCP230XX"; |
| 172 | + |
| 173 | + |
| 174 | +export default function() { |
| 175 | + const leds = new MCP23008({ |
| 176 | + inputs: 0b00000000 |
| 177 | + }); |
| 178 | + |
| 179 | + leds[0].write(1); |
| 180 | + leds[1].write(0); |
| 181 | + leds[2].write(1); |
| 182 | + leds[3].write(0); |
| 183 | + leds[4].write(1); |
| 184 | + leds[5].write(0); |
| 185 | + leds[6].write(1); |
| 186 | + leds[7].write(0); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | +## Properties |
| 193 | + |
| 194 | +| Property Name | Description | Read Only | |
| 195 | +|---------------| ----------- | ----------| |
| 196 | +| `pin` | The GPIO pin number | Yes | |
| 197 | +| `offset` | Register offset: `1` | Yes | |
| 198 | +| `reg` | Registers used by this IC | Yes | |
| 199 | +| 0-16 | `Pin` instances | Yes | |
| 200 | + |
| 201 | + |
| 202 | +## Methods |
| 203 | + |
| 204 | +#### constructor({ [address], [hz], [sda], [scl], [inputs], [pullups] }) |
| 205 | + |
| 206 | + | Property | Type | Value/Description | Default | Required | |
| 207 | + |----------|--------|----------------------|---------|----------| |
| 208 | + | address | Number | The address of the I2C device | `0x20` | no | |
| 209 | + | hz | Number | The clock speed of the I2C device. | 100kHz | no | |
| 210 | + | sda | Number | The I2C sda (data) pin. | 4 | no | |
| 211 | + | scl | Number | The I2C scl (clock) pin. | 5 | no | |
| 212 | + | inputs | Word | A byte representing the input/output initialization state of the 8 GPIO pins. `1` for input, `0` for output | `0b1111111111111111` | no | |
| 213 | + | pullups | Word | A byte representing the pullup initialization state of the 8 GPIO pins. `1` for pullup, `0` for default | `0b0000000000000000` | no | |
| 214 | + |
| 215 | +#### mode(mode) |
| 216 | + |
| 217 | +Set the _mode/direction_ of the pin by object. |
| 218 | + |
| 219 | +#### read() |
| 220 | + |
| 221 | +Set the _mode/direction_ to _input_ and read the value of the pin object. |
| 222 | + |
| 223 | +#### write(value) |
| 224 | + |
| 225 | +Set the _mode/direction_ to _output_ and write the value to the pin object. |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | +## Manifest Example |
| 230 | + |
| 231 | +``` |
| 232 | +{ |
| 233 | + "include": [ |
| 234 | + "$(MODDABLE)/examples/manifest_base.json", |
| 235 | + "$(MODULES)/drivers/mcp230/manifest.json" |
| 236 | + ], |
| 237 | + "modules": { |
| 238 | + "*": [ |
| 239 | + "./main", |
| 240 | + ] |
| 241 | + }, |
| 242 | + "platforms": { |
| 243 | + "esp": { |
| 244 | + "modules": { |
| 245 | + "*": "$(MODULES)/pins/i2c/esp/*", |
| 246 | + }, |
| 247 | + }, |
| 248 | + "esp32": { |
| 249 | + "modules": { |
| 250 | + "*": "$(MODULES)/pins/i2c/esp32/*", |
| 251 | + }, |
| 252 | + }, |
| 253 | + } |
| 254 | +} |
| 255 | +``` |
0 commit comments