Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Wed Jul 18 21:28:45 2018 +0000
Revision:
21:f87464a7e7c6
Parent:
20:cdeed4dad690
Code with diagnostics

Who changed what in which revision?

UserRevisionLine numberNew 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
jrodenburg 0:a28a1035c31b 6 namespace {
jrodenburg 0:a28a1035c31b 7 const uint8_t I2C_WRITE = 0x00;
jrodenburg 0:a28a1035c31b 8 const uint8_t I2C_READ = 0x01;
jrodenburg 0:a28a1035c31b 9
jrodenburg 0:a28a1035c31b 10 /* MCP23008 registers */
jrodenburg 0:a28a1035c31b 11 const uint8_t IODIR = 0x00;
jrodenburg 0:a28a1035c31b 12 const uint8_t GPIO = 0x09;
jrodenburg 5:0f38a0bd4f86 13 const uint8_t PULLUP = 0x06;
jrodenburg 0:a28a1035c31b 14
jrodenburg 0:a28a1035c31b 15 //Channel addresses
jrodenburg 0:a28a1035c31b 16 const uint8_t CHNL_0 = 0x01;
jrodenburg 0:a28a1035c31b 17 const uint8_t CHNL_1 = 0x02;
jrodenburg 0:a28a1035c31b 18 const uint8_t CHNL_2 = 0x04;
jrodenburg 0:a28a1035c31b 19 const uint8_t CHNL_3 = 0x08;
jrodenburg 0:a28a1035c31b 20 };
jrodenburg 0:a28a1035c31b 21
jrodenburg 0:a28a1035c31b 22 MCP23008::MCP23008 (PinName sda, PinName scl, uint8_t address, int freq): i2c( sda, scl ){
jrodenburg 0:a28a1035c31b 23 addrI2C = address;
jrodenburg 0:a28a1035c31b 24 i2c.frequency(freq);
jrodenburg 0:a28a1035c31b 25 }
jrodenburg 0:a28a1035c31b 26
jrodenburg 0:a28a1035c31b 27 void MCP23008::setAddress(int address){
jrodenburg 0:a28a1035c31b 28 addrI2C = address;
jrodenburg 0:a28a1035c31b 29 }
jrodenburg 0:a28a1035c31b 30
jrodenburg 21:f87464a7e7c6 31 int MCP23008::writeOutput(int chn1, int chn2, int chn3, int chn4){
jrodenburg 0:a28a1035c31b 32 char data[2];
jrodenburg 5:0f38a0bd4f86 33 char data2[2];
jrodenburg 1:0182b86f9bd4 34 uint8_t val = 0x00;
jrodenburg 1:0182b86f9bd4 35
jrodenburg 1:0182b86f9bd4 36 //Create mask for all bits that should be on
jrodenburg 1:0182b86f9bd4 37 if(chn1) val = val | CHNL_0;
jrodenburg 1:0182b86f9bd4 38 if(chn2) val = val | CHNL_1;
jrodenburg 1:0182b86f9bd4 39 if(chn3) val = val | CHNL_2;
jrodenburg 1:0182b86f9bd4 40 if(chn4) val = val | CHNL_3;
jrodenburg 0:a28a1035c31b 41
jrodenburg 1:0182b86f9bd4 42 data[0] = GPIO; //register address
jrodenburg 1:0182b86f9bd4 43 data[1] = val; //GPIO bit map
jrodenburg 5:0f38a0bd4f86 44 data2[0] = PULLUP;
jrodenburg 5:0f38a0bd4f86 45 data2[1] = 0x00;
jrodenburg 1:0182b86f9bd4 46
jrodenburg 5:0f38a0bd4f86 47 i2c.write((addrI2C<<1)|(I2C_WRITE), data, 2);
jrodenburg 21:f87464a7e7c6 48 return i2c.write((addrI2C<<1)|(I2C_WRITE), data2, 2);
jrodenburg 21:f87464a7e7c6 49 //wait(0.02);
jrodenburg 0:a28a1035c31b 50 }
jrodenburg 0:a28a1035c31b 51
jrodenburg 0:a28a1035c31b 52 void MCP23008::init(){
jrodenburg 0:a28a1035c31b 53 char io_setup[2];
jrodenburg 0:a28a1035c31b 54
jrodenburg 0:a28a1035c31b 55 io_setup[0] = IODIR;
jrodenburg 0:a28a1035c31b 56 io_setup[1] = 0x00; //turn all pins to outputs
jrodenburg 0:a28a1035c31b 57
jrodenburg 0:a28a1035c31b 58 i2c.write((addrI2C<<1)|(I2C_WRITE), io_setup, 2);
jrodenburg 20:cdeed4dad690 59 //wait(0.1);
jrodenburg 0:a28a1035c31b 60
jrodenburg 0:a28a1035c31b 61 //Set all outputs to off
jrodenburg 0:a28a1035c31b 62 GPIO_Pin_Status = 0x00;
jrodenburg 0:a28a1035c31b 63 }