Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MCP23008/MCP23008.cpp@6:c980535393ed, 2018-03-07 (annotated)
- Committer:
 - jrodenburg
 - Date:
 - Wed Mar 07 18:50:10 2018 +0000
 - Revision:
 - 6:c980535393ed
 - Parent:
 - 5:0f38a0bd4f86
 - Child:
 - 7:8a5e65e63e2a
 
Most recent code, running on test rack.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| jrodenburg | 0:a28a1035c31b | 1 | #include "mbed.h" | 
| jrodenburg | 0:a28a1035c31b | 2 | #include "MCP23008.h" | 
| jrodenburg | 0:a28a1035c31b | 3 | #include "MODSERIAL.h" | 
| jrodenburg | 0:a28a1035c31b | 4 | |
| jrodenburg | 0:a28a1035c31b | 5 | MODSERIAL pc2(USBTX, USBRX); | 
| jrodenburg | 0:a28a1035c31b | 6 | |
| jrodenburg | 0:a28a1035c31b | 7 | namespace { | 
| jrodenburg | 0:a28a1035c31b | 8 | const uint8_t I2C_WRITE = 0x00; | 
| jrodenburg | 0:a28a1035c31b | 9 | const uint8_t I2C_READ = 0x01; | 
| jrodenburg | 0:a28a1035c31b | 10 | |
| jrodenburg | 0:a28a1035c31b | 11 | /* MCP23008 registers */ | 
| jrodenburg | 0:a28a1035c31b | 12 | const uint8_t IODIR = 0x00; | 
| jrodenburg | 0:a28a1035c31b | 13 | const uint8_t GPIO = 0x09; | 
| jrodenburg | 5:0f38a0bd4f86 | 14 | const uint8_t PULLUP = 0x06; | 
| jrodenburg | 0:a28a1035c31b | 15 | |
| jrodenburg | 0:a28a1035c31b | 16 | //Channel addresses | 
| jrodenburg | 0:a28a1035c31b | 17 | const uint8_t CHNL_0 = 0x01; | 
| jrodenburg | 0:a28a1035c31b | 18 | const uint8_t CHNL_1 = 0x02; | 
| jrodenburg | 0:a28a1035c31b | 19 | const uint8_t CHNL_2 = 0x04; | 
| jrodenburg | 0:a28a1035c31b | 20 | const uint8_t CHNL_3 = 0x08; | 
| jrodenburg | 0:a28a1035c31b | 21 | }; | 
| jrodenburg | 0:a28a1035c31b | 22 | |
| jrodenburg | 0:a28a1035c31b | 23 | MCP23008::MCP23008 (PinName sda, PinName scl, uint8_t address, int freq): i2c( sda, scl ){ | 
| jrodenburg | 0:a28a1035c31b | 24 | addrI2C = address; | 
| jrodenburg | 0:a28a1035c31b | 25 | i2c.frequency(freq); | 
| jrodenburg | 0:a28a1035c31b | 26 | } | 
| jrodenburg | 0:a28a1035c31b | 27 | |
| jrodenburg | 0:a28a1035c31b | 28 | void MCP23008::setAddress(int address){ | 
| jrodenburg | 0:a28a1035c31b | 29 | addrI2C = address; | 
| jrodenburg | 0:a28a1035c31b | 30 | } | 
| jrodenburg | 0:a28a1035c31b | 31 | |
| jrodenburg | 1:0182b86f9bd4 | 32 | void MCP23008::writeOutput(int chn1, int chn2, int chn3, int chn4){ | 
| jrodenburg | 0:a28a1035c31b | 33 | char data[2]; | 
| jrodenburg | 5:0f38a0bd4f86 | 34 | char data2[2]; | 
| jrodenburg | 1:0182b86f9bd4 | 35 | uint8_t val = 0x00; | 
| jrodenburg | 1:0182b86f9bd4 | 36 | |
| jrodenburg | 1:0182b86f9bd4 | 37 | //Create mask for all bits that should be on | 
| jrodenburg | 1:0182b86f9bd4 | 38 | if(chn1) val = val | CHNL_0; | 
| jrodenburg | 1:0182b86f9bd4 | 39 | if(chn2) val = val | CHNL_1; | 
| jrodenburg | 1:0182b86f9bd4 | 40 | if(chn3) val = val | CHNL_2; | 
| jrodenburg | 1:0182b86f9bd4 | 41 | if(chn4) val = val | CHNL_3; | 
| jrodenburg | 0:a28a1035c31b | 42 | |
| jrodenburg | 1:0182b86f9bd4 | 43 | data[0] = GPIO; //register address | 
| jrodenburg | 1:0182b86f9bd4 | 44 | data[1] = val; //GPIO bit map | 
| jrodenburg | 5:0f38a0bd4f86 | 45 | data2[0] = PULLUP; | 
| jrodenburg | 5:0f38a0bd4f86 | 46 | data2[1] = 0x00; | 
| jrodenburg | 1:0182b86f9bd4 | 47 | |
| jrodenburg | 5:0f38a0bd4f86 | 48 | i2c.write((addrI2C<<1)|(I2C_WRITE), data, 2); | 
| jrodenburg | 5:0f38a0bd4f86 | 49 | i2c.write((addrI2C<<1)|(I2C_WRITE), data2, 2); | 
| jrodenburg | 6:c980535393ed | 50 | wait(0.2); | 
| jrodenburg | 0:a28a1035c31b | 51 | } | 
| jrodenburg | 0:a28a1035c31b | 52 | |
| jrodenburg | 0:a28a1035c31b | 53 | void MCP23008::init(){ | 
| jrodenburg | 0:a28a1035c31b | 54 | char io_setup[2]; | 
| jrodenburg | 0:a28a1035c31b | 55 | |
| jrodenburg | 0:a28a1035c31b | 56 | io_setup[0] = IODIR; | 
| jrodenburg | 0:a28a1035c31b | 57 | io_setup[1] = 0x00; //turn all pins to outputs | 
| jrodenburg | 0:a28a1035c31b | 58 | |
| jrodenburg | 0:a28a1035c31b | 59 | i2c.write((addrI2C<<1)|(I2C_WRITE), io_setup, 2); | 
| jrodenburg | 6:c980535393ed | 60 | wait(0.2); | 
| jrodenburg | 0:a28a1035c31b | 61 | |
| jrodenburg | 0:a28a1035c31b | 62 | //Set all outputs to off | 
| jrodenburg | 0:a28a1035c31b | 63 | GPIO_Pin_Status = 0x00; | 
| jrodenburg | 0:a28a1035c31b | 64 | } |