Control Code with I/O and ADC working

Dependencies:   MODSERIAL mbed

Committer:
jrodenburg
Date:
Mon Jan 22 20:11:52 2018 +0000
Revision:
0:a28a1035c31b
Child:
1:0182b86f9bd4
Code that has I/O working, after replacing the hotswap buffer with a dumb buffer

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 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 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 0:a28a1035c31b 31 void MCP23008::writeOutput(int chnl, bool val){
jrodenburg 0:a28a1035c31b 32 char data[2];
jrodenburg 0:a28a1035c31b 33 int chnSel = 0;
jrodenburg 0:a28a1035c31b 34
jrodenburg 0:a28a1035c31b 35 //select channel to read
jrodenburg 0:a28a1035c31b 36 switch (chnl){
jrodenburg 0:a28a1035c31b 37 case 0:
jrodenburg 0:a28a1035c31b 38 chnSel = CHNL_0;
jrodenburg 0:a28a1035c31b 39 break;
jrodenburg 0:a28a1035c31b 40 case 1:
jrodenburg 0:a28a1035c31b 41 chnSel = CHNL_1;
jrodenburg 0:a28a1035c31b 42 break;
jrodenburg 0:a28a1035c31b 43 case 2:
jrodenburg 0:a28a1035c31b 44 chnSel = CHNL_2;
jrodenburg 0:a28a1035c31b 45 break;
jrodenburg 0:a28a1035c31b 46 case 3:
jrodenburg 0:a28a1035c31b 47 chnSel = CHNL_3;
jrodenburg 0:a28a1035c31b 48 break;
jrodenburg 0:a28a1035c31b 49 }
jrodenburg 0:a28a1035c31b 50
jrodenburg 0:a28a1035c31b 51 //turn channel on
jrodenburg 0:a28a1035c31b 52 if(val == 1){
jrodenburg 0:a28a1035c31b 53 data[0] = GPIO; //register address
jrodenburg 0:a28a1035c31b 54 GPIO_Pin_Status = GPIO_Pin_Status | chnSel;
jrodenburg 0:a28a1035c31b 55 data[1] = GPIO_Pin_Status;
jrodenburg 0:a28a1035c31b 56 }
jrodenburg 0:a28a1035c31b 57
jrodenburg 0:a28a1035c31b 58 //turn channel off
jrodenburg 0:a28a1035c31b 59 else if(val == 0){
jrodenburg 0:a28a1035c31b 60 data[0] = GPIO; //register address
jrodenburg 0:a28a1035c31b 61 GPIO_Pin_Status = GPIO_Pin_Status & (0xFF^chnSel);
jrodenburg 0:a28a1035c31b 62 data[1] = GPIO_Pin_Status;//data to send to that address
jrodenburg 0:a28a1035c31b 63 }
jrodenburg 0:a28a1035c31b 64
jrodenburg 0:a28a1035c31b 65 i2c.write((addrI2C<<1)|(I2C_WRITE), data, 2);
jrodenburg 0:a28a1035c31b 66 wait(2);
jrodenburg 0:a28a1035c31b 67 }
jrodenburg 0:a28a1035c31b 68
jrodenburg 0:a28a1035c31b 69 void MCP23008::init(){
jrodenburg 0:a28a1035c31b 70 char io_setup[2];
jrodenburg 0:a28a1035c31b 71
jrodenburg 0:a28a1035c31b 72 io_setup[0] = IODIR;
jrodenburg 0:a28a1035c31b 73 io_setup[1] = 0x00; //turn all pins to outputs
jrodenburg 0:a28a1035c31b 74
jrodenburg 0:a28a1035c31b 75 i2c.write((addrI2C<<1)|(I2C_WRITE), io_setup, 2);
jrodenburg 0:a28a1035c31b 76 wait(2);
jrodenburg 0:a28a1035c31b 77
jrodenburg 0:a28a1035c31b 78 //Set all outputs to off
jrodenburg 0:a28a1035c31b 79 GPIO_Pin_Status = 0x00;
jrodenburg 0:a28a1035c31b 80 }